7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.1 Write a Real User Provider

In this lesson, we’ll build on the basic authentication we’ve already looked at and start implementing useful authentication components. We’ll update the database and our User class, as well as write a real user provider.

Related Links

3.1 Write a Real User Provider

In the past three lessons, we have customized our application with some pretty meaningless modifications. So in this lesson, we're going to do something useful. We're going to add a new column to our users table that determines whether or not a user is an admin. And then we are going to change our application, so that we will take that new column into account. And we will protect pieces of our application, based upon whether or not a user is an admin. So the first thing we need to do is modify our users table, and we're going to do that with a new migration. So let's do php artisan make:migration. Let's call this add_is_admin_to_users and then we want to specify the users table. So that's going to create that migration that we can then go in and modify. So we just want to add that new column. So let's open up that migration, and we are going to say table and we're going to call the tinyInteger method. Now, we might could call the Boolean method, but I have to say I've never done that. I assume that behind the scenes, that would create a tiny int for MySQL because MySQL does not have a Boolean field. So I'm going to play it safe and I'm going to call tinyInteger. Let's call this is_admin, and let's also set a default value. We want users to default to not to be an admin. That's something that we want to give only certain users. So that's our only thing that we need to add to our table. We want to go ahead and modify our user class, because inside of our code we want that is_admin property to be a Boolean. So we are going to say casts, and inside of this array, we will specify is_admin needs to be cast as a Boolean value. And so with that done, we can execute our migration. So let's go back, let's do php artisan and migrate, and that is going to modify our table. Just to make sure, let's go and look at it. So there is the users table. Let's go into there. The data should still be there, which it is. But if we scroll on over, we should have our new is_admin, and we do. The next thing we're going to do is write a new user provider. One that is going to retrieve a user by the given credentials, but it's only going to return that user if it is an admin. So let's go to app and then Extensions, and we are going to add a new class, and let's call this AdminUserProvider. And the great thing about this class is we don't have to write a lot of code, because really, we are just piggybacking off of the built-in EloquentUserProvider. So let's first of all put this in our namespace, that is going to be App\Extensions, and we want to use that EloquentUserProvider. So we'll add a use statement Illuminate\Auth\EloquentUserProvider. And then we will define our class, class AdminUserProvider. And that is going to extend the EloquentUserProvider. And really the only thing that we want to do is implement the retrieveByCredentials method, because everything else is going to be the same. So we will go ahead and write that. We want to retrieveByCredentials. Hopefully I typed that correctly, and it's going to accept an array that contains the credentials. And here we can go ahead and retrieve the user with our parent class' method of the same name. So let's say $user = parent::retrieveByCredentials() and we will pass in those credentials. Now it's entirely possible that user is null because of the credentials that were passed don't give us a user, then we need to return null here. But we also want to return null if the user is not an admin. Because the whole purpose of this AdminUserProvider is to return a user that is an admin. So let's first check to see if user is null or if user is_admin, or rather is not is_admin. If that is the case, then we will return null, otherwise they will return our user. So now that we have this AdminUserProvider, let's go ahead and register it. So we would need to go to our Providers folder, open up the AuthServiceProvider, and we're going to add another provider here. We'll go ahead and keep the two useless things that we have. By the time we're done in this lesson, those things aren't going to be used by the application anyway. So we want to say Auth::provider and we're going to call this admin user provider. And then we need our closure. It's going to accept the app and then the config. Now in this case, we need to use the app and the config in order to initialize our AdminUserProvider, because if you remember, the base class is the EloquentUserProvider and there are things that it needs in order to perform its work. So, we will new up AdminUserProvider. The first thing we need to pass in is the application hash. So that's the first thing that's passed to the constructor. The second thing is the model, because the EloquentUserProvider needs to know the model class for our user, and we saw that inside of the config. If we scroll on down to our providers, where the user's provider is defined the driver's eloquent, the model is the user class. So we need to pass this to the constructor, but we do so using our config. So we say config, and then we say model, and that gives us the value that is specified here, the user class. So we now have that registered. Let's go back to our auth.php file and we are going to add another entry here. We're going to call this admin-users and our driver is going to be what we just registered, and I already forgot what that was, so let's go back. Let's copy that and paste it in. That is the value. So driver is the key, and then the model is going to essentially be what we have for the already defined user's provider, because it's going to use the same class. Now let's scroll on up to where the guards defined. We are going to fix the web guard. So we're going to reset the driver to session. Let's get rid of the driver for the do not use, and the provider is going to be set back to users. Now we're going to create another guard here and we're going to call it admin. So that is going to be the key, the value is going to be an array that is going to resemble what we have for the web guard, but we are going to change the provider to the provider that we just created. So let's get that name, it is admin-users. That is going to be the name of our provider here. We will still use the session driver, but that is really what we want to do, the session driver is a very good driver. So let's go to our AuthServiceProvider. We do need to add a use statement for our new provider. So let's add that in, AdminUserProvider, and we should be okay to at least go to the browser, refresh the page, and see what happens. If we get any errors, then we know that something needs to be fixed, but hopefully we won't get anything. So let's go to the home page, that looks fine. Let's go to the login page. And there we go, let's also log in with the user that we have created already, so we should be able to log in. Everything there is working okay. And so now all we need to do in order to add this admin authentication to our application is write a little bit of middleware and we will do that in the next lesson.

Back to the top