Laravel framework comes with an out of box working authentication functionality for your application that includes Login, Logout, Register, Forgot Password and Remember me functions.

Laravel authentication tutorial shows you how you can enable the functionality in your application.

Requirements

  • Working Laravel Framework (I am using Laravel 5.5)
  • MySQL with database created to be used by your application.
  • Configuration changes in .env file to set database properties.

Step 1 : Laravel Installation

Make sure you have a working Laravel application on your machine. Also you have configured your .env file as per the database you are using. Once you have installed laravel. It should have the following folder structure.

As you can see Laravel application already have some classes that are specific to the Authentication functionality. Controller that are specific to the authentication module are under app > Controllers > Auth

Step 2 : Run make:auth command

On your project root directory run following command.

php artisan make:auth

This command will generate the required Controller files, views and routes that are required for the Authentication. If you open your route file (resources > routes > web.php) you should see the Route generated Auth::routes();

This specifically means following code.

   $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
   $this->post('login', 'Auth\LoginController@login');
   $this->post('logout', 'Auth\LoginController@logout')->name('logout');
     
   // Registration Routes...
   $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
   $this->post('register', 'Auth\RegisterController@register');
   // Password Reset Routes...
   $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
   $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
   $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
   $this->post('password/reset', 'Auth\ResetPasswordController@reset');

This command will create a HomeController.php file under app > Http > Controllers which is a basic controller and makes use of auth middleware to protect it’s functions to be accessed without being logged in.

public function __construct()
{
   $this->middleware('auth');
}

You can also see the view files generated by this command.

 

Step 3 : Run migrate command

Next, we need to run the migrate command to create the tables required for authentication. Run the following command on project Root in your terminal.

php artisan migrate

Once completed, it should create following tables in your database.

That’s It, Only these steps are required to enable the out of box authentication module provided by Laravel framework.

Step 4 : Test

Let’s test the authentication to see if everything is working fine. Open /register page in your laravel application and you should see the following page.

Click on the Register button and it should create a entry into the users table with Hashed Password and a unique remember_token. You can now login into application by going to /login page.

 

This is out of the box authentication functionality provided by Laravel and should fit most of the applications. However there might be need for customisation and Laravel authentication module if highly customisable as per needs.

Great Job on Learning to Implement the Laravel’s Out of Box Authentication Functionality. Once you have the basic authentication ready on your application. You might find the following tutorials useful.


Digital Ocean Refferal

Comments (11)

  • Nibin Reply

    Really helped me, tnx

    January 26, 2018 at 3:13 pm
  • Mattieu Reply

    It would be nice if you can customize the tutorial

    March 27, 2018 at 11:56 pm
    • tgugnani Reply

      Mattieu,
      How do you want the tutorial to be customized?

      March 28, 2018 at 10:05 am
      • Naufal Reply

        i think he mean how to customize auth view

        September 11, 2018 at 2:05 pm
  • Bernard Ben Reply

    Nice one, it really help me alot.

    May 10, 2018 at 7:14 am
  • bilal Reply

    how to mantain session in laravel 5.6
    tell me pleace

    July 11, 2018 at 10:49 am
  • Afia Reply

    Hi, Can you show us how to make registration and login system manually with proper validation? Thanks!

    July 31, 2018 at 4:12 am
  • USAAM ABID Reply

    I follow the same steps but the dropdown menu is not working and logout option is not showing.

    January 14, 2019 at 9:40 pm
  • Romil Reply

    in step no. 3
    migration , password_resets, and users…..
    whats their attributes

    March 9, 2019 at 5:57 am
  • Amir Reply

    Can you show, how to handle multi-roles (multiple users) using laravel auth?

    April 3, 2019 at 9:38 am

Leave a comment

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

*
*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.