1
00:00:00,240 --> 00:00:00,540
Great.

2
00:00:00,570 --> 00:00:08,910
And up next, I want to set up a function in the utils that automatically attaches the cookie to our

3
00:00:08,910 --> 00:00:09,390
response.

4
00:00:09,420 --> 00:00:09,870
Why?

5
00:00:10,320 --> 00:00:12,840
Well, because we'll have to use it in three places.

6
00:00:13,080 --> 00:00:14,220
We'll attach Cookie.

7
00:00:15,010 --> 00:00:19,840
When we register login and also update user.

8
00:00:19,870 --> 00:00:23,410
Now can you let's say copy and paste this logic?

9
00:00:23,440 --> 00:00:24,190
Sure.

10
00:00:24,670 --> 00:00:30,340
But since we're repeating, we might as well set up the function in the utils.

11
00:00:30,370 --> 00:00:32,159
So first let's go to utils.

12
00:00:32,170 --> 00:00:33,520
Let's create a new file.

13
00:00:33,520 --> 00:00:37,180
In my case, that is going to be a Dutch cookie.

14
00:00:38,390 --> 00:00:40,350
That's Cookie over here.

15
00:00:40,390 --> 00:00:44,960
JS And as I say, not I didn't set up the proper extension here.

16
00:00:44,960 --> 00:00:45,320
So.

17
00:00:45,320 --> 00:00:45,920
James.

18
00:00:46,370 --> 00:00:51,650
And then what's interesting, we need to pass in the response.

19
00:00:51,680 --> 00:00:58,070
Now we don't have to return it from this utils function, but we do need to pass in response from the

20
00:00:58,070 --> 00:00:58,850
controller.

21
00:00:59,420 --> 00:01:07,400
So that way we have access to it and then we automatically add to that particular response and why we're

22
00:01:07,400 --> 00:01:07,810
doing that?

23
00:01:07,820 --> 00:01:10,520
Well, because we'll call this attach cookie.

24
00:01:11,520 --> 00:01:14,010
In the controllers and notice we'll pass through things.

25
00:01:14,010 --> 00:01:20,670
We'll pass in the response that we want to send back to that particular URL.

26
00:01:20,670 --> 00:01:23,100
And also we pass in the token.

27
00:01:23,100 --> 00:01:25,980
So those are the two things that will pass into function.

28
00:01:26,220 --> 00:01:32,610
And then in the function, we're pretty much repeat the functionality we set up in the last video.

29
00:01:33,530 --> 00:01:38,900
The difference is that now we need to pass both of these things as a parameter, and it's always my

30
00:01:38,900 --> 00:01:40,550
preference if I have multiple.

31
00:01:40,640 --> 00:01:46,910
When I basically set up the object so that way I don't need to worry about the order of those parameters.

32
00:01:47,060 --> 00:01:49,400
So let's go to our controller.

33
00:01:49,700 --> 00:01:56,180
And in order to speed this up, I'll just grab all of this code like so that in the attached cookies,

34
00:01:56,180 --> 00:01:57,650
let's set up the function.

35
00:01:57,950 --> 00:02:00,620
And we're looking for here.

36
00:02:01,130 --> 00:02:03,860
And I'll write away export as default.

37
00:02:03,860 --> 00:02:06,500
Since knowing me, I'll forget about that.

38
00:02:06,500 --> 00:02:10,009
And then I'll run around like a headless chicken looking for the error.

39
00:02:10,130 --> 00:02:11,170
Copy and paste.

40
00:02:11,180 --> 00:02:12,680
So we create one day.

41
00:02:12,920 --> 00:02:13,970
That's beautiful.

42
00:02:14,720 --> 00:02:16,490
And now let's set up those parameters.

43
00:02:16,640 --> 00:02:20,510
First, we want access to response and second to the token.

44
00:02:20,510 --> 00:02:24,480
And yes, we don't need to return this restart cookie.

45
00:02:24,500 --> 00:02:27,680
It's automatically added to our response.

46
00:02:28,220 --> 00:02:33,980
And we have processed data entry that looks about right, so everything else can stay.

47
00:02:33,980 --> 00:02:35,450
We just want to go to.

48
00:02:36,150 --> 00:02:38,640
Fourth controller we want to import.

49
00:02:39,390 --> 00:02:42,880
So in here, we're looking for that attached cookie.

50
00:02:42,900 --> 00:02:45,030
Since I'm exporting as default.

51
00:02:46,380 --> 00:02:47,010
I can go.

52
00:02:47,010 --> 00:02:47,610
Just.

53
00:02:48,460 --> 00:02:49,390
Patch cookies.

54
00:02:49,510 --> 00:02:52,480
Remember you need to add this JS.

55
00:02:53,470 --> 00:03:01,930
And then where we have a register, basically right after the token, we want to go with attach cookies

56
00:03:02,140 --> 00:03:07,000
and we want to go with ROAS and we also want to pass in the token.

57
00:03:07,180 --> 00:03:10,120
And we want to do that in login as well.

58
00:03:10,740 --> 00:03:14,130
So let's find where is our login.

59
00:03:14,760 --> 00:03:16,940
Over here again, invoke it.

60
00:03:16,950 --> 00:03:18,880
And the same goes for update user.

61
00:03:18,900 --> 00:03:20,670
Since we create that token.

62
00:03:20,670 --> 00:03:21,260
Correct.

63
00:03:21,280 --> 00:03:23,100
And let's invoke that.

64
00:03:23,430 --> 00:03:27,090
And I mean, it doesn't really matter.

65
00:03:27,750 --> 00:03:29,970
Let's test out the register.

66
00:03:29,970 --> 00:03:30,380
Maybe.

67
00:03:31,640 --> 00:03:32,750
So let me go here.

68
00:03:33,380 --> 00:03:34,820
Let me log out.

69
00:03:35,000 --> 00:03:40,160
And then instead of the log in one, let's go with the register.

70
00:03:40,550 --> 00:03:42,770
So I'm going to create some dummy user here.

71
00:03:43,880 --> 00:03:46,250
Let's say that it's going to be Susan.

72
00:03:47,230 --> 00:03:48,550
Sure's and art.

73
00:03:48,580 --> 00:03:50,560
Hopefully it's not taken already.

74
00:03:51,900 --> 00:03:53,030
And let's see.

75
00:03:53,040 --> 00:03:53,830
Submit.

76
00:03:53,850 --> 00:03:54,360
Yep.

77
00:03:54,390 --> 00:03:55,620
User created.

78
00:03:55,680 --> 00:03:58,000
And then if we take a look.

79
00:03:58,020 --> 00:03:59,920
So I have no jobs, Nothing like that.

80
00:03:59,970 --> 00:04:02,330
But if I take a look at the application.

81
00:04:02,340 --> 00:04:02,940
Yep.

82
00:04:03,060 --> 00:04:04,260
The token is present.

83
00:04:04,260 --> 00:04:08,940
And again, we can test out all of those routes in the postman.

84
00:04:08,940 --> 00:04:13,540
But just to save some time, I'll just do it with register.

85
00:04:13,560 --> 00:04:15,720
Again, the end result is following.

86
00:04:16,370 --> 00:04:18,310
Where you know all of these roots.

87
00:04:18,320 --> 00:04:22,340
Now we attach that cookie to the response.

88
00:04:22,340 --> 00:04:28,760
And then from that point on, every time the browser is going to make a request back to the server,

89
00:04:28,760 --> 00:04:37,040
it will automatically include that token cookie in the request, which is again, really, really,

90
00:04:37,040 --> 00:04:44,570
really, really, really cool because you'll see that it just allows us to write less code on our frontend.

