1
00:00:00,360 --> 00:00:10,200
Okay, so how we can fix the page reload issue where basically since we're not using local storage anymore.

2
00:00:10,230 --> 00:00:11,910
The moment I refresh.

3
00:00:12,650 --> 00:00:14,770
Essentially, I'm logged out right away.

4
00:00:15,370 --> 00:00:17,380
So our fix is going to be following.

5
00:00:18,170 --> 00:00:22,340
We will set up another route on a server.

6
00:00:23,820 --> 00:00:24,230
Um.

7
00:00:24,990 --> 00:00:26,130
In that road.

8
00:00:26,130 --> 00:00:28,470
We'll get the user details.

9
00:00:29,100 --> 00:00:30,630
And we're looking for two things.

10
00:00:30,950 --> 00:00:36,800
We're looking for the user as well as location, just like when we logged in.

11
00:00:36,810 --> 00:00:40,260
Because keep in mind, when we log in, everything is correct.

12
00:00:40,650 --> 00:00:47,550
The user is in a state because again, let me repeat in that protected route, the reason why we have

13
00:00:47,550 --> 00:00:52,800
the error is because there's no more user once we refresh the page, that's why we get this error.

14
00:00:53,310 --> 00:00:58,920
So in order to fix it, we'll have to set up a route that essentially gets the user info.

15
00:00:59,010 --> 00:01:00,900
And let me show that in the read me.

16
00:01:01,170 --> 00:01:02,480
So let's keep on moving.

17
00:01:02,480 --> 00:01:03,870
We'll create a new route.

18
00:01:04,700 --> 00:01:07,640
We'll find the user that is logging in.

19
00:01:07,670 --> 00:01:10,520
And if you're wondering, well, how we can get that user?

20
00:01:10,880 --> 00:01:15,500
Well, keep in mind that in the auth, we haven't changed anything.

21
00:01:16,300 --> 00:01:18,220
I get the token again.

22
00:01:18,250 --> 00:01:21,400
The difference is that I'm getting that token from the cookie.

23
00:01:21,730 --> 00:01:23,650
I still get the user here.

24
00:01:24,310 --> 00:01:30,910
It's in a payload and I pass it on as a property on the request object.

25
00:01:31,900 --> 00:01:36,640
So in our new route, I'll still have access to this user.

26
00:01:37,520 --> 00:01:45,350
So access to user info, it's going to be stored in the user and I'll send back user and the location

27
00:01:45,350 --> 00:01:48,140
just like I do in login register and all that.

28
00:01:49,040 --> 00:01:51,080
And essentially every time.

29
00:01:52,490 --> 00:01:54,440
My application starts up.

30
00:01:54,470 --> 00:01:56,360
I want to make that request.

31
00:01:56,480 --> 00:01:57,500
So now check it out.

32
00:01:57,830 --> 00:02:05,000
If you go to the production application, if you refresh, you'll see that in the network we get get

33
00:02:05,000 --> 00:02:06,020
current user.

34
00:02:06,200 --> 00:02:10,220
And if you paid attention, you notice that little loading spinner.

35
00:02:10,820 --> 00:02:16,960
Basically, this one over here, notice we have this little loading and then we get to the application.

36
00:02:16,970 --> 00:02:23,180
So that's that middle step that we need to make because every time basically we start up the application,

37
00:02:23,180 --> 00:02:24,650
we want to check Hey.

38
00:02:25,700 --> 00:02:27,800
Is the current user valid?

39
00:02:27,830 --> 00:02:29,570
Is the cookie valid?

40
00:02:30,140 --> 00:02:31,880
Has the token valid and all that?

41
00:02:32,030 --> 00:02:35,750
If all of them are valid, yes, then let's show the application.

42
00:02:35,750 --> 00:02:40,610
And then from there we can get the stats and the rest of the cool stuff.

43
00:02:41,180 --> 00:02:43,670
So first, let's set up that route.

44
00:02:43,820 --> 00:02:47,720
It's going to be in the controller, going to be a get root.

45
00:02:47,720 --> 00:02:53,000
And essentially in here we want to get the user again, not going to repeat where this is coming from.

46
00:02:53,980 --> 00:02:56,590
And then we want to set up the author outs.

47
00:02:56,710 --> 00:03:00,010
And we want to go with get current user.

48
00:03:00,100 --> 00:03:07,120
It's going to be authenticated because we want to pass it through the auth middleware and then respond

49
00:03:07,400 --> 00:03:09,310
with get current user.

50
00:03:09,460 --> 00:03:13,900
Again, if it's invalid, then we send back 401.

51
00:03:14,670 --> 00:03:15,120
Great.

52
00:03:15,330 --> 00:03:18,180
And after that, of course, we'll fix the front end.

53
00:03:18,510 --> 00:03:25,950
So first, let's start in the controller, where right after the update user, I guess let's go with

54
00:03:25,950 --> 00:03:34,740
const, get current user that is equal to H sync rec and rows of course.

55
00:03:34,740 --> 00:03:36,420
And then we want to go with columns.

56
00:03:36,480 --> 00:03:40,140
The user is equal to a wait and then user.

57
00:03:40,140 --> 00:03:41,400
So that's our model.

58
00:03:41,490 --> 00:03:44,760
Then find and we're looking for find one.

59
00:03:45,440 --> 00:03:53,960
And let's pass in here the underscore ID and that will be equal to req user user ID like so.

60
00:03:54,350 --> 00:03:55,010
And then.

61
00:03:55,770 --> 00:04:01,500
Of course, we want to go with red dot status and now we want to send back basically the same thing.

62
00:04:01,500 --> 00:04:04,470
So in order to speed it up, I'm just going to copy and paste.

63
00:04:04,680 --> 00:04:08,850
So we're still send back to user as well as the location.

64
00:04:08,850 --> 00:04:16,320
Now we do want to export that, so get current user and then in the author out where it is.

65
00:04:16,620 --> 00:04:21,390
So our routes here have all throughout, we want to import that.

66
00:04:21,870 --> 00:04:30,240
So let's go to that user, let's get current user, same controller, and then let's set up that new

67
00:04:30,240 --> 00:04:30,720
route.

68
00:04:31,320 --> 00:04:33,650
And in my case, I'm going to do that at the very end.

69
00:04:33,660 --> 00:04:34,960
I'll just copy that.

70
00:04:35,010 --> 00:04:38,760
I think it's going to be faster as far as the value in here.

71
00:04:38,760 --> 00:04:40,580
You can go as well as you want.

72
00:04:40,590 --> 00:04:43,470
In my case, I'm just going to go get current user.

73
00:04:43,500 --> 00:04:44,790
It's not going to be patch one.

74
00:04:44,790 --> 00:04:46,050
It's going to be get one.

75
00:04:46,050 --> 00:04:48,770
We still want to authenticate the user.

76
00:04:48,780 --> 00:04:53,100
We don't want to set up the test user over here and we just want to invoke.

77
00:04:54,050 --> 00:04:56,960
The get current user.

78
00:04:57,410 --> 00:05:00,500
Now, I'm not going to test it right now from the front end.

79
00:05:00,920 --> 00:05:08,600
Basically, in the next video, I want to set up the actions of the function as well as when we invoke

80
00:05:08,600 --> 00:05:08,960
it.

81
00:05:09,080 --> 00:05:13,220
So for now, we're just want to change the setup on the server.

82
00:05:13,220 --> 00:05:19,310
And again, the entire point for this is when we will refresh the application.

83
00:05:19,340 --> 00:05:26,300
The first thing we'll do, we'll send out a request to get current user if it's valid, awesome.

84
00:05:26,300 --> 00:05:27,100
We can continue.

85
00:05:27,110 --> 00:05:31,280
If not, then we'll send back the 401 response.

