Okay, so how we can fix the page reload issue where basically since we're not using local storage anymore.
The moment I refresh.
Essentially, I'm logged out right away.
So our fix is going to be following.
We will set up another route on a server.
Um.
In that road.
We'll get the user details.
And we're looking for two things.
We're looking for the user as well as location, just like when we logged in.
Because keep in mind, when we log in, everything is correct.
The user is in a state because again, let me repeat in that protected route, the reason why we have
the error is because there's no more user once we refresh the page, that's why we get this error.
So in order to fix it, we'll have to set up a route that essentially gets the user info.
And let me show that in the read me.
So let's keep on moving.
We'll create a new route.
We'll find the user that is logging in.
And if you're wondering, well, how we can get that user?
Well, keep in mind that in the auth, we haven't changed anything.
I get the token again.
The difference is that I'm getting that token from the cookie.
I still get the user here.
It's in a payload and I pass it on as a property on the request object.
So in our new route, I'll still have access to this user.
So access to user info, it's going to be stored in the user and I'll send back user and the location
just like I do in login register and all that.
And essentially every time.
My application starts up.
I want to make that request.
So now check it out.
If you go to the production application, if you refresh, you'll see that in the network we get get
current user.
And if you paid attention, you notice that little loading spinner.
Basically, this one over here, notice we have this little loading and then we get to the application.
So that's that middle step that we need to make because every time basically we start up the application,
we want to check Hey.
Is the current user valid?
Is the cookie valid?
Has the token valid and all that?
If all of them are valid, yes, then let's show the application.
And then from there we can get the stats and the rest of the cool stuff.
So first, let's set up that route.
It's going to be in the controller, going to be a get root.
And essentially in here we want to get the user again, not going to repeat where this is coming from.
And then we want to set up the author outs.
And we want to go with get current user.
It's going to be authenticated because we want to pass it through the auth middleware and then respond
with get current user.
Again, if it's invalid, then we send back 401.
Great.
And after that, of course, we'll fix the front end.
So first, let's start in the controller, where right after the update user, I guess let's go with
const, get current user that is equal to H sync rec and rows of course.
And then we want to go with columns.
The user is equal to a wait and then user.
So that's our model.
Then find and we're looking for find one.
And let's pass in here the underscore ID and that will be equal to req user user ID like so.
And then.
Of course, we want to go with red dot status and now we want to send back basically the same thing.
So in order to speed it up, I'm just going to copy and paste.
So we're still send back to user as well as the location.
Now we do want to export that, so get current user and then in the author out where it is.
So our routes here have all throughout, we want to import that.
So let's go to that user, let's get current user, same controller, and then let's set up that new
route.
And in my case, I'm going to do that at the very end.
I'll just copy that.
I think it's going to be faster as far as the value in here.
You can go as well as you want.
In my case, I'm just going to go get current user.
It's not going to be patch one.
It's going to be get one.
We still want to authenticate the user.
We don't want to set up the test user over here and we just want to invoke.
The get current user.
Now, I'm not going to test it right now from the front end.
Basically, in the next video, I want to set up the actions of the function as well as when we invoke
it.
So for now, we're just want to change the setup on the server.
And again, the entire point for this is when we will refresh the application.
The first thing we'll do, we'll send out a request to get current user if it's valid, awesome.
We can continue.
If not, then we'll send back the 401 response.