So let's continue working on the Remember token model.
Now, I can understand that so far we are not really seeing any effects.
We seem to be stuck on this login page, but I can promise you one thing.
You're going to learn a lot of useful things by implementing this.
Remember token functionality.
Apart from how do you actually do such things as it's not obvious and it's really hard for beginners
to just know how things are done.
So let's go back to remember token model for sure.
I've missed one thing the save method.
Since it returns the model itself like we see right here, we don't really need to return this.
We can just return the result of calling this save.
Like that.
And now why won't we add another method to this model that is static and might be called not just find,
but instead find valid token or just find valid and it will accept a token.
And it will return either null or the model.
So it is nullable.
Static.
Static refers to the current class.
So the goal of that is to get a token by the value of this actual generated token.
That would be this column, as we are going to keep this token value inside a cookie.
So when we fetch it from the cookie, we just need to then get it from the database.
But we are not interested in expired tokens.
That's why we would like to immediately get the valid one.
So here we need to get the database.
That's why we do app.
Get database.
Not sure if the app is imported here.
It's not.
Now it is.
Next up, we need to calculate the current time.
We can do that by calling date with the known format.
Year.
Month.
Day.
Hour.
Minute.
Second.
I basically memorized this as that's the date format you're going to be using so often that eventually
you're just going to memorize it.
And now let's just construct the SQL.
That's not super fancy.
We are just taking all the columns from the static table.
We've got some conditions.
So I said we only care about a specific token.
So we need to find it by the token column.
And another thing is that it can't be expired.
So that's why we use this comparison.
So if this column value is bigger this means the date is after the date that we pass right here, which
is the current time.
And just to be sure, we can limit it to one, we just want one token so we can do return DB fetch using
the SQL, passing the parameters which is the string token current time.
And we make sure that we're going to get the current class.
That is a remember token.
Let's just jump to database to make sure that's the way it will work.
So the fetch method.
Okay.
So fetch method can actually return false if we are unable to fetch the model.
That's why we might use a variable called result and then return result if it's truthy.
Otherwise return null.
I think it might be just better.
Okay, guys, still, we have to remember what is our goal.
So if someone wants to be remembered, we need to create and store this remembered token in a database
with an expiry date, which we have just completed.
This model can handle everything about storing and retrieving a valid token and also rotating the token.
So we've got the database logic.
But the next step was to store the token in a cookie and also remove it from a cookie when someone logs
out.
So that's another layer of logic that we need to implement.
We've got the model now we need to handle the whole process, including handling the cookie.
And then the next step after that would be to connect everything into the authentication class that
just handles logging in, signing out and getting the current user.
So let's take a short break.
And in the next one we're going to be working on storing and retrieving the token in a cookie.