loading...

Laravel 8 Auth (Registration and Login)

kingsconsult profile image Kingsconsult Updated on ・4 min read

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, 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 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.

Step 2: Database Setup

Open the .env file on your IDE or text editor

.env file
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

Step 3: Migration

php artisan migrate

migration file

Step 4: install Jetstream

composer require laravel/jetstream

Installing Jetstream
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
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

Step 5: Migrate the new table that is created

php artisan migrate

migration Command
Let’s run our app

php artisan serve

Running our app
Holding down the Ctrl button and Clicking the localhost http://127.0.0.1:8000/ will serve our app in our default browser
Localhost
Then click on register by the top-right to register an account,
Registration page
will give us this page, filling your name, email address and password and submitting will take us to the our dashboard
Dashboard
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/ , where we have the options of either to login or register another account,
Home
Let's login back
Dashboard
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
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
then check your mail
Email inbox

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
You can leave a comment, suggestion for improvement or corrections, don’t forget to follow me.

Discussion

pic
Editor guide
Collapse
marcellusgraceman profile image
marcellusGraceman

I have followed the instructions above.. I dont see the login button at the top of the homepage of Laravel application

Collapse
kingsconsult profile image
Kingsconsult Author

were you able to register successfully?

Collapse
marcellusgraceman profile image
marcellusGraceman

no.. I keep getting this error..page not found..even for login page

Thread Thread
kingsconsult profile image
Kingsconsult Author

I need to see your code is to where you are getting it wrong

Thread Thread
marcellusgraceman profile image
marcellusGraceman

can I send you a zip file of the project?

Thread Thread
kingsconsult profile image
Kingsconsult Author

you can push to github and share the link

Thread Thread
marcellusgraceman profile image
kingsconsult profile image
Kingsconsult Author

you might wish to send it privately to my email if the code is confidential

kingsconsult001@gmail.com

or reach me through my WhatsApp
+234 806 605 6233

Thread Thread
kingsconsult profile image
Kingsconsult Author

alternatively, we can use video chat, zoom

Thread Thread
getrackerdan profile image
GETrackerDan

Was there a solution to this issue? Running into the same problem with ' The requested URL was not found on this server. ' with login - register pages.

Thanks

Thread Thread
kingsconsult profile image
Kingsconsult Author

Yes, there was a solution, the error was a typo

Thread Thread
getrackerdan profile image
GETrackerDan

Could you elaborate? As facing the same issue as the OP.

Regards

Thread Thread
kingsconsult profile image
Kingsconsult Author

I mean, he misstyped something

Thread Thread
kingsconsult profile image
Kingsconsult Author

cause of the error might be different, but the error message might be the same

Collapse
codingfix1 profile image
codingfix

Hi. A really nice post, thank you. I have a question: how can I customize profile adding billing and shipping addresses, for example? It's not just about customizing views but even the controllers. I could't find the Jetstream controllers nor a way to customize registration and/or profile as I told so I gave up and came back to the old Auth system which fortunately still works. But I'd like to know if there is a way to do what I said: maybe an idea for a future post? 😏

Collapse
kingsconsult profile image
Kingsconsult Author

Thank you for this, I will try to write about how to customize the Jetstream, follow on dev.to/kingsconsult , so you will not miss it out, but for the time being, the controllers for Jetstream can be found in app/Actions/ ,

Collapse
codingfix1 profile image
codingfix

Thank you so much. I'll follow you for sure

Collapse
zenahr profile image
Zenahr

Thanks for your post. I would love to have actual code snippets instead of pictures of code next time. That would be awesome.

I've followed all steps and ended up with something very different. The compiled Laravel app only comes with a Profile section, no API Tokens section. Also, there is no way for me to upload a profile picture. Could you link a GitHub repo with your code so we can reproduce exactly what you did in your tutorial with the correct dependencies?

Also, please update the guide by either saying explicitly which dependency versions you used or by updating to the current state of affairs. Cheers.

Collapse
kingsconsult profile image
Kingsconsult Author

Thank you Zenahr for your feedback, maybe you miss it, the repository for the code is in the write-up. However, in case you miss it, this is it github.com/Kingsconsult/laravel_8_..., I used Laravel 8.0, livewire 2.0, Jetstream 1.2.
If you still needs clarification, you are free to reach out, thanks

