Laravel 8 Auth (Registration and Login)
Tags: beginners, webdev, php, laravel
Published: 2020-09-17 06:18:40

<script data-ad-client="ca-pub-0111568483904129" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
Hello, how are you doing? Today, I am going to teach us how to do user authentication in Laravel 8. As you all know, the Laravel team released the latest version last week which is Laravel 8, and as expected, there is some difference between the previous Laravel 7 and this present version. Some of the difference is highlighted in my previous post on how to create a CRUD application with, [Laravel 8](https://dev.to/kingsconsult/laravel-8-crud-bi9), it is a step by step guide in creating a CRUD Project Management app.
Previously, in Laravel 7 and Laravel 6 in other to do user authentication, we use an artisan command  **composer require laravel/ui** while from Laravel 5.9 downwards uses **php artisan make:auth**

In Laravel 8, there is a major change in that area in the sense that many things are introduced and a lot of configurations have been done to get you started and not minding the boilerplate of your application, one of those changes is the introduction of Jetstream, Laravel Jetstream is a beautifully designed application scaffolding for Laravel. A major shift from the legacy authentication UI of Laravel. 

Click on my [profile](https://dev.to/kingsconsult) to follow me to get more updates.

Without saying much, let’s dive into the new features of Laravel 8. 
### **Step 1: Install a new Laravel app**
> composer create-project laravel/laravel projectapp --prefer-dist

The command above only install Laravel, however, if you want to install Jetstream together then either
> Laravel new projectapp --jet

or
> composer require laravel/jetstream

we are going with the first of only installing Laravel, I want to explain other things in the process.

I highlighted some changes noticed in the installation of Laravel 8 from the previous Laravel versions, you can check them out in my previous post [Laravel 8 CRUD](https://dev.to/kingsconsult/laravel-8-crud-bi9). 
### **Step 2: Database Setup**
> Open the .env file on your IDE or text editor

![.env file](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/1_k2jrn6.png)
Change the DB_DATABASE to the name of your database and if you have set a Username and password for your phpmyadmin, specify it, otherwise, leave the username as root and password blank.
Before we migrate, let’s catch one bug before it throws an error, go to **App/Providers/AppServiceProvider.php**
and add
> Schema::defaultstringLength(191);

to the boot function, also add
> use Illuminate\Support\Facades\Schema;

to the top
![AppServiceProvider file](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/2_nqmoix.png)
### **Step 3: Migration**

> php artisan migrate

![migration file](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/3_p6mom9.png)
### **Step 4: install Jetstream**
> composer require laravel/jetstream

![Installing Jetstream](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/4_xv2wmx.png)
Laravel advises that Jetstream and its stacks (livewire or inertia) should be done on a fresh application because it will install a layout view, registration, and login views, as well as routes for all authentication end-points. A dashboard route will also be generated for post-login requests. So an application that has some of those, might throw some conflicts.
### **Step 5: Install livewire or inertia**
We need to install one of the stacks, either a livewire or an inertia stack, in this tutorial, I will only be using livewire because it set up everything I need for the app
> php artisan jetstream:install livewire

![Installing Livewire](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/5_pbsx3t.png)
As suggested, **run npm install && npm run dev** to build all the javaScript files and CSS we need for our app. On successful build, Laravel will send a notification at the bottom left. 
![Laravel Notification](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/6_poqd5y.png)
### **Step 5: Migrate the new table that is created**
> php artisan migrate

![migration Command](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/7_qw9uge.png)
Let’s run our app
> php artisan serve

![Running our app](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/8_ozpw6e.png)
Holding down the Ctrl button and Clicking the localhost [http://127.0.0.1:8000/](http://127.0.0.1:8000/) will serve our app in our default browser
![Localhost](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/9_exlf99.png)
Then click on register by the top-right to register an account, 
![Registration page](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/10_rqgzrc.png)
will give us this page, filling your name, email address and password and submitting will take us to the our dashboard
![Dashboard](https://res.cloudinary.com/kingsconsult/image/upload/v1600318472/11_htzvj5.png)
Clicking on the image by the top-right, pulls down a tray that contains our profile, link to logout, and API tokens, 
Clicking on the logout, will log you out of the app and return the page to the localhost [http://127.0.0.1:8000/](http://127.0.0.1:8000/) , where we have the options of either to login or register another account,
![Home](https://res.cloudinary.com/kingsconsult/image/upload/v1600318472/12_cujuko.png)
Let's login back
![Dashboard](https://res.cloudinary.com/kingsconsult/image/upload/v1600318472/13_osdwbg.png)
Login will take us to the dashboard, pulling down the image at the top-right and clicking on Profile, will give us options to do the following
1. Add a profile picture (max: 1024KB)
2. Edit our name
3. Edit our email address
4. Change our password
5. Enable two-factor Authentication
6. Browse sessions
7. Delete account
With this, we have our authentication setup, A user can receive email notification in the case where the user forgets his/her password, an email to reset the password within 60 minutes of request, 

But before requesting, lets setup our mail comfig
![mail settings in .env](https://res.cloudinary.com/kingsconsult/image/upload/v1600318472/15_ed1xxx.png)
Check my mail settings in line 26 to 33, also checkout line 1 for my app name, then click on email password reset link
![Reset email](https://res.cloudinary.com/kingsconsult/image/upload/v1600318472/14_gzkth3.png)
then check your mail
![Email inbox](https://res.cloudinary.com/kingsconsult/image/upload/v1600318472/16_rxpi8t.png)

Finally, we are done, hope this explains it very well, in case you miss anything or you did not follow all through, here is the code on my [Github](https://github.com/Kingsconsult/laravel_8_auth) 
You can leave a comment, suggestion for improvement or corrections, don’t forget to follow me.