WEBVTT

00:02.060 --> 00:09.830
So last time we came up with a way to share some information with every single template and every single

00:09.830 --> 00:14.720
layout using magic variables like this user variable.

00:14.810 --> 00:18.260
But I don't want to share my name with everyone.

00:18.260 --> 00:27.260
Instead, I want to share the currently authenticated user either the model, the loaded user model,

00:27.260 --> 00:35.210
if someone is signed in, or if no one is signed in, then this should be the value of null.

00:35.990 --> 00:42.800
So let's jump to our Auf service, which only currently has this attempt method.

00:43.100 --> 00:45.230
Let's add something more.

00:45.620 --> 00:48.920
Let me add a static method.

00:51.200 --> 00:59.390
Called user and it would return either the user model or null.

01:00.600 --> 01:02.070
Now also.

01:04.350 --> 01:04.710
Okay.

01:04.710 --> 01:07.290
So how this can work.

01:07.950 --> 01:11.250
We should get the user ID from the session.

01:11.250 --> 01:19.710
So remember that we are storing the user ID in the session if user signs in and if the session is not

01:19.710 --> 01:20.820
expired.

01:22.260 --> 01:23.910
So we should have the user id.

01:24.630 --> 01:34.320
This means that we should be able to return the user model if this is not null.

01:34.530 --> 01:45.480
So if the user id is not null, we can use the user model find method to find the user by id.

01:45.690 --> 01:48.600
That's one of the built in model methods.

01:48.840 --> 01:51.360
Otherwise we return null.

01:51.720 --> 01:54.210
It is this simple.

01:54.240 --> 02:01.600
Now we're going to optimize that during this video because currently if you'd like to get the currently

02:01.600 --> 02:07.120
authenticated user, it's gonna make a database query every single time.

02:07.120 --> 02:12.640
And it's not necessary, at least not during one single request.

02:13.390 --> 02:16.240
So let's jump back to the front controller.

02:16.390 --> 02:25.300
And now instead of the name, I can use the auth class static method user, which I hope should give

02:25.300 --> 02:28.210
me the currently authenticated user.

02:28.240 --> 02:31.330
I'm going I'm using it right here.

02:33.490 --> 02:43.690
So let me jump to my blog and well, nothing terrible has happened, but I'm still not sure what we

02:43.690 --> 02:46.330
have inside this user variable.

02:48.430 --> 02:50.020
Maybe let's jump right here.

02:50.020 --> 03:02.420
And let me try to echo the logout link using my email I'm just going to try and echo the user email.

03:02.420 --> 03:09.470
Every user needs to have an email, so it can't be null if this is the user object.

03:11.600 --> 03:12.440
There it is.

03:12.440 --> 03:15.140
So it works perfectly.

03:15.920 --> 03:18.290
Now we need to optimize it.

03:19.280 --> 03:19.910
Okay.

03:19.910 --> 03:23.990
So we have the user ID in the session.

03:23.990 --> 03:34.700
And for the duration of the request we can keep the user model in a variable in a static field maybe.

03:37.250 --> 03:38.630
Let's define it then.

03:38.630 --> 03:44.360
Let's make it protected static user.

03:44.750 --> 03:47.060
By default it is null.

03:47.600 --> 03:51.110
And now let's modify this user method.

03:51.110 --> 03:58.190
So first if the user is null.

04:00.360 --> 04:07.710
Only then we're gonna try to, you know, get this logic of fetching the user.

04:07.710 --> 04:11.250
So we get the user ID first from the session.

04:14.940 --> 04:22.680
And then we store the result using static user without the return keyword here.

04:24.750 --> 04:32.310
And only then we always return the value of this user variable which would be null.

04:32.340 --> 04:36.930
Or it will contain the user model if that works.

04:36.930 --> 04:39.300
So I'm refreshing that.

04:39.300 --> 04:45.810
And it seems that everything still works and it still works everywhere.

04:47.010 --> 04:55.620
So this means that we should also add a sign in link if I am not signed in at the very moment, but

04:55.620 --> 05:02.200
to be able to do that, first we should work on logging out functionality and we're going to do this

05:02.200 --> 05:03.130
next.

05:03.610 --> 05:12.220
So one last thing to fix with this code is that the sessions they automatically expire after some time.

05:12.220 --> 05:14.140
That's normal in PHP.

05:14.170 --> 05:21.880
You've got a session timeout, and after some time of inactivity of the user, his session will expire

05:21.880 --> 05:28.240
and we just try to get the user ID from the session always.

05:28.240 --> 05:35.230
And if the key is not inside the super global, we're going to get this error, which also might happen

05:35.230 --> 05:37.120
if you are just not authenticated.

05:37.150 --> 05:41.830
That's why we need to make sure we add a null coalescing operator here.

05:41.830 --> 05:49.210
So if this key is not defined we just provide an alternative which is null.

05:49.210 --> 05:52.000
And then this fixes this problem.

05:52.000 --> 05:57.250
If someone is not authenticated or if his session has already expired.
