In this tutorial you will learn about the Laravel 5.8 User Registration And Login System and its application with practical example.
Laravel 5.8 User Registration And Login System
Laravel comes with an built-in authentication system, that includes out of the box user registration, login, logout, forgot password and remember me functionality. It saves us a lot of time building a custom login and registration system starting from scratch. In this step by step tutorial, we will guide you through building user registration and login using laravel’s built-in authentication package.
Before starting with tutorial, we are assuming that you already have a fresh installation of a Laravel 5.8. If you have not installed it follow Laravel Installation Step.
Install Laravel 5.8
First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel larablog |
Make sure you have composer installed. Now, lets switch to the project directory and start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/
Output:-
Configure Database In .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara58blog DB_USERNAME=root DB_PASSWORD= |
Generate Laravel Application Key
Now, use the following command in terminal to generate laravel application key.
1 |
php artisan key:generate |
Create Database Migration
Before you run migration, open app/providers/AppServiceProvider.php file and put following two line of code in boot method as following –
1 2 3 4 |
Use Schema; public function boot(){ Schema::defaultStringLength(191); } |
Now, use the below command to create required user authentication tables and model automatically.
1 |
php artisan migrate |
Authentication Scaffolding
Now, use the below command to generate authentication scaffolding –
1 |
php artisan make:auth |
This command will generate required Controller files, views and add routes in our web.php routes file that are required for the authentication. If you open your route file (resources/routes/web.php) you can see the authentication route added as following –
1 |
Auth::routes(); |
The Auth::rutes()
is a set of common authentication, registration and password reset routes.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$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'); |
Restart Development Server
Restart the development server using following artisan command –
1 |
php artisan serve |
Test Laravel 5.8 User Authentication
Now, you can see following authentication pages –
http://localhost:8000/register
http://localhost:8000/login
http://localhost:8000/password/reset