Laravel Authentication with Example

January 30, 2018


Laravel Authentication with Example

One of the best features of Laravel is its Authentication component. Almost everything required for authentication is configured by Laravel which is available in each Laravel application out of the box. We can create a working authentication model with a single artisan command. You can try and create a fresh laravel application before running this command:

php artisan make:auth

All the configuration relating to authentication are stored in config/auth.php file. All the authentication route, controller, layout views, database migrations and models are available in all the Laravel application (of course, unless we manually delete). You can always pause and have a look at these files in your project for better understanding.

# Auth Routes

Routes can be protected with auth middleware defined at Illuminate\Auth\Middleware\Authenticate:

Route::get('dashboard', function () {
    // view for authenticated user

# Auth Controllers

Default authentication controllers are stored in App\Http\Controllers\Auth directory. Controller names are self-explanatory though, RegisterController manages new user registrations, LoginController handles user authentication, ForgotPasswordController manages e-mail links for password resetting and ResetPasswordController stores reset password logic. HomeController is generated in root controllers directory that handles post-login requests.

# Auth Views

As we run the auth command (mentioned earlier in this section), Laravel creates all the necessary views for authentication in resources/views/layouts namespace.

# Retrieving Authenticated User

We can access the authenticated user with the help of Auth facade.

use Illuminate\Support\Facades\Auth;

$user = Auth::user();

We can also validate whether the current user is authenticated or not by using the check method on Auth facade. It returns a boolean value as a result:

use Illuminate\Support\Facades\Auth;

if (Auth::check()) {
    // logic if logged in

# Logging out

Though this is shipped with the Laravel auth command utilization, yet we will discuss it here merely because of its multiple usability use cases. To log users out of the application, we have to use logout() method of Auth facade. This removes authentication values of a user from the session. To logout a user, simply use:


We just completed one of the most looked forward topic of Laravel – The Authentication. Laravel makes the job of a developer really easy in terms of Authentication with built-in scaffolding and useful methods. Note that we can also authenticate users without using Laravel’s system by working on the logic and calling auth methods manually. But I don’t really see a point as to why you would want to that yourself, unless you have some complex requirements which aren’t already covered by Laravel’s auth system. Hence, try to get familiar with it.

Questions & Comments:

Thanks for reading. Hope you had a fulfilling read about Authentication in Laravel. Drop your suggestions and questions in the comment section below.

Leave a comment

Your email address will not be published. Required fields are marked *