Okay, finally, it's time that we're gonna have a live debut of this Remember Me feature we've been
cooking for the last 30 or 40 minutes.
Let's begin by changing the form.
This is a create form inside the auth folder, and that's our placeholder for a Remember Me feature.
Let's add it then start with a label for the remember field.
And we're going to wrap the input with the label.
The input is of type checkbox.
The ID is.
Remember needs to be the same as this for attribute.
So it can be activated when the label is clicked and the name is.
Also remember.
Let's Self close and add the Remember Me text.
Okay, so this looks terrible and my suggestion would be to just ignore that.
We can restyle this app a little later.
So I don't think we should focus on this right now.
Let's just close it and go back to the auth class or more specifically to auth controller as that's
the first point.
Okay.
So we've got the email, the password.
We are missing the remember setting.
So here we should either get the remember token if it's present.
So maybe we can first verify if it was sent.
So if there is a Remember token set, we're going to use a ternary operator.
Then we get the remember token.
Otherwise we set to false.
Maybe we can also cast this to boolean.
Yeah.
Let's see what's the value actually.
So let me var dump the remember value.
I'm going to type any data in here.
And yeah I need to also die.
So we are just testing what will be sent okay.
So if we don't check it then we have a boolean.
This seems fine.
Um, let me jump back.
And if I select this check box I'm getting true.
So this is perfectly fine what we expect.
And this value would be used for the auth attempt which should have another argument.
So this next argument would be the boolean remember which is defaulting to false.
So we don't break any existing code okay.
So in this attempt method we mark the user as being sign in regenerating the session, setting the user
ID on the session and we should do something else.
So if the remember token is expected, we additionally use the Remember Me class static method.
And I think we should call create token for this user.
I'm not sure if this would be an integer.
We can see later on anyway.
This just does everything what's required.
So a quick reminder.
Storing a token in a database.
Setting a cookie and returns the token.
We might not need this token inside the auth class, but we definitely need to create it.
Then we should definitely do something when we log out.
So we destroy the session.
We reset the user.
That is just a static property on this class.
But additionally we should start with clearing the token.
So this Remember Me class has a clear token method.
It is simple.
It's not deleting all possible tokens for that user, as user might have many on different devices.
It's just checking inside, let's say the current session.
Current browser session for the token cookie.
If there is a cookie.
It tries to find a token that would be valid because invalid tokens.
Well, we don't need to worry about them.
We might want to remove them after some time, but we definitely don't need to worry about them because
they are expired.
And if it finds a token in a database, it is deleting it and then it's removing the cookie, thus forgetting
the token.
That definitely needs to happen when we sign out.
And here we also got this user method.
So currently it is relying on the session.
But we also have this remember token, and it exists for that very reason that user might have an expired
session after a couple of hours of inactivity.
So also here we need to check if maybe the user has a cookie and we can fetch the current user using
this Remember Me token.
So let's jump to Remember Me class again.
And we have this validate token method.
We enlarge the code editor.
Let me see if we have used this anywhere.
Now it is just defined okay you know what.
Since this is returning a user I'm not sure this should be called validate token.
It may be should just be called user because it knows what it needs to do.
It's not accepting any arguments.
It's getting data from the cookie and it is fetching the user from the database if there is a cookie.
So that's an alternative to what we are doing right here.
And this means that if we cannot get the user ID from the session, another way we can do is to try
and get the user from the Remember Me token.
Okay, so since here we are checking if the user ID is set and if it is in the session.
We are trying to find the user inside the database or we return null.
This other condition can actually be remember me user.
So we prefer a session that is our first choice.
But if there is no one in the session or we can't find the user from the session, we try using the
Remember Me token, which as we can see, will either return the found user looking at the cookie,
then a token in the database and then fetching the user with this given remember token or well, it
will just return null, which aligns perfectly with our logic right here.
I can reformat that so it is clear what's happening.
And then we always return static user.
So it wasn't very complicated.
And I think we added this remember me functionality at least theoretically, because obviously now it's
the hardest part.
We need to see if it works.
So first let me open the terminal and let's make sure we updated the schema.
So we've got the okay schema load.
It's updated.
Let me verify this.
That's database block SQLite.
I should see another table.
You might refresh if you don't see it.
So there we have a Remember Tokens table.
Let me jump to remember token.
If the table name is right that's remember tokens.
So far everything is going fine.
So I think that our next step should be to test that.
So I think I need a bigger browser window.
We need to see at least if I will be able to sign in, if the cookie would be created, if the remember
token would be created, then we're gonna try somehow to reset the session and we'll see if I will be
still authenticated.
Thanks to this cookie and the token stored in the database.
So simulating this process probably wouldn't be too easy and can take some time.
A little bit of manual work.
I think it will also be useful to just figure out how how all of those things work.
That's why, um, we've prepared ourselves the best we can.
And let's now take a short break before before the final boss, which is verifying if everything works
perfectly.
We see each other in couple seconds.