Auth in Laravel 8: Fortify and Laravel UI (without Jetstream)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in recent years i've done a lot of demo projects and i used this design with laravel auth default as a starting base so it's a simple bootstrap theme you just launched laravel new dash dash auth and you continue with it another example was my youtube video for vue.js example but same here i've created a new laravel project that gives a fresh design doesn't matter if i use auth or not and inside of it i code whatever i want and for the last year i've been using this command to do that so dash dash auth generated everything i needed in new laravel 8 however it was replaced so there's no dash dash auth anymore instead they recommend to use a new laravel jet stream for scaffolding of login registration and all auth the problem with laravel jet stream however if we take a look at the official documentation of jet stream you need to choose the new stack new tech stack between livewire or inertia.js and if you don't work with those technologies then it's additional problem for you so in this video i will talk how to generate that off the older design version without jet stream so without learning livewire or inertia.js and we will talk about two ways the first is laravel ui package which is kind of deprecated but still working or you can use a package called laravel fortify which is actually a part of jetstream but can be used separately and customized for whatever auth design you want so let's dive in the first way to generate to scaffold that laravel off is use the same laravel ui package which is actually what happens under the hood when you use laravel new dash dash auth it used the package laravel ui which was also a standard and it gives you some artisan commands to generate that so you generate artisan ui bootstrap for example with dash dash auth the same flag and it worked all the time until laravel 8 where it became kind of deprecated it's not officially deprecated but it should no longer be used that's how it stated and again it is advised to use laravel jet stream luckily laravel team and dries vince listened to the crowd who were complaining about that deprecation and they released a new laravel ui version on september 11th version 3 which was still supported by laravel 8. and let's try to do that if we do laravel new project okay it is installed then we go cd project and do composer require laravel ui it will require version three automatically the new version three which is supported by laravel 8 and then we can use the same command let's try to generate bootstrap off like this generated and we run npm install and npm run there okay successfully done and now if we launch that project test which i have prepared locally as a subdomain we see default laravel 8 homepage which is different from laravel 7 and on the top right you see login and register which lead to the familiar auth bootstrap design so login and register everything will work so that is one way of doing that with laravel ui but this notice tells me that it's better to prepare for the future because laravel ui may not be supported in laravel 9 and going forward and i would like to use something more official but still without gesturing apparently there is a way with laravel fortify and laravel fortify is a new package that was introduced as a part of laravel 8 and it was positioned as a part of laravel jet stream and at first i even misunderstood the purpose of it i thought it was authentication engine 4 jet stream and if you go to jet stream documentation you see fortify as a part of it but two days ago on reddit taylor launched the official documentation for fortify as a standalone package so he filled the readme of 45 with a lot of useful information customization and how to use fortify without gesturing and we will try level 45 and generate the same bootstrap auth but first i want to tackle the philosophical part why fortify now so it's still a change and at first i was really skeptical about it so why people have to relearn but it does make sense if you think about it laravel ui as a package was actually serving two purposes generating the ui and generating the auth so it was really hard to separate those to what if you want to use just auth so just the controllers for example or if you just want to generate the ui the bootstrap or the react or view ui without auth so it was kind of two in one package with the weird mix of designs and it was working but fortify is a package that tries to solve that problem so fortify is only for auth only for back and off and you can replace the front end with whatever you want and even in the documentation it is stated fortify is front-end agnostic authentication back-end so it's kind of if you would do in the old days php artisan make off but without front-end and front-end can be used as laravel jet stream with live wire or inertia.js or you can easily create your own front end or we use the same front end from laravel ui which is exactly what we will do now let's try so new laravel project again and let's install fortify instead of laravel ui we compose require fortify and we will follow the official documentation for it okay installed now we publish the resources as you can see config fortify is generated then app actions will be an important folder and also there's app provider for fortify and database migrations so this is the next thing that we need to do artisan migrate okay migration is successful the error above is just i had forgotten to put in my credentials for database and as you can see a new thing from laravel 45 is two-factor columns to users table so in the database in user stable you see columns two factor secret and two factor recovery codes which is another benefit of using fortify it comes by default with two-factor authentication and now let's take a look at the config of fortify there are a few features which are useful like what username to use what home to redirect to rate limiting but the main thing and the main benefit of fortify is disabling or enabling features so earlier in laravel auth to enable or disable for example email verification or resetting the password or other features it was a pretty weird big dance of regenerating something editing something in a few files front end and back end and fortify has a convenient config for enabling disabling features so by default it enables two-factor authentication but if you want to disable it just comment it out if you want to enable email verification just do this one and remember it enables and disables it only on the backend you still need to build the front-end views for that and this is exactly what we will do now and by default minimum amount of views that we need to create is login view register view and password reset view which is actually two views password request and then password reset form so four views in total and if you go to laravel 45 official documentation there is a method how you define any view and you can copy it from here and paste into fortify servers provider let's open fortify service provider here which was generated by the publishing of vendor publish in fortify and by the way don't forget to register that service provider after publishing i've made that mistake and then was debugging for a long time this line is important you should ensure the file is registered within the providers in app configuration file so if you go to config app in here you need to go 45 service provider class that's it probably like this and there are some actions already defined but we ignore them and in the boot we just paste this so we define that auth login however we want and we will use bootstrap for that in a minute but we tell fortify use this view for login and similar if you go to the documentation deeper same with register view so copy register view here then copy and paste password reset link view like this and resetting the password so four views in total copy and paste so we told fortify to use these views now where do we get them we can get them from the same laravel ui so it would be kind of a trick you get laravel ui on a separate project in a separate folder you generate all the blades and all the css and js and then copy into your main project and this is what i did i opened a separate project i will open it in sublime text and after all the npm run and everything i have resources views auth folder with everything we need and also layouts app and i will just copy and paste both into our main folder copy and paste on the top and now we have those files inside of our resources views and also we need to copy public js and css because if you go to layouts app they are required as assets so i will copy those as well so public css and js copy and paste into our main folder and now let's relaunch the login page refresh and we see our familiar login blade a few things we need to tweak here because some of the view files are not exactly the same so odd forgot password and auth reset password should be password confirm and password reset so auth pass words email and passwords reset so now we can click forgot your password and see the familiar view and also if we click register i will use fake filler to fill in the form we click register and we successfully register the user and the final problem is that we don't have a home link it exists as a view like this home blade but we haven't added that in route so let's do that as a final thing route view home homeblade refresh and we have our dashboard home and we can click log out and it will all work fine and don't forget to protect your routes because if we go slash home now i'm still able to see that page so we need to protect that by doing middleware off and then it will automatically redirect back to login important notice keep in mind it's all really fresh and it may still change it changes almost daily with additional documentation and explanation how to use things and fortify is still in its early days so if you are watching this video in the future with laravel 9 came up i'm not sure what happened then so read the latest official docs an example of this one thanks to philip on twitter who provided me the link actually published by taylor yesterday just 10 hours ago an overview of the whole authentication system with changes of laravel 8 because it became a pretty confusing thing so there was laravel auth but then there was laravel passport then there was laravel sanctum for api and now scaffolding jet stream fortify it's a mess well not exactly a mess but not clear to newcomers so ecosystem overview is a really well written explanation of the whole ecosystem what to use and when to use them and with that i like the fact that laravel stays as a back-end framework my overall disagreement with laravel 8 changes for the auth and with jestrum and all of that was that it's kind of pushing us to be full stack developers so to learn live wired inertia.js tailwind css and all of that but in fact fortify is the package that actually breaks that chain and allows laravel auth to be only backend and then for api you have passport or sanctum which is also only backend so laravel stays as a back-end framework with some front-end if you wish to use that in jet stream with tailwind live wire inertia and it is opinionated but that's what taylor suggests himself so he stands by that decision and he expressed that in a reddit post i really really recommend you to read that especially if you are not a fan of laravel 8 changes this is the post why he did it or the team did it and why he thinks that the new stock with live wire and inertia is a great way so in general taylor and the team did a great job of reacting to reactions from the community they explained why they released some patches to laravel ui and then they release documentation to fortify and there will be surely more things that they will release especially it will be more clear when tutorials appear demo packages demo projects and articles about customization of jet stream and fortify and by the way it already exists so if you go to blog laravel.com there is an article already just stream customization with a few tips from taylor himself so just watch on twitter or elsewhere what will be released around that topic and i will be probably contributing to some of that so subscribe to the channel if you want to get more news about laravel 8 maybe i will review jet stream with tailwind and livewear inertia we'll see but for now in this video i've showed you how to use still old auth as we used before laravel 8 but with laravel 8 and 45 i hope it was helpful and see you guys in other videos
Info
Channel: Laravel Daily
Views: 119,132
Rating: undefined out of 5
Keywords:
Id: NuGBzmHlINQ
Channel Id: undefined
Length: 14min 4sec (844 seconds)
Published: Fri Sep 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.