Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.1 Create an Authenticatable User

Laravel’s authentication system is made up of several individual components. The first thing you need is a class that represents your users. In this lesson, we’ll briefly look at Laravel’s authentication configuration, and then we’ll write an Authenticatable user class.

Related Links

2.1 Create an Authenticatable User

In this lesson, we're going to start customizing our applications user authentication. And we're going to start by going to the config folder and inside of there is a file called auth.php. Now this is our application's authentication configuration. And user authentication and layer of L is made up of two pieces. The first is called a guard, which defines how users are authenticated for every request. Now, layer of L comes with two guards. One is called a session guard, the other is called a token guard. A session guard is used for your typical web application. All of the user information is stored in session storage and cookies and that session guard is responsible for reading that in order to authenticate the user on every request. But if you have a web API, sessions aren't really used. Instead you have a token that is provided with every request. So the token guard would be used to authenticate the users in that way. But what we are going to get started with in this lesson Is customizing our user provider. This is the second part of user authentication. So you have your guard, which defines how users are authenticated, and then you have your user provider. And here you can see where the providers are defined for our application. Now, there is one called users. There's something called a driver, and we can see that that is set to eloquent. So this is using the eloquent driver. And that the eloquent driver needs to know the model class for representing our users and that is that user class that is inside of the app folder. We briefly looked at this in the previous lesson. So if we go back and look at that file. This is being used by eloquent in order to represent the data that is inside of our user table. And we can see that it extends this authenticatable, and that's just an alias for illuminate foundation auth user. Now,that's not important, at least to us right now. If we were still going to be using eloquent, then that is something that we would probably want to extend. But what we are going to do is write a user provider that always returns a user. We're not storing that information anywhere. So what is important to us is that we implement an interface called authenticatable. If we go to vendor and then layer of L. Framework source and then we want to go to illuminate contracts and then authenticatable.php. This is an interface and you can see the methods involved here. Now getAuthIdentifierName is a method that will return the name of the primary key. So if we were going to create a user class that was going to be stored within the database, the identifier name would typically be ID. And then the next method to getAuthIdentifier. That would be actually retrieving the ID for that user. And then there's a method for retrieving the user's password, a method for getting the token value for the remember me. You know, that checkbox whenever you attempt to log in. And then a method for setting that value. And then finally is a method that returns the name of the remember me token. So once again, if we were storing this information in a database, then this would return the name of the column in the table where we would be storing that remember me token name. Now that seems like a lot of work. There are several methods here that we would have to implement on our user class. But thankfully, there is a class already defined that we can use as a base class for our user. It's inside of illuminate auth, and it's simply called generic user. We can see that it implements the authenticatable interface and if we scroll on down we can see that all of those methods are implemented. Now, you'll see that there is something called attributes and if you solve the constructor accepts an arrays of attributes. So whenever we create a user, we're going to have to pass an array that has a variety of information. For example, we need to specify an ID and we will also need to specify a password and a remember token. And it's not listed here but we will also need to pass in a name. Otherwise, we will get an error whenever we try to view this in the browser because one of the views displays the name. So this is going to be our base class. Let's go back to our app folder and let's create a new folder called Extensions. And then inside of this extensions folder we're going to create a MyUser.php file. And let's go ahead and define the namespace. And I'm just going to say app slash extensions. Now we want to use that generic user. So let's say use Illuminate and then Auth and then GenericUser. And we're going to call our class MyUser. So class MyUser extends GenericUser. And that's all that we need to do for our user class. Of course, user classes are relatively simple because all they do is represent the user information. But in the next lesson, we are going to implement a user provider. Which in our case is still going to be simple, but there is more code involved.

Back to the top