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.
Contollers
Laravel ships with several pre-built authentication controllers, which are located in theApp\Http\Controllers\Auth
namespace.
The RegisterController
handles new user registration,
The LoginController
handles authentication,
The ForgotPasswordController
handles e-mailing links for resetting passwords, and
The 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.
Routing
Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command:
1 |
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.
Views
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 resources/views/auth
directory.
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.
Database Considerations
By default, Laravel includes an App\User
Eloquent model in your app
directory. plus two migrations; create_users_table
, and create_password_resets_table
your database/migrations
directory.
You have to setup the database configuration in your .env
file, else you will get an error like:
1 |
SQLSTATE[HY000] [1049] Unknown database 'homestead' |
If you have configured your database connection properly, now add the tables to the database by running;
1 |
php artisan migrate |
This will the migrations, users
and password_resets
table to you database.
Authenticating
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.
Your will realize that your welcome
page now includes a login
, and register
link which where not there before. And there will be a reset password
link on the login page.
Upon, successful registration or login, you will be straight away redirected to the home
page.
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.