WEBVTT

00:00.050 --> 00:04.160
So we need to store the remembered tokens inside the database.

00:04.370 --> 00:08.450
That's why I'd like to start with changing the schema.

00:08.600 --> 00:13.790
So we need to add a new schema for our remember tokens to make it quicker.

00:13.790 --> 00:16.310
I'm just going to copy and paste some code.

00:17.990 --> 00:22.460
So the table name would be remember underscore tokens.

00:22.460 --> 00:25.490
There are some of the columns that we need here.

00:25.490 --> 00:31.430
For example content can be renamed to token because it will be of type text.

00:31.700 --> 00:33.200
We need the user ID.

00:33.200 --> 00:34.370
So let's keep it.

00:34.400 --> 00:38.810
We don't need post ID and we need another date.

00:38.810 --> 00:41.000
This is expires at.

00:41.030 --> 00:44.300
We need to know when will the token expire.

00:44.330 --> 00:50.120
It doesn't have a default value but it can be null.

00:50.120 --> 00:57.170
We also have a created at timestamp and we can get rid of this post id foreign key as we don't have

00:57.170 --> 00:58.370
that relation.

00:58.400 --> 00:59.180
There it is.

00:59.180 --> 00:59.220
this.

00:59.220 --> 01:02.760
We've got the Remember Tokens table definition.

01:04.080 --> 01:04.290
Okay.

01:04.320 --> 01:07.110
So next up maybe let's create a model.

01:08.550 --> 01:12.600
Let's call it remember token.

01:14.370 --> 01:18.540
We don't really have any strict requirements on how we call the model.

01:18.900 --> 01:27.660
Let's define the namespace that's up models and the classes remember token.

01:28.890 --> 01:32.760
So it needs to extend a base model.

01:33.510 --> 01:39.600
It might be possible the model will also learned some interesting new methods.

01:39.990 --> 01:42.930
But first let's start with the basics.

01:43.560 --> 01:46.710
Let's add a field that defines the table name.

01:46.710 --> 01:52.830
So this is protected static string table.

01:52.830 --> 01:57.970
And this is remember Tokens.

01:58.540 --> 02:01.720
Then let's define all the fields.

02:01.930 --> 02:03.880
This would be public.

02:04.120 --> 02:04.660
Maybe.

02:04.660 --> 02:10.540
Let's try with some typing this time public int which is nullable before we save it.

02:10.570 --> 02:14.230
We don't have an ID so there is an ID.

02:14.470 --> 02:17.440
Then the next one is user ID.

02:18.820 --> 02:26.770
Then we've got the token and expires at.

02:29.410 --> 02:32.620
We should also have a createdat.

02:32.770 --> 02:37.150
And let's jump to schema ID token.

02:37.180 --> 02:39.460
User ID expires at createdat.

02:39.490 --> 02:40.150
Okay.

02:40.180 --> 02:43.360
All five fields are present.

02:46.510 --> 02:46.720
Okay.

02:46.720 --> 02:50.320
Next up let's think how can we generate this token.

02:50.410 --> 02:58.040
So I think that it's a good idea to generate the token within this Who remember talking model so that

02:58.040 --> 03:05.570
when this model is used, no one would have to figure out a way to generate the token by themselves.

03:06.260 --> 03:13.100
So let's add a private static method called generate token.

03:13.130 --> 03:16.100
It's only for internal use.

03:17.870 --> 03:20.120
We're going to generate the token a simple way.

03:20.570 --> 03:23.480
First we'll generate some random characters.

03:23.690 --> 03:33.680
The length is 32 since this is random bytes, so it might not generate Ascii characters, but also bytes

03:33.680 --> 03:36.230
with some random meaning.

03:36.620 --> 03:43.070
We convert that from binary to hexadecimal, so it looks like a normal string.

03:46.070 --> 03:55.170
So every token needs to expire at certain time, and this expiry date will be stored not only inside

03:55.170 --> 03:59.130
the database, but also inside the cookie.

04:00.720 --> 04:03.810
We might begin by adding a constant.

04:03.840 --> 04:13.020
Here this would be private constant and let's call it token lifetime.

04:14.130 --> 04:16.740
It needs to be reflected in seconds.

04:16.740 --> 04:19.350
So I multiply 30 days.

04:19.740 --> 04:24.270
That would be my default times 24 hours.

04:24.270 --> 04:27.840
Times 20 minutes times 20.

04:27.870 --> 04:32.310
Sorry times 60 minutes and 60s.

04:34.320 --> 04:49.320
Next up let's add a method called get expiry date and it will return a string because this one it will

04:49.320 --> 04:59.640
generate a date in a specific format, which is year, month, day, hour, minute, second.

05:01.620 --> 05:08.400
We generate it by getting the current time, which was returned in seconds.

05:10.260 --> 05:15.630
And to that we are adding the constant which is the token lifetime.

05:18.930 --> 05:19.170
Okay.

05:19.170 --> 05:24.180
So next let me add a public static method.

05:24.420 --> 05:27.540
We're going to call it create for user.

05:28.680 --> 05:32.340
And we pass the user id that's an integer.

05:32.340 --> 05:39.450
And it returns static which essentially means please return an instance of an remember token class.

05:39.840 --> 05:49.200
So what we will do here is we will wrap the create method that exists in a model as you see.

05:49.230 --> 05:50.350
It accepts the data.

05:50.350 --> 05:59.470
We've used this this method previously and we just pass some values for the columns.

05:59.470 --> 06:04.090
So for user ID we are passing the value of the passed argument.

06:04.360 --> 06:12.280
But some of those columns will have the values generated like the token.

06:12.370 --> 06:21.790
We've just created this generate token method and also the expiry date.

06:22.270 --> 06:26.860
We also have a method that can give us an expiration date.

06:29.470 --> 06:30.160
There we have it.

06:30.160 --> 06:37.330
So every single time you want a new remember token you can obviously create it using the create method.

06:37.330 --> 06:38.530
That's possible.

06:38.560 --> 06:46.000
But this seems to be a more handy way because you don't need to worry about the token or the expiration

06:46.000 --> 06:50.870
date, it just generates the right token, the right date.

06:50.900 --> 06:55.430
You only need to pass a user and the token is saved.

06:56.420 --> 07:02.060
Okay, so we have created the model, some basic methods and the schema.

07:02.150 --> 07:05.720
Next up we've got more work to do with the remember token.

07:05.750 --> 07:13.070
We need a method to find a valid token and another one to rotate the token which means to regenerate

07:13.070 --> 07:13.580
it.

07:13.730 --> 07:18.830
For that, I'd like us to also add something to the base model class.

07:18.860 --> 07:26.000
A method that will allow us to call save method on an instance of the model object.

07:26.360 --> 07:34.550
And this would either create or update database record with all the defined fields.

07:35.270 --> 07:38.120
That's also something that exists in Laravel.

07:38.150 --> 07:41.120
And we're going to see a simple way we can implement that.

07:41.150 --> 07:46.670
And since it might take a little longer I want us to take a short break right now.