Collapse
anisahmmed profile image
Anis Ahamed

I am using jetstream authentication scaffolding in laravel 8. How to redirect different user to the different pages after login in this?

Collapse
kingsconsult profile image
Kingsconsult Author

if I understand your question, that is, you have different classes of users, you want to direct them to their dashboard on login.

  1. You can write a helper function, use if statement to determine what the function returns
  2. Go to RouteServiceProvider.php in app\Providers\RouteServiceProvider public const HOME = '/dashboard';, change the '/dashboard/' to the helper function
  • grab the user's id using Auth use Illuminate\Support\Facades\Auth; $id = Auth::id();
  • how do you determine the class of the user (different user)
Collapse
anisahmmed profile image
Anis Ahamed

. I have used this way but it's not working. Is there any way to redirect user by using middleware

public function redirectTo()
{
$role = Auth::user()->role_id;
switch ($role) {
case '1':
return redirect(route('dashboard'));
break;
case '2':
return redirect(route('judge_dashboard'));
case '3':
return redirect(route('user_dashboard'));
default:
return '/login';
break;
}
}

Thread Thread
kingsconsult profile image
Kingsconsult Author

Hello, sorry I missed your comment, have you resolved it? you can still reach out to me, if you have not

Collapse
tunjiyr profile image
Tunjiy Ridwan

thanks for this boss

Collapse
kingsconsult profile image
Collapse
thedudetechnologies profile image
Rishit Nimavat

can we used customize view for login register and dashboard..?

Collapse
kingsconsult profile image
Kingsconsult Author

Yes, you can use any custom page for any of them

Collapse
lengyelbalint profile image
lengyelbalint

Hello Kingsconsult! It works perfectly. :)

How can I send verification mail after registration?

Collapse
kingsconsult profile image
Kingsconsult Author

I just wrote an article on that, you can get the full tutorial here
How to send email in Laravel 8 downwards using Gmail

Collapse
pushpak1300 profile image
Pushpak Chhajed

Yes It is Right But laravel team has started support for laravel/ui

Collapse
robertlook profile image
Collapse
richard4s profile image
Richard Oluwo

Definitely saved my day

Collapse
kingsconsult profile image
Kingsconsult Author

Wow, glad to hear that
😄

Collapse
surendra1993 profile image
SURENDRA BHARATHI

Nice Tutorial. I am trying to authenticate admin and users with the same login page and redirect to the admin or user landing page. How do I achieve this using Laravel 8 jetstream authentication?

Collapse
gausoft9 profile image
Gauthier

Thanks for this post. How to customize the authentication views? I'am wondering how to override the laravel 8 auth system

Collapse
kingsconsult profile image
Kingsconsult Author

Thank you, it just depends on how much customization you need, you can choose to use inertia scaffolding over the livewire, then define your own view, but you can also customize the livewire. A view directory is created in your app directory, you can customize them, also in your resources directory, navigation-dropdown blade file is created, and layouts directory, all those files are what made up the views, you can customize them to your likings.

Collapse
lyphuthao profile image
LyPhuThao

Thank you so much bro!

Collapse
kingsconsult profile image
Collapse
hoshmandaqil profile image
Aqil Hoshmand

Hi dear, many thanks for you nice teach...
when I register a user and log out that , I can't login again , why?

Collapse
kingsconsult profile image
Kingsconsult Author

Thank you for the encouragement.
Check how you store (register) the password and also how you want to retrieve (login) the password, did you hash it the same way?
That might be a cause

Collapse
bosmanda profile image
bosmanda

Hi, Great post. It helped a lot.

My problem is the following:
Doing exactly what you said, my project is working perfectly on 127.0.0.1:8000, but as soon as I change to my apache server, it loads the home page, but my login and registration routes don't work. Have you encountered anything like this before? Maybe a hint or 2? I cant load this to go live on "php artisan serve". It needs to run on a server. Everything else seems so work ok.

I am working on ubuntu 20.04 on a standard lamp instalation with php -v 7.4.3, apache/2.4.41 server.
Regards,
Dirk