1
00:00:00,240 --> 00:00:06,660
And once we have the get current user set up on a server, now let's do the same thing on the front

2
00:00:06,660 --> 00:00:07,060
end.

3
00:00:07,080 --> 00:00:14,070
And since this video might get long, I will actually split it up in two.

4
00:00:14,100 --> 00:00:21,210
So in the first video, we'll set up the app context and then in the following we'll set up the reducer.

5
00:00:21,240 --> 00:00:22,200
So here's the plan.

6
00:00:22,200 --> 00:00:24,930
We want to set up these two actions.

7
00:00:24,990 --> 00:00:28,040
And you'll see in a second why we don't need the error.

8
00:00:28,530 --> 00:00:31,890
We'll set up the imports and app context as well as the reducer.

9
00:00:32,369 --> 00:00:37,460
And the first thing we want to do is set up in the initial state user loading.

10
00:00:37,470 --> 00:00:40,800
So this one will be a little bit different than is loading.

11
00:00:41,620 --> 00:00:46,390
This one we will only use when we're fetching the current user.

12
00:00:46,690 --> 00:00:48,580
So essentially the idea is following.

13
00:00:49,000 --> 00:00:58,210
Every time we refresh the application will perform a request, get current user, which of course is

14
00:00:58,210 --> 00:01:00,980
going to go to the route we just set up.

15
00:01:01,000 --> 00:01:03,970
And essentially we want to get to things.

16
00:01:03,970 --> 00:01:08,710
We want to get the user and location to things that we're already sending back from the server.

17
00:01:08,740 --> 00:01:09,370
Correct.

18
00:01:09,580 --> 00:01:11,230
And then we want to dispatch that.

19
00:01:11,740 --> 00:01:13,900
We want to essentially set it up in a state.

20
00:01:14,230 --> 00:01:15,880
Now, why do we want to do that?

21
00:01:16,240 --> 00:01:18,970
Well, because we don't have any local storage anymore.

22
00:01:19,000 --> 00:01:19,510
Correct.

23
00:01:19,930 --> 00:01:22,530
And we do need to get that data when we refresh.

24
00:01:22,540 --> 00:01:23,020
Yes.

25
00:01:23,020 --> 00:01:28,910
When we log in and let's say we navigate that around our application, everything is still stored in

26
00:01:28,910 --> 00:01:29,680
the state.

27
00:01:29,710 --> 00:01:31,210
So everything is working.

28
00:01:31,390 --> 00:01:36,310
But once we refresh that data, the user and location is not.

29
00:01:37,400 --> 00:01:38,760
In the initial state.

30
00:01:38,780 --> 00:01:39,410
Correct.

31
00:01:39,890 --> 00:01:40,860
User is null.

32
00:01:40,880 --> 00:01:42,500
And location is an empty string.

33
00:01:42,500 --> 00:01:43,940
And we used to fix that.

34
00:01:44,610 --> 00:01:45,780
With local stores.

35
00:01:45,810 --> 00:01:47,040
Now, we don't have that anymore.

36
00:01:47,040 --> 00:01:53,580
So we perform a request the moment we refresh the application which fetches that data.

37
00:01:53,850 --> 00:02:00,990
And if everything is correct, then essentially what we do, we set up that user and.

38
00:02:01,640 --> 00:02:06,410
We don't want that protected route to kick us out.

39
00:02:06,650 --> 00:02:09,500
Remember, in the protected route, we're checking for the user.

40
00:02:10,400 --> 00:02:13,600
And once we refresh, it will automatically run this code.

41
00:02:13,610 --> 00:02:19,640
So that's why we need that user loading that is going to stop the protected us from logging us out will

42
00:02:19,640 --> 00:02:20,720
just display the loading.

43
00:02:20,720 --> 00:02:28,160
And then once we find out whether the cookie and the JWT is valid, then we display the user.

44
00:02:28,190 --> 00:02:29,500
Hopefully this is clear again.

45
00:02:29,510 --> 00:02:30,230
Hopefully.

46
00:02:31,120 --> 00:02:33,760
I'm going to be able to repeat quite a few times.

47
00:02:33,760 --> 00:02:35,620
So you definitely get it.

48
00:02:35,920 --> 00:02:40,540
And as far as the error, why do we need essentially a action?

49
00:02:41,080 --> 00:02:41,990
Well, think about it.

50
00:02:42,010 --> 00:02:46,300
We have only two options when we perform this request.

