So last time we came up with a way to share some information with every single template and every single
layout using magic variables like this user variable.
But I don't want to share my name with everyone.
Instead, I want to share the currently authenticated user either the model, the loaded user model,
if someone is signed in, or if no one is signed in, then this should be the value of null.
So let's jump to our Auf service, which only currently has this attempt method.
Let's add something more.
Let me add a static method.
Called user and it would return either the user model or null.
Now also.
Okay.
So how this can work.
We should get the user ID from the session.
So remember that we are storing the user ID in the session if user signs in and if the session is not
expired.
So we should have the user id.
This means that we should be able to return the user model if this is not null.
So if the user id is not null, we can use the user model find method to find the user by id.
That's one of the built in model methods.
Otherwise we return null.
It is this simple.
Now we're going to optimize that during this video because currently if you'd like to get the currently
authenticated user, it's gonna make a database query every single time.
And it's not necessary, at least not during one single request.
So let's jump back to the front controller.
And now instead of the name, I can use the auth class static method user, which I hope should give
me the currently authenticated user.
I'm going I'm using it right here.
So let me jump to my blog and well, nothing terrible has happened, but I'm still not sure what we
have inside this user variable.
Maybe let's jump right here.
And let me try to echo the logout link using my email I'm just going to try and echo the user email.
Every user needs to have an email, so it can't be null if this is the user object.
There it is.
So it works perfectly.
Now we need to optimize it.
Okay.
So we have the user ID in the session.
And for the duration of the request we can keep the user model in a variable in a static field maybe.
Let's define it then.
Let's make it protected static user.
By default it is null.
And now let's modify this user method.
So first if the user is null.
Only then we're gonna try to, you know, get this logic of fetching the user.
So we get the user ID first from the session.
And then we store the result using static user without the return keyword here.
And only then we always return the value of this user variable which would be null.
Or it will contain the user model if that works.
So I'm refreshing that.
And it seems that everything still works and it still works everywhere.
So this means that we should also add a sign in link if I am not signed in at the very moment, but
to be able to do that, first we should work on logging out functionality and we're going to do this
next.
So one last thing to fix with this code is that the sessions they automatically expire after some time.
That's normal in PHP.
You've got a session timeout, and after some time of inactivity of the user, his session will expire
and we just try to get the user ID from the session always.
And if the key is not inside the super global, we're going to get this error, which also might happen
if you are just not authenticated.
That's why we need to make sure we add a null coalescing operator here.
So if this key is not defined we just provide an alternative which is null.
And then this fixes this problem.
If someone is not authenticated or if his session has already expired.