Multiple Authentication in Laravel 5.4 Natively! (Admins + Users) - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everybody and welcome back to the deaf marketer channel today's video is going to be a fun and exciting one we're going to be building a multi authentication system in level 5.4 and I think this will actually work in level 5.3 actually yes it will it's going to be backward compatible with 5.3 nothing really changed between as far as authentication is concerned between the two versions so it'll work in both versions so if you're still on an older version it's going to work most likely you're going to be using a future version and we're going to be talking about how we can build multi authentication into it all right now this is something you guys have been asking for a ton I've getting lots of requests for this and so I'm really excited to finally bring it to you today to be 100% truthful the reason I haven't done it yet is because I honestly didn't really know how to do it it's something that I I've always used a package in the past but I'm getting so sick of tired of using packages that I'm just I decided to do with the level way I'm going to see I wanted to figure out how we actually use label to do it be so that we don't have all the bloat of a package but then the other problem is like every time level updates then we gotta wait for the package to update as well and then you're constantly half these packages there are so many authentication packages that get abandoned you know we used to use in trust while in trust the entrust plugin got abandoned is like 5.1 I think or maybe 5.2 there was a package called multi offs that worked in 5.0 and I think it got operated to 5.1 and then like abandoned there was there's actually a couple packages out there called multi ops there is one currently that people use it's only available in 5.2 it doesn't work in 5.3 there's lera trust which tulare trust a you know credit they're actually maintaining it quite well I think it is actually it is compatible 5.4 but again you're always running into these problems of compatibility and so I wanted kind of a simple clean way I didn't need a huge package I want to import a whole package if I just want a second type of user I wanted a way to do that without having to bring in a package and learn a package and then have the facade that the package has and stuff like that I wanted to use the label facades I wanted to use it as clean and natural to level as possible and that's what I'm going to show you guys to do today okay so this is going to be use the default authentication facade you are authentication system in layer but we're going to extend what you do when you write PHP artisan make off we're extending that whole system we're going to have two different tables one for admins one for users this will work with multiple users though like if you wanted three or four or five different types of users you could do that that would be a pretty edge case scenario what I should mention before we get going is don't get confused between types of users and roles of users okay so if you have a user let's call it a customer right and well let's just call it an admin so we got an admin user well you might have a super admin that has the ability to do everything create new admins you know change every setting in the book you know issue refunds or something like that below then you might have like I don't know like an editor right and the editor can view everybody's blog posts and you know manage all of the media and stuff like that but they can't create new users and they can't add plugins or something and then below then you might have an author who can write their own blog posts but can't you know see everyone else's blog posts or something like that those are user roles okay they're all admins they're just different user roles they have different authority levels that is what we call user roles that's different than what I'm talking about today we're talking about actually building two different systems you would not want to build a table for your super admins another table for your you know editors and then another table for your authors you wouldn't want to do that you would want those all in one table and then just give them different authority levels the use case for what I'm showing you today is if you say have a ecommerce website you have admins that manage the website they're going to create users they can create products they can manage orders as they come in issue refunds right those are all the employees of the company or the admins on the other side you're going to have customers and customers are going to have their own order history they can view their order history they going to have like a wish list they can manage and stuff like that and it's on a completely different backends this like you know interface they've got their own interface and basically looks like a front-end interface and that's how they manage their account basically whereas your admins have like a whole different interface completely different functionality maybe the information you're storing in the tables for the admin is completely different information than what you would store in the customer table okay so this that's kind of what we're doing today is for those two different types of users completely and of course built-in tool arrabal last thing before we get started I want to mention this is not going to be a beginner's tutorial you need to have at least I'm going to explain it really well but you need to at least have a basic understanding of how level authentication works beyond just typing in PHP artisan make off and then clicking enter and then just running my great and you know it magically works at least understand the way middleware works the the different levels of middleware that there are understand how we can use things like traits and you know class inheritance and stuff like that you need to at least understand that to do this this isn't going to be a completely beginner tutorial I have a lot of other tutorials if you are looking for complete beginners you can check out my channel and I've got a lot of other stuff for complete beginners but this tutorial is not for complete beginners ok also the link for this for github is going to be down below because that's always requested everyone always wants to know so in the description make sure to follow up in the description if I have any updates for this video if anything changes with the codebase so there's a newer version of this video there will always be a link to that in the description so check the link in the description if you feel like this video is outdated I'll try to keep it up to date and I'll link to the new updated videos in the description because I can't change the video but I can change the description also I'll have a link for the github in the description on where you can follow follow along or get the code or whatever all right so let's go ahead and it started on this I've gone ahead and created a brand new fresh project okay this is 100% fresh project called multi off is what I named it and this is just your default thing I committed it to get just so we can see all the new files as we make them and stuff but otherwise it's a fresh install there's nothing new here just a new label application okay so what I want to do is show you guys how from a new application you would do it I want to make sure there's nothing else like contaminating our project or anything that might confuse you guys or something that you don't expect so the first thing I want to do is we're going to go through and actually just generate a the PHP artisan make off command this is going to create the authentication scaffolding for the user and then we'll have the user will basically be done we could go ahead and start creating users and everything right on the box you guys know that right what you want is another you don't want to create a different type of user and then start working with that so let's start by just doing the scaffolding and then we'll move on and make the second type of user now in this tutorial I'm going to call it at admins and that's going to be basically our employee type and then users is going to be for like our customers our front end sort of customer type of thing okay so let's just go over to the terminal here and inside of our multi off folder we're going to run our command PHP artisan make off make off like that generator our scaffolding correctly nice you can see now we got some new files so it creates automatically the home controller with the middleware and then we're going to learn a little bit more about this as we go on and then it creates a couple views for us and then the route to add you know this routing okay pretty basic stuff of course none of its complete until you run these migrations but I'm not going to run the migrations just yet because we this only creates a user's table right well we also need to create another table to store our new type of user which is going to be admins so let's go ahead let's create a migration for that and then we'll run the migrations at that point so back to our terminal let's do PHP artisan make migration and we're going to call this create admins table and I'm going to use a flag you don't need to but it make a lot easier I'm going to use a new tag called create and then just set it equal to the table name that I'm going to create so I'm going to create a table called admins it creates a table called admins or it sets that up in the migration so it makes our job a little bit easier it kind of gets the migration going for us I'm going to go and click enter you can see it creates the new migration for us and now that should be available in our migrations folder if we open it up you can see what that create flag does it sets up our schema create creates a table called admins drops the table called Advan sets up our increments in our time stamps cool okay so now look we can put whatever information we want to store for our admins we could obviously put in here run the migration and get that in the table so you can do whatever you want here what I'm going to do for the tutorial is just copy what's in the users table so I'm going to come over to the user migration here we already have the increments and the timestamp so I'm going to copy what's in the middle right here so just copy this come down here and we're going to paste it like that alright so it springs in a name field email password you're going to want to make sure you have at least one field that's unique this your unit a unique identifier and this you doesn't have to be email if you want this to be username or employee ID or whatever you can customize that just keep in mind what you name the column and as we go throughout the tutorial I'll tell you guys basically where you want to change things so I'm going to be using email as our unique identifier but if you wanted to use something else I'll tell you guys where to change it as necessary okay but it's not very many places it's pretty easy just make sure whatever your unique identifier is that it's you know there's a unique index for it that's everything you need and of course you can add more information that you need so if you were storing for example like string the job title call it title what we call job title I don't know let's say that was important for you for your admins they wanted you need to destroy the job title just to make it a little different than users I guess we'll store that in here and that's all you need to do for your migrations and then we can run the migration so let's just run them real quick PHP artisan migrate what do I do wrong unknown database won't be oh yeah yeah you got to have a database so I forgot to do that let's make sure you have a database so come in here click connect make a database I'm going to call it multi ah okay now we want to go into our env file and just edit our env files so let's give our database name multi off and then let's the password is going to be root for my computer and then just an empty password there okay so that's all we need to do for our configuration okay now we should be able to run this so let's go through and run it again there we go and it ran all of our migrations so now in our table I closed it of course let's run it look at it again now we got our admin table here we have our job title and then we've got our users table here slightly different but again the important thing is to have a unique identifier a password field and then you want this remember token and I should have mentioned also I know if you guys notice this but there is this field called remember token and this is just a default that level has that we'll set that up appropriately so just make sure you have this remember token filled in here and then level will just handle it automatically you don't need to worry about it okay so now that we got this note that we added a new job title column in here right so now we're going to do is we're going to go through and make our model okay so we need we already have a model in our app for users okay and this is our normal customer and now we need one to manage our admins so what I recommend doing is instead of using the artisan commands to generate a model what that's going to do is just create an empty model and it's not what you really need you what you need is this authentic a table model all right so what I recommend doing is just taking your user model and then we're going to duplicate this file and I'm going to call it admin and then we're going to use this because it's all the same stuff we're just going to use this as our admin model so make sure you change the name to admin everything else should be the same notice up here we import notifiable and then we'd say use notifiable what this means is if you've ever used the notifications system in label where you can basically just define a notification like a slack or a HipChat notification or a text message or whatever just that notifications quick hand the way it works you just say hey notifications and then you you know set give it the notification and it automatically determines the user based on the user object it determines who to send it to and how to send it well that's because you're normally using users and users you know inherits all this notifiable trait so if you want that ability then you need to have that as well so now both your admins and your users can take advantage of notifications natively and this is some of the advantage of using the native authentication system instead of a plug-in is we get access to all of this and if things like notifications gets updated the notification system or anything like that you automatically get all that functionality it's not broken when you upgrade okay that's why I love you in the built-in functionality instead of trying to use some sort of package okay so there we go we've got that so notifiable means then down here you'll see it uses foundation off user imports it as authentic a table and that's what we're extending this is our base model for authentic a table users okay if you actually followed this and looked at the source code what you'll find is that it actually it's it itself extends the model so you're getting all of your model you know normally you see this extends model well that already happens behind the scenes with this so this basically builds on top of the model okay so you get all the authentication stuff and the model together and that's what you get there okay now down here you've got your pure syllable array and your hidden array so because we're in admin now we had that new item job title you're going to want to add job title here just so that it you can max assign that so all everything here in this syllable array is mask assignable so obviously if you have something that you don't want mask assignable like like authority level if you had an authority level field you obviously wouldn't want that to be masked assignable so don't put it in there okay and then if you need anything hidden from the collection of the array you just put it in here so it doesn't get thrown in with all the other stuff you can just keep adding new items here whatever is needed for your user and that's it so now we've got a table to store add we've got a model that manages our admins now what we need to do is just set up our guard and we're done basically so let's do it so to configure our authentication settings all of that is handled inside of a config file called config off obviously ok inside of here if you've never been in here I'll just kind of go through everything really quick you can see we have a default for our authentication and what this means is this is something that a lot of people don't understand but layer both has multiple ways to log people in so we call each of these guards so by default you have a user's guard which is actually over the web guard which is for your users and that's kind of like defined for everyone doing like HTTP you know on the actually looking at a computer type of authentication and then the other type of authentication you have is an API guard ok and you can define all these guards you can see the default one here is web and you see what all the guards are down here so here's the web guard and here's the API guard ok so you can define as many of these as you want and that's what we're going to be doing today is adding more guards to and tying it to the model we just created so we can take advantage of everything natively ok and that's what's awesome a labels you actually can add as many of these guards as you need now by default if you don't specify a guard then you need to set a default that it uses if you don't specify it so what I mean by this is in your application you can do commands like auth check and then you can you know check if somebody you know you put in their credentials here an array of all their credentials like their you need put their email in their password or whatever and then this will come back to or false if they're authenticated well you didn't specify the guard so what it's going to do is it's going to use the default here the web guard ok and the web guard is defined here it uses your users provider I'll explain that here in a second we also have attempt to try to log people in you can do once to log of someone in for one HTTP request there's different commands with this off that you can do that are all native but if you don't specify a guard it's by default uses whatever you specify here so if you wanted the admins to be either default then you would change it up here the default all right but it really doesn't matter I usually just keep it as users and then my admins are employees you can always specify because what we're going to do here after we set this up you'll be able to specify this so you can do guard admin or admin so now you're saying you're authenticating against the admins instead of the users does that make sense and then you can say one so you can do attempt or you can do whatever check like that okay so this gives you all that functionality with the authentication facade so that's what we're defining up here is whatever the default is going to be if no one gets if no guard gets specified then it's going to by default use the web guard and the web guard is defined down here is going to be tracked using session which means there's two different ways to track basically session and token so tokens best for api's session is for web interface is usually session uses cookies and storage and then token uses obviously an API token alright that you'll have to pass every time you want to do something that's pretty common when you're doing API requests you normally pass in a token would be one of the parameters that you pass in in the headers or something so you're going to use that token to authenticate somebody but when you're on the internet you want to make it as easy as possible when I say the Internet when you're you're on through the browser basically you want to make it as easy as possible so we use things like cookies and storage to track users between each request and that's what the session means okay so that's what driver means so here's in the web guard you're saying use the session driver with the API guard we use the token driver and then you can see the provider for each of these is users and users so these are both defining the user's provider well what is a provider okay well the providers are defined down here so you can see we only have one provider and it's a user's provider and what the provider does is it tells it how to talk basically with back and forth with the database so there's two different drivers we can use to talk back and forth to the database there's eloquent you're familiar with that that's the ORM and then that's the easiest way I recommend and then it does an example down here we do young comment this there's also a database driver so if you just want to use database we'll just use the query builder and that's all behind the scenes so technically for the most part unless you're building custom queries with this uh with this guard you usually don't need to worry about it but that's what's happening behind the scenes is it's either using the query builder or is using eloquent all right now we don't need this so let's go and comment that out notice that if you use the database driver then you have the second parameter you need to note is the table whereas if you use the eloquent driver you note the model got it okay so we're going to be using eloquent for our second one we're going to configure a new provider for admins so that it knows how to talk to the admins database all right so we're going to set that up here we're going to set admins we're going to use the eloquent driver and then for our model we're going to use app admin and then admin class right this ties in with this app admin class up here so if you namespace your whole app just make sure you change that and then whatever you name your your model make sure you change that here got it pretty easy so now we have access to two providers so you could change the web to be admins by default or you could just create another guard which is what we're going to do let's just go in and copy this web one paste and we're going to call this admin and now we have an admin guard and we're going to use the session driver because this is for the browser and then the provider links to one of the providers down here we're going to use admins got it so there we go so now we have three of these and if you wanted a if you wanted to set up a guard for the admin API like you wanted the admins to be able to go in go in with an API as well then you could just create another one I would just do this effect we'll just do it there's no harm in it we're kind of prepared we'll just do admin API and then we'll just set this to token and then this to admins okay so now we have access we have a tokenized way to authenticate and then we have a browser-based way to authenticate like that cool so that's everything we need for guards and actually with this you're basically done there's one last thing we need do actually before we go to there let's also talk about passwords you notice when I set up the migrations we didn't create a migration for password resets and that's obviously because we already have a password reset table right well with the password resets table we actually can share it across multiple guards or multiple models and so we don't need to have a different password reset table for every single every single user model in here we can actually just share the exact same one and so the way you do that is you configure all of the different types of password resets you can do down here so for an example we'll just do admins and then we'll say the provider is admins so that ties in up here and then the table is password resets and then the expiration is 60 minutes or an hour if you wanted admins to be only 15 minutes you can define that here and now admins are basically a little more secure all right so just to recap remember you can configure your default here in this bit case default is users and this also says the default passwords if you're doing a password reset and you don't specify which password reset provider to use then it will by default use the users it will assume users but we can also define different types of passive resets and we'll do that in the delayed video ok so these are your defaults these are your guards you can do as many guards as you want you can tie them to as many providers or drivers as you want and then down here is where you set up your providers and providers basically Thai it's basically a shortcut where you can define the driver and model or you know table to use for this so we're saying to use the admin model the last thing we need to do though to make this work since we're pointing to the admin model we need to make sure that the admin model can is expecting or knows which guard to work back with and so we need to configure that guard in the admin model let's go over there and do that we go to admin dot PHP and up here at the top or wherever just set up another protected attribute and we're going to call it guard and then we're going to set it equal to admin all right or yeah admin make sure it defines this guard right here okay so let's go ahead and click that click enter and we've gone ahead and saved it so now we're actually good to go everything is going to work as intended now we actually have access now to multiple different types of users so let's go ahead and try it out really quick before we you know finish the video what we're going to do is let's go take a look at our routes file let's create a route that we can oops and we're going to add middleware to this route and allow only admins to access it so I'm going to make this admin we're going to put it into let's do home can well no home controller is let's create another controller called admin controller and then let's see that's basically it let's go back to our controllers here let's just duplicate this home controller call it admin controller and then let's just define it up here like that admin controller see everything looks good you'll notice that this controller is tied to this middleware so it's the authentication middleware we'll show that here in a second and let's just tie it to another view we're going to make a viewer quick so we'll go to resources views let's just make a new view in here new file actually forget the new file let's just duplicate this home file and we're going to type we're going to call this admin blade PHP and then let's just define a few things here so we're going to say admin dashboard and you are logged in as admin ok so this is our admin dashboard and then for our normal dashboard is we're going to say you're logged in as strong users okay so now we have we can kind of tell what which view were looking at when we're on the front page okay so just to recap we basically created a route called Flash admin and then we're going to tie this now eventually we're going to tie this here to the middleware so that only admins have access to this page and only users have access to that page okay that's the goal now if we go there right now what you'll see is let's just refresh here let's register a new user so we're going to say Alex Curtis Alex addict and example.com okay and register okay so now we're in the user dashboard we're logged in as a user right so let's try to go to home admin alright well now we have access to the admin dashboard as a user so not exactly the functionality I promised right well that's just because if we go to our admins close some of these if we go to our admin controller now you can see that this is tied by the authentication middleware what did I say about the authentication middleware remember when we were looking at the defaults if you don't specify a guard then it's going to default to whatever your default is in our case the default is just web which web is really just users okay so this is by default allowing any users to come in so what we need to do is with our middleware we need to define the guard that we want to protect against in this case we want to do the admin guard so we're going to do is we're going to do : and whatever we do after the colon is going to be our guard so in this case we say off admin and that is going to sale the use the admin guard but not the user's guard or any of the other guards that we have okay so it only allows people with the admin guard in got it so now let's go back to our project let's refresh we're still technically logged in because this is the same session so if i refresh you can see it redirects us to the user dashboard and if I try to type admin now every time I type the admin it redirects us to the user dashboard okay because it's what's actually happening behind the scenes is it's going to the admin this off admin they're not logged in so it's logging them it's actually redirecting them to the flash login page and then the /login page is only forgets and because they're logged in it's been redirecting them to a home page that's actually what's happening so we're going to talk about how to customize those redirects in the next video but well we're going to do it in the video after that probably the middleware video so the next video if you guys want to learn how to customize logins I'm going to go over that in the next video that does require a little bit of custom functionality we're going to basically do a custom authentication login type thing and I'm going to show you guys how to do that in the next video if you guys want to walk over there and watch that the video after that's going to go over customizing middleware a little bit so that we can set we know where it's redirecting to and different things like that to really get it the functionality that I would normally expect with this but you can see the out of the box we have that ability to we you know we have if you currently have users and your you can login a user then you you have access is protecting them against those different types of guards so now if we try to go to at home because we're logged out it won't let us write it tries to log us in the only problem we have right now is that the login page only is only tied to the users so you need to create another login page and then have it login to the admin so that your admins can log-in and that's what we'll be doing in the next video if you want to go watch that but that's basically it so if you can probably if you experienced none with label you probably don't need to watch that video you can probably connect the dots and just figure it out yourself but if you do want to learn how to do that I've got that available in the video coming up next okay hopefully you guys found that informative I know this is something that a lot of people have been looking for and I wanted to show how it works okay so this is definitely how you can tie this together and I know this is only the beginning and because the other parts you know all add to the length of the video so this is a good 30 minute video I'm going to end this here and then if you want to skip to the if you want to watch the other two videos or I guess there'll be three more videos then the fourth video is going to be about forget forgot my password functionality so I'll talk about how to customize that for multiple guard the next video will be the different logins the video after that will be the different middlewares and then the video after that will be the different forget my passwords and that will basically sum up everything you need to do for multiple authentication if you found this video informative please please please subscribe if you're not subscribed already I really really appreciate it you can see more videos like this I do tons of videos on label on VJ s and stuff like that so if you found it interesting and informative please please make sure you subscribe give it a thumbs up as well if it really helped you out it's the least you can do I'm not asking for money just asking for a thumbs up pretty easy stuff and just helps more and more people find the video and then last but not least make sure you see the other videos if you guys have any questions you can always try to email me I'm not the best with email because I get so many emails but I do read them all and I try to respond to them if I have time my email is down in the description you can also follow me on Twitter my you know Twitter handle is underscore J a Curtis J a Cu RT is tweet me there and I always respond on Twitter ok so I'll definitely get your response on Twitter if you found this informative and you finally understand multi Oz make sure to tell me about it just hit me up on Twitter and tell me I finally understand multiple authentication if you found this informative and you've been looking for something about multiple authentication if you guys are looking for the write-up of this I know everyone's going to want a write-up and so I am writing a blog post for it and the link for the written description on how to do all of these steps if you it was hard to follow on the video I'll have everything with all the code snippets down in the description below there's also a link to where you can find this on github you can download it if that's helpful for you or you can go through the files over there so I've gotten written description or in the description below we've got the written tutorial and we've got the github link and that's basically it so if you finally understand multi-up bola authentication multi user authentication I'd love if you guys told me about it hit me up on Twitter you can just say hey I finally understand multiple user authentication thanks a lot I appreciate that I usually retweet them too or like them or whatever and it just it just makes my day so anyway that's all I got for you guys in this video the next video we're going to go over login the video after that middleware the one after that forget my password so if you want to skip to any of those videos skip over one that you don't need whatever just do what you got to do thanks so much for tuning in subscribe if you have it and I'll see you guys the next video
Info
Channel: DevMarketer
Views: 400,145
Rating: undefined out of 5
Keywords: Laravel, users, php, code, authentication, laravel 5.4, tutorial, multiauth
Id: iKRLrJXNN4M
Channel Id: undefined
Length: 32min 56sec (1976 seconds)
Published: Sat Feb 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.