< go back to home

Laravel Authentication Series (Part 1): Using The Default Login Setup

August 21, 2017

This is a series of related articles regarding different Laravel authentication methods. For the part one of this series, we'll tackle the most straightforward way by using Laravel's default setup. Laravel has an excellent documentation for authentication which I recommend you to read as it is detailed. Here we are focusing on the actual step-by-step implementation that you can follow.

For the requirements, I recommend to fist setup Homestead which I did a write up here. This allows us to avoid environment based issues by having a simillar baseline. I've written this article using Laravel 5.5 but it should work on other versions that is in 5.*. With that, let's start!

Welcome page for a fresh Laravel install.

Laravel provides an easy way of setting up a login page. Run the following command to see what I mean:

This command scaffolds controllers and views for authentication. It generates HomeController and views for authentication including the home view that is accessible by authenticated users only. For login related controllers, you may explore it in the compoer package "Illuminate\Foundation\Auth\AuthenticatesUsers.php".

You may now actually go to "/login" page but attempting to login will result in error related to database users table. Let's fix that. The migration file for users table is already setup by default, so let's just create the seeder.

Edit the seeder file generated in "database/seeds/UsersTableSeeder.php" to be the same as following:

We are now ready to run the migration commands.

Login now using 'john.smith@gmail.com' as email and 'secret' as password. You will be redirected to home page upon successful login.

generated login page
home page after successful login

We now have a website with login capabilities. :) Creating a page that is protected by authentication is also easy. You just need to add the prebuilt Middleware "auth". Let us show by creating a simple page. Edit the route in "routes/web.php" and add the following.

As you can see, you just have to chain the "middleware" function containing "auth" in routes that you need to protect with authentication. There are other ways of doing this which you can check in Laravel's documentation for authentication. Now, check the "/protected-route" url as a login user and as a guest and you'll verify that the authentication is working.

That's it for using of Laravel's authentication setup. For the part two, we'll show how to implement a customize email and password authentication.




< go back to home