WEBVTT

00:00.590 --> 00:04.430
So let's continue working on the Remember token model.

00:04.460 --> 00:09.200
Now, I can understand that so far we are not really seeing any effects.

00:09.200 --> 00:15.320
We seem to be stuck on this login page, but I can promise you one thing.

00:15.620 --> 00:19.820
You're going to learn a lot of useful things by implementing this.

00:19.820 --> 00:21.920
Remember token functionality.

00:21.950 --> 00:31.190
Apart from how do you actually do such things as it's not obvious and it's really hard for beginners

00:31.190 --> 00:33.710
to just know how things are done.

00:34.160 --> 00:38.570
So let's go back to remember token model for sure.

00:38.570 --> 00:41.120
I've missed one thing the save method.

00:41.120 --> 00:49.070
Since it returns the model itself like we see right here, we don't really need to return this.

00:49.100 --> 00:52.790
We can just return the result of calling this save.

00:55.430 --> 00:56.360
Like that.

00:56.360 --> 01:09.120
And now why won't we add another method to this model that is static and might be called not just find,

01:09.120 --> 01:20.070
but instead find valid token or just find valid and it will accept a token.

01:22.170 --> 01:25.500
And it will return either null or the model.

01:25.500 --> 01:27.330
So it is nullable.

01:27.330 --> 01:27.870
Static.

01:27.870 --> 01:30.480
Static refers to the current class.

01:33.000 --> 01:41.460
So the goal of that is to get a token by the value of this actual generated token.

01:41.460 --> 01:49.080
That would be this column, as we are going to keep this token value inside a cookie.

01:49.770 --> 01:55.560
So when we fetch it from the cookie, we just need to then get it from the database.

01:55.560 --> 01:59.580
But we are not interested in expired tokens.

01:59.580 --> 02:04.380
That's why we would like to immediately get the valid one.

02:04.860 --> 02:07.260
So here we need to get the database.

02:07.260 --> 02:09.540
That's why we do app.

02:09.540 --> 02:12.360
Get database.

02:12.390 --> 02:14.190
Not sure if the app is imported here.

02:14.190 --> 02:15.120
It's not.

02:16.390 --> 02:17.620
Now it is.

02:18.370 --> 02:23.440
Next up, we need to calculate the current time.

02:23.920 --> 02:27.880
We can do that by calling date with the known format.

02:27.910 --> 02:28.300
Year.

02:28.330 --> 02:28.780
Month.

02:28.810 --> 02:29.560
Day.

02:29.590 --> 02:30.220
Hour.

02:30.220 --> 02:30.700
Minute.

02:30.700 --> 02:31.690
Second.

02:31.840 --> 02:40.570
I basically memorized this as that's the date format you're going to be using so often that eventually

02:40.600 --> 02:42.400
you're just going to memorize it.

02:44.290 --> 02:47.140
And now let's just construct the SQL.

02:47.170 --> 02:48.790
That's not super fancy.

02:48.820 --> 02:56.650
We are just taking all the columns from the static table.

02:58.300 --> 03:00.250
We've got some conditions.

03:00.250 --> 03:05.380
So I said we only care about a specific token.

03:05.380 --> 03:08.140
So we need to find it by the token column.

03:08.140 --> 03:13.390
And another thing is that it can't be expired.

03:15.820 --> 03:18.580
So that's why we use this comparison.

03:18.610 --> 03:28.600
So if this column value is bigger this means the date is after the date that we pass right here, which

03:28.600 --> 03:30.070
is the current time.

03:30.700 --> 03:44.320
And just to be sure, we can limit it to one, we just want one token so we can do return DB fetch using

03:44.320 --> 03:51.040
the SQL, passing the parameters which is the string token current time.

03:51.220 --> 03:54.880
And we make sure that we're going to get the current class.

03:54.880 --> 03:58.030
That is a remember token.

03:59.020 --> 04:03.820
Let's just jump to database to make sure that's the way it will work.

04:05.440 --> 04:07.090
So the fetch method.

04:08.020 --> 04:08.590
Okay.

04:08.590 --> 04:15.040
So fetch method can actually return false if we are unable to fetch the model.

04:17.020 --> 04:27.160
That's why we might use a variable called result and then return result if it's truthy.

04:27.190 --> 04:29.050
Otherwise return null.

04:29.950 --> 04:32.270
I think it might be just better.

04:33.440 --> 04:36.980
Okay, guys, still, we have to remember what is our goal.

04:37.010 --> 04:44.180
So if someone wants to be remembered, we need to create and store this remembered token in a database

04:44.180 --> 04:48.170
with an expiry date, which we have just completed.

04:48.170 --> 04:57.830
This model can handle everything about storing and retrieving a valid token and also rotating the token.

04:59.450 --> 05:01.280
So we've got the database logic.

05:01.280 --> 05:08.900
But the next step was to store the token in a cookie and also remove it from a cookie when someone logs

05:08.900 --> 05:09.560
out.

05:09.560 --> 05:13.370
So that's another layer of logic that we need to implement.

05:13.370 --> 05:18.290
We've got the model now we need to handle the whole process, including handling the cookie.

05:18.290 --> 05:25.490
And then the next step after that would be to connect everything into the authentication class that

05:25.490 --> 05:30.380
just handles logging in, signing out and getting the current user.

05:30.950 --> 05:32.360
So let's take a short break.

05:32.360 --> 05:39.350
And in the next one we're going to be working on storing and retrieving the token in a cookie.
