Please note that this tutorial will not work with laravel > 5.2. This is an outdated tutorial.
This tutorial assumes you have already setup laravel 5.0/5.1 project. If you have not, then refer to my first tutorial of laravel 5.0/5.1 series on how to setup laravel & xampp on linux. This tutorial also assumes you have twitter bootstrap setup with your project. [Optional] If not, then refer to the second tutorial. Come back here when you have setup everything and are ready to add authentication to your laravel 5.0/5.1 project.
In laravel 5.0/5.1, the authentication has been made really simple. Earlier for laravel 4, I personally preferred to use Confide because it was simple to use and very flexible but laravel 5.0/5.1 makes it really easy and I don’t need to use confide anymore. The authentication configuration file is located at config/auth.php. It also provides default migrations for user table. These are located in database/migrations/. For example,
Now open the _create_users_table file and you will see two methods already in it. Nothing is to be changed in these files until and unless you want to add or edit any field that will be created in the database. Not much has changed in case of migrations. It is still very much the same as it was in laravel 4.x. So if you want to learn more on migrations and seeding, refer to this post. By default, you should have all the important fields like name, email, password, etc. Now let us use artisan to create these tables. Execute the following commands and you should get the output if you have configured your database correctly.
That’s it. You can open your phpmyadmin and check the tables and their structure. Next step is how do we add data to table using forms. For this, we will first create our layouts using blade. Then we will write a controller to handle the actions of login and register. Also, in laravel, we make use of HTTP Requests. These let us validate the input provided by the users when they submit the form and also lets us define whether the users are authorized to make such requests or not.
Important Note: By default, laravel has namespace set to “App” and mapped to app/ directory. But you can change the namespace of all classes using one simple command. For example, in this tutorial, I have changed namespace for the app using command sudo php artisan app:name Learnlaravel, where the new namespace is “LearnLaravel”.
Now moving on, first create the following blade layouts. In laravel 5, the views have been moved to app/resources/views directory. In that, create the following files. Please follow the directory structure otherwise you will have to make changes in routes as well.
baselayout.blade.php (In app/resources/views/) - This is our parent layout. This uses twitter bootstrap. Learn how to integrate twitter bootstrap with your laravel project in this post. If you just want to try it out, you can use CDN bootstrap and jquery. Just remove Html::style & Html::script instances in <head> section and uncomment the commented part.
login.blade.php (In app/resources/views/users directory) - Contains the login form.
register.blade.php (In app/resources/views/users directory) - Contains the register form.
dashboard.blade.php (In app/resources/views/users directory) - Once users are logged in, they will get redirected to this page which is only accessible to authenticated users. displays the name of the logged in user. The name value corresponding to the authenticated user is stored in database when they register.
That’s it. We have our basic layouts ready. Next, we setup routes.
Open app/Http/routes.php, and in that add the following
Next, we move on to the controller. In laravel 5, you are provided with a default AuthController.php in app/Http/Controllers/Auth/. We will edit it to handle login, register and logout actions that we have defined in routes.php.
That’s it. You have the controller setup to handle login, register and logout actions. Now, the only thing missing is LoginRequest and RegisterRequest classes. We will create them now.
In your projects root, execute
$ php artisan make:request 'Auth\RegisterRequest'
Output : Request created successfully.
$ php artisan make:request 'Auth\LoginRequest'
Output : Request created successfully.
Now it will automatically create the two required classes in App/Http/Requests/Auth/ and will add template methods to them. We just have to edit the methods now. In both, set authorize() to return true. Like,
And in RegisterRequest.php, edit rules() as,
In LoginRequest.php, edit rules() as,
Next, we need to edit app/Http/Middleware/Authenticate.php which handles redirection of user who is not logged in and is trying to access a page that requires authentication. Open that file and edit handle() function,
That’s all. You can now test it. On project root, execute php artisan serve and try to open http://localhost:8000/users/dashboard. You will be redirected to login page. To open register page, visit http://localhost:8000/users/register and submit the form. Check the database to verify. That’s all there is to user authentication in laravel 5.0/5.1.