51
00:02:46,480 --> 00:02:52,510
Either we get back to essentially a valid user or it's going to be 401.

52
00:02:52,690 --> 00:02:54,580
There are no other options.

53
00:02:55,030 --> 00:03:00,430
Now, just let's say for the kicks we can add here logout user.

54
00:03:00,640 --> 00:03:05,790
But in general, we'll log out this user already where in the interceptor.

55
00:03:05,800 --> 00:03:09,310
Remember, in the app context, we already have interceptor that does that.

56
00:03:09,700 --> 00:03:10,300
Correct.

57
00:03:10,420 --> 00:03:14,160
So and here in the error you can simply log it if you want.

58
00:03:14,170 --> 00:03:18,610
But in my case, I just went the long route where I'm going to check for response status.

59
00:03:18,610 --> 00:03:24,640
And if it's for one that I'm simply returned because I already know that essentially the user has been

60
00:03:24,640 --> 00:03:27,720
logged out where in the interceptor.

61
00:03:27,730 --> 00:03:29,500
So let's start working on this one.

62
00:03:30,190 --> 00:03:36,280
Now, in order to save a little bit of time or grab these two things, essentially the actions we want

63
00:03:36,280 --> 00:03:42,910
to navigate there and we want to copy and paste, I believe my last one is change page.

64
00:03:43,180 --> 00:03:44,560
So let me copy and paste.

65
00:03:44,570 --> 00:03:45,280
Okay, Good.

66
00:03:46,100 --> 00:03:49,340
Then we want to go to context.

67
00:03:49,370 --> 00:03:51,860
And we want to import those two things.

68
00:03:51,860 --> 00:03:54,650
So let me first just grab this one over here.

69
00:03:55,870 --> 00:03:58,660
And then in the app context, copy and paste.

70
00:03:59,470 --> 00:04:00,310
Or here as well.

71
00:04:00,310 --> 00:04:02,450
And let's set it up as sex.

72
00:04:02,490 --> 00:04:03,280
Okay, good.

73
00:04:03,700 --> 00:04:07,510
And then we want to grab both of them in the reducer.

74
00:04:08,620 --> 00:04:09,910
Okay, awesome.

75
00:04:10,090 --> 00:04:15,440
And like I said, in the app context, we want to set up user loading.

76
00:04:15,460 --> 00:04:16,959
That's the first thing we want to do.

77
00:04:17,170 --> 00:04:24,100
And no, we don't want to use is loading, because keep in mind that we're using that is loading essentially

78
00:04:24,460 --> 00:04:26,490
in different places in our application.

79
00:04:26,500 --> 00:04:32,710
So if we'll add that one to the protected route, then we'll have that loading spinner essentially in

80
00:04:32,710 --> 00:04:33,460
front of.

81
00:04:34,430 --> 00:04:36,830
Every request, and that's not what we want to do.

82
00:04:36,830 --> 00:04:38,690
So let's go here with user loading.

83
00:04:38,690 --> 00:04:40,250
Again, this is very specific.

84
00:04:40,250 --> 00:04:43,400
This only going to run when we refresh the application.

85
00:04:43,400 --> 00:04:48,910
And yes, we want to set the default true, even though an reducer will still set it as true.

86
00:04:48,920 --> 00:04:49,580
Why?

87
00:04:49,610 --> 00:04:54,370
Because the code for dashboard, the protected route runs right away.

88
00:04:54,380 --> 00:04:59,240
So if this is going to be false and then while you're fetching your set at equal to true, it doesn't

89
00:04:59,240 --> 00:05:01,450
matter, you'll be already logged out.

90
00:05:01,460 --> 00:05:02,630
Hopefully that is clear.

91
00:05:02,630 --> 00:05:04,210
That's why it's very, very important.

92
00:05:04,220 --> 00:05:07,250
Set up new loading and set it equal to true.

93
00:05:07,280 --> 00:05:09,620
Yes, those are the two things we want to do.

94
00:05:09,710 --> 00:05:13,160
Then let's keep on moving and we want to invoke this.

95
00:05:14,160 --> 00:05:16,530
Essentially every time the application starts.

96
00:05:16,980 --> 00:05:18,090
Now, when is that?

97
00:05:18,090 --> 00:05:21,540
Well, when we have a use effect.

98
00:05:21,540 --> 00:05:22,230
Correct.

99
00:05:22,770 --> 00:05:24,150
Empty dependency, right.

