Laravel

Laravel Login Authentication

Laravel Login Authentication
Written by ProgrammingPot

In this tutorial, I’m going to cover the Laravel Login Authentication. The main aim of this article is to create a login authentication system which is provided by the laravel.

Laravel provides a very solid authentication system in the core that makes the implementation of basic authentication a breeze. In fact, you just need to run a couple of artisan commands to set up the scaffolding of an authentication system.

Laravel Login Authentication


Requirements

  • Working Laravel Framework (I am using Laravel 5.5)
  • MySQL with a 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. The controller that is specific to the authentication module is 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 its 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.

Laravel Login Authentication

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
Laravel Login Authentication

 

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

 

Laravel Login Authentication

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.

 

Laravel Login Authentication

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

Laravel Login Authentication

This is out of the box authentication functionality provided by Laravel and should fit most of the applications. However, there might be the need for customization and Laravel authentication module if highly customizable 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.

Also read: Laravel Simple CRUD Operation

Leave a Comment

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