Laravel Framework: Authentication
Laravel makes implementing authentication very simple. In fact, almost everything is configured for you out of the box. The authentication configuration file is located at
config/auth.php, which contains several well documented options for tweaking the behavior of the authentication services.
Laravel ships with several pre-built authentication controllers, which are located in the
RegisterController handles new user registration,
LoginController handles authentication,
ForgotPasswordController handles e-mailing links for resetting passwords, and
ResetPasswordController contains the logic to reset passwords.
Each of these controllers uses a trait to include their necessary methods. For many applications, you will not need to modify these controllers at all.
Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command:
php artisan make:auth
This command should be used on fresh applications and will install a layout view, registration and login views, as well as routes for all authentication end-points. A
HomeController will also be generated to handle post-login requests to your application’s dashboard.
As mentioned in the previous section, the
php artisan make:auth command will create all of the views you need for authentication and place them in the
make:auth command will also create a
resources/views/layouts directory containing a base layout for your application. All of these views use the Bootstrap CSS framework, but you are free to customize them however you wish.
By default, Laravel includes an
App\User Eloquent model in your
app directory. plus two migrations;
You have to setup the database configuration in your
.env file, else you will get an error like:
SQLSTATE[HY000]  Unknown database 'homestead'
If you have configured your database connection properly, now add the tables to the database by running;
php artisan migrate
This will the migrations,
password_resets table to you database.
Now that you have routes and views setup for the included authentication controllers, you are ready to register and authenticate new users for your application! You may simply access your application in a browser since the authentication controllers already contain the logic (via their traits) to authenticate existing users and store new users in the database.
Upon, successful registration or login, you will be straight away redirected to the
The Laravel documentation is a very rich resource to find out more ways you can customize authentication in your application. In our next tutorial; I will discuss who to setup SMTP in your application to enable user registration verification by email, as well as password resets over email.