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

2.2 Create a User Provider

Providers are an integral part of Laravel’s authentication system—without them, the guards wouldn’t know how to retrieve users from the data store. In this lesson, we’ll write a simple user provider for our custom users.

Related Links

2.2 Create a User Provider

In the previous lesson we started customizing our application's user authentication by creating a custom user class. But by itself, that user class isn't very useful. We need a provider that will allow us to retrieve users from our data store. Now, we're not using any type of data store right now. Instead, the provider that we are going to write is simply going to return a user object, that has some static information. But we need to implement a user provider interface. It is Illuminate\Contracts\Auth\UserProvider. And you can see that there are several methods that we will need to implement within our UserProvider class. The first is retrieveById, this is so that we can retrieve a user by its given ID. We can also retrieve a user by its remember me token as well as its identifier. The next method is for updating that remember me token. The next method is for retrieveByCredentials. So that whenever we have the username and password we would use those credentials in order to find the user, but then we would also need a way to validate those credentials as well. Basically, this method would be used in order to validate the user that we have with its given credentials. So what I'm going to do is just copy the contents of this interface so that we don't have to type all of this out. And we will go to our extensions folder inside of app, and let's create a new file called MyUserProvider.php. Now let's go ahead and define the namespace, that's going to be our App\Extensions. Now we need to reference our myuser class. Let's go ahead and use App\Extensions\myuser. We also need to reference that authenticatable interface, so we want to use Illuminate\Contracts\Auth\Authenticatable. And then lastly we want to bring in that UserProvider namespace, so that is Illuminate\Contracts\Auth\UserProvider. Now the name of this class is going to be called my MyUserProvider. And we want to implement the UserProvider interface. And then we will just paste in all of that code that we copied. Now, of course, these aren't fully fledged methods. But they will be by just adding in those curly braces. So we're going to see that a lot of these methods return an authenticatable user. So really, let's just write a helper method that is going to create that user and return it. And of course, we want this to be a private, and function createUser, we aren't going to accept any arguments. If we wanted to make things a little bit more interesting we could, so that we can have a more dynamic user but let's just keep things simple. We're going to new up MyUser, but remember that this inherits from the generic user class. And if you remember from the previous lesson, I mentioned that we need to pass in an array that's going to contain some information about this user. So let's go ahead and set those things. The first thing we need is the ID. So let's go ahead and set that as the value of 1. We also need to specify the password, and for the lack of a better password, let's just use password. We also need the remember me token. So remember_token. Now, I got this from the generic user class. If you look at the code for that, you'll see that that is automatically set for the name of that token. And for the value, let's just have remembertoken. And then finally let's also have the name. This is a piece of information that would be used inside of the view. And let's just call this Static User. So this createuser method is going to create our user and return it. So that whenever we want to retrieve a user by ID we are simply going to return $this->createUser. Now of course, within a real user provider we would be working with a real data store. We would want to retrieve the user with the given identifier. But that's not going to be the case here. So, that is it for the retrieveById method. We also want to implement this retrieveByToken which returns a user so, let's just do the same thing. We aren't going to worry about any information passed to these methods, we are just going to return the user where we need to return that user. Now for the updateRememberToken, we don't really need to return anything. We can see here that it returns void. So we're not going to have anything inside of that method. Once again, if this was a real user provider, we would have the code there for updating that remember token in our data store. Now, this retrieveByCredentials needs to return a user, so we will do that there. And then finally, the validate credentials. And let's just return true. So basically, it doesn't matter what we type into the login form. It is going to log us in. So this is definitely something that we would not want to use within a real application, but we're going to use it anyway. So we have created our user provider, we have our user class, now we just need to register our user provider so that we can then use it within our application. And we can do that in a variety of places. We could write our own service provider that would then setup our custom user provider, or we can just open up the AuthServiceProvider class. And inside of the boot method, we can register that user provider. And that's what we are going to do just to keep things simple. Now, in order to register a user provider, we need to use the Auth facade. But, in order to do that, we need to come to the top and we need to say use Auth. And while we're here, we'll go ahead and use App\ExtensionsMyUserProvider. So we'll use the Auth facade, and we will call the provider method. Now this accepts two arguments, the first is the name that we want to use for this provider, and this is something that we don't really want to use. So let's go ahead and say donotuse-user-provider. So the first argument is the name that we are going to give this provider. The second is a closure that returns our new user provider. Now this closure accepts two pieces of information. The first is the app, the second is an array that contains the configuration. So if you need to configure your user provider, then you have the information that you need. Now, our user provider doesn't need any type of configuration because it's pretty self-sufficient. So we are simply going to return a new MyUserProvider. So we have our user provider registered, but we also need to configure our application to use it. So we're going to go to the config folder. We're going to go to the auth.php file, and we're going to scroll down to the user provider section. Now there is a user provider called users, the driver is set to eloquent and then the model is set to the user class. Now the driver is our user provider, the one that we just registered. So we could modify this user's entry or we could just create our own, and in the spirit of the name donotuse, we're going to do the same thing, donotuse. So this is going to be a provider called donotuse, its driver is going to be set to the user provider that we just registered, so donotuse-user-provider. Although that's the value, we first of all need the name driver and then the value donotuse-user-provider. Now we don't need to specify a model because our driver doesn't need to know what type of user that we are going to be using. Once again, it is pretty self sufficient. So we don't need to specify model there. Now all we have done is set up our provider. We aren't actually using that yet. But if we scroll on up to where the guards are defined, where we have the web and then the API guards, this is where we can use our new user provider. You can see that users is the value for the provider, and we will basically do the same thing except that we will set it to donotuse. So I'm going to comment out what was there and then we will set the provider to donotuse. So just to recap everything that we have done in these two lessons, we have created a custom user, and then we wrote a user provider to give us those users. After doing that, we registered our user provider with the auth facade and then we set up our own custom provider inside of auth.php. We gave the provider its name. We set its driver to the user provider that we've registered inside of AuthUserProvider. And then we set the web guard to use the donotuse-provider. So If we wrote everything correctly, we should be able go to the browser refresh and we don't have any errors. So that's good. Now anything thing that we type into these two fields shouldn't matter, because our user provider is always going to return a user instance. So we can just come in here and type something nonsensical, and if we log in, then it should allow us to log in. If we look at the name of this user, we see that it is Static User. So right now, our application is using our custom user, and custom user provider. Now I know that these classes that we have written aren't very useful and that's by design. I want to get these concepts across to you before we actually write something that you might want to use within your application. So in the next lesson, we are going to write a custom guard, it's still going to be in the same vein that we have been looking at the custom user and custom user provider. But after we do that, then we will write a real custom user and user provider that you may or may not want to include within your own applications.

Back to the top