How to Setup Laravel Authentication in few Simple Step

In this article we will discuss how to implement authentication in laravel framework quickly. Setup laravel in few simple step is quick and easy.

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.

Requirements

  • Working Laravel installation (in this article we use version 5.8)

Step 1 : Laravel Installation

To make an authentication we have to make sure we have a working installation of laravel. In case you do not have one please refer to this article “Complete guide installation laravel 5.8 on linux” to install it on your computer. If you have it installed you should notice a directory structure similar to this.

Laravel basic structure directory

You can notice that laravel already have a folder under Controllers that’s take care of authentication. We will make it works in the next step.

Step 2 : Run make:auth command

Laravel uses the following command to create forms and the associated controllers to perform authentication.

Go to your project root directory and execute the 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 look at the files (routes>web.php) you will notice a line Auth::routes(); the content of this files is the routes for the login process.

   $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 also create a file named HomeController under the Controllers directory. Let’s take a look at this file.

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
 
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
}

In the constructor of this class you can notice the line $this->middleware(‘auth’):. This line restrict all the content of this controller to authenticated users.

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
Migration created by laravel

Once complete this command will create in your database the table users and the table password_resets.

At this point you already setup laravel authentication in few simple steps. Now let’s test all of that.

Step 4 : Testing

We gonna check that everything work. To start your application if it’s not already started run this command.

php artisan serve

Our Home page should look like the following capture. Notice the link we have now in the top login and register.

Laravel welcome page

Now click on the register page and register a user .

After registered your user you can click on the login page to login.

This the authentication provided by laravel it’s should be fit for most of application but you can always customize it to fit our need. In a next article i will discuss about how we can customize the login in laravel.

This is the end of our guide on how to Setup laravel authentication in a few simple step. Thanks you for reading if you have a question please ask in comment box below.

You may also like...

1 Response

  1. Hi there, just became aware of your blog through Google, and found that it is truly
    informative. I am gonna watch out for brussels. I’ll be grateful if you
    continue this in future. Lots of people will be benefited from
    your writing. Cheers!

Leave a Reply

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