100
00:05:24,180 --> 00:05:25,470
That's what we need to do.

101
00:05:25,590 --> 00:05:30,240
So first, let's just set up the function and let's right away invoke it and then we'll add the logic.

102
00:05:30,480 --> 00:05:33,360
Let's go get current user.

103
00:05:33,390 --> 00:05:37,560
This is going to be a sync function in here.

104
00:05:37,800 --> 00:05:38,820
Let's set up.

105
00:05:40,320 --> 00:05:41,230
The logic.

106
00:05:41,250 --> 00:05:42,840
So let me add the curlies.

107
00:05:43,380 --> 00:05:44,650
Eventually it's going to be there.

108
00:05:44,670 --> 00:05:52,320
For now, I want to go with use effect and I simply want to invoke it again when well, when the application

109
00:05:52,320 --> 00:05:53,040
starts.

110
00:05:53,280 --> 00:05:57,720
So essentially, every time we refresh, that's our goal over here.

111
00:05:57,870 --> 00:06:01,710
Let's go with get current user and let's invoke it again.

112
00:06:02,130 --> 00:06:07,830
This happens pretty much in our case when we refresh the application, this is what we're going to run.

113
00:06:08,110 --> 00:06:12,510
Now, as far as the logic, we want to dispatch first the begin.

114
00:06:13,080 --> 00:06:18,780
So let's set up this patch, let's say type and then get current user begin.

115
00:06:19,230 --> 00:06:20,820
Then let's go with that.

116
00:06:20,820 --> 00:06:22,320
Try and catch.

117
00:06:23,160 --> 00:06:24,600
And we want to go with counsel.

118
00:06:24,630 --> 00:06:26,770
I know that there's going to be data, of course.

119
00:06:26,790 --> 00:06:29,730
It is equal to a weight.

120
00:06:30,900 --> 00:06:32,890
And we're going to go with all the fetch.

121
00:06:32,910 --> 00:06:34,680
And what was the URL?

122
00:06:34,950 --> 00:06:38,610
Or we need to go with forward slash auth and then forward slash get.

123
00:06:39,620 --> 00:06:42,140
Current and user.

124
00:06:42,710 --> 00:06:45,200
Now what are we sending back from the server?

125
00:06:45,200 --> 00:06:48,320
We have const, user and location.

126
00:06:48,320 --> 00:06:53,690
So we structure both of these things out of the data and then of course we'll pass it down.

127
00:06:54,490 --> 00:06:55,870
To our reducers.

128
00:06:55,870 --> 00:06:57,190
So let's go with this patch.

129
00:06:57,700 --> 00:07:02,530
The type is going to be get user success in this case.

130
00:07:02,950 --> 00:07:05,310
And we want to go with comma payload.

131
00:07:05,320 --> 00:07:07,300
I'm sorry, not the package payload.

132
00:07:07,840 --> 00:07:09,970
And that will be equal to an object.

133
00:07:10,480 --> 00:07:16,000
And in here, we'll passing the user and location like so.

134
00:07:16,450 --> 00:07:17,260
Okay, good.

135
00:07:17,350 --> 00:07:24,850
And then, like I said, as far as the error, the heavy lifting is going to be done by the interceptor.

136
00:07:24,880 --> 00:07:28,110
We already have that code that checks for the 401.

137
00:07:28,120 --> 00:07:33,910
Because, again, we have only two options with this Route 200, which brings back all the user data

138
00:07:33,910 --> 00:07:35,050
or 401.

139
00:07:35,050 --> 00:07:35,670
That's it.

140
00:07:35,680 --> 00:07:41,380
But I'll still go the long route where I'll say error, but you don't have to error.

141
00:07:41,470 --> 00:07:46,640
Then response and then status you that is equal to 401.

142
00:07:46,660 --> 00:07:49,390
Then I'll simply return over here.

143
00:07:49,420 --> 00:07:50,320
I'll say return.

144
00:07:50,470 --> 00:07:54,640
And if let's say for some I don't know.

145
00:07:54,670 --> 00:07:56,560
We had reason we passed this.

146
00:07:57,030 --> 00:07:58,930
I'll still go with the log out user.

147
00:07:59,680 --> 00:08:00,340
Okay, good.

148
00:08:00,430 --> 00:08:04,300
So that pretty much completes the app context.

149
00:08:04,720 --> 00:08:05,620
And then.

150
00:08:06,970 --> 00:08:10,660
In the following video, we want to set up the reducer logic.

