1
00:00:00,660 --> 00:00:05,850
We can successfully send the cookies as well as pass them on our server.

2
00:00:06,240 --> 00:00:12,060
So now let's refactor the code, and the way we'll do that will set up a function.

3
00:00:12,360 --> 00:00:19,680
That name is up to you, but I'll use batch cookies to response function in the Utils and JWT.

4
00:00:20,130 --> 00:00:25,110
If you want, you can create a separate file, but in my case, all the same because I want to use this

5
00:00:25,110 --> 00:00:25,530
function.

6
00:00:26,250 --> 00:00:28,500
JWT in there.

7
00:00:28,890 --> 00:00:33,480
What I want you to do is to accept the payload as an argument.

8
00:00:33,690 --> 00:00:39,480
So we'll be looking for sponsors and token user when we want to create a token.

9
00:00:40,020 --> 00:00:41,430
And then you have two options.

10
00:00:41,730 --> 00:00:47,850
Either we can send directly a response or there's another way out we can do that.

11
00:00:48,000 --> 00:00:52,060
So positive video try to get something.

12
00:00:52,080 --> 00:00:55,320
And once you're done, just resume the video.

13
00:00:55,920 --> 00:01:03,630
So first, what I want to do is go back to JWT and I'm going to create that function or say that there's

14
00:01:03,670 --> 00:01:05,670
going to be a function by the name of attach.

15
00:01:06,740 --> 00:01:07,370
Cookies.

16
00:01:08,420 --> 00:01:09,530
Two response.

17
00:01:09,860 --> 00:01:17,080
The reason why I'm calling this cookie is not cookie is because later when we take a look at a refreshed

18
00:01:17,090 --> 00:01:22,430
token functionality, there's going to be two cookies that will add over here again.

19
00:01:22,670 --> 00:01:25,220
You can name it differently, but in my case, I'm going to go with this one.

20
00:01:25,830 --> 00:01:28,880
And there, like I said, I'm going to be looking for two things.

21
00:01:29,300 --> 00:01:32,780
First, since will be attaching that cookie to the response.

22
00:01:32,780 --> 00:01:36,890
We want to get the response object that is coming from the controller.

23
00:01:37,250 --> 00:01:39,770
And the second one will be that user.

24
00:01:40,220 --> 00:01:41,060
Remember that guy?

25
00:01:42,080 --> 00:01:44,210
We're here to the we created the token user.

26
00:01:44,580 --> 00:01:49,030
That's the second thing, but I'm looking for in this function now in the function body.

27
00:01:49,040 --> 00:01:51,290
First, I want to create the token.

28
00:01:51,770 --> 00:01:58,070
I want to create a token by running on token is equal to create JWT.

29
00:01:58,430 --> 00:02:02,360
And as far as the payload, well, we'll just pass in the user, correct?

30
00:02:02,810 --> 00:02:09,259
Let's say that whatever we're passing in as a user, we'll just set it up as a payload.

31
00:02:09,690 --> 00:02:10,940
So that's step number one.

32
00:02:11,300 --> 00:02:18,620
Then once we create, we basically want to set up that cookie so I can take this one and remove the

33
00:02:18,620 --> 00:02:18,980
code.

34
00:02:19,190 --> 00:02:23,540
We won't use this one anymore, and then I'll remove these lines of code as well.

35
00:02:23,570 --> 00:02:28,700
Now these ones, I'll just cut it out because we've already used them here in the attached cookie.

36
00:02:29,210 --> 00:02:30,560
So in the same function?

37
00:02:30,770 --> 00:02:31,640
Copy and paste.

38
00:02:32,060 --> 00:02:38,870
Since I'm going to be passing the response object, I'll have access to it and I'll say right that Cookie

39
00:02:39,350 --> 00:02:45,050
Sam, the token token expires and all that and we'll just leave here as one day.

40
00:02:45,560 --> 00:02:50,270
Now let me set up the importing a lot and then I'll talk about the responses.

41
00:02:50,630 --> 00:02:52,370
Basically, what are our options?

42
00:02:52,730 --> 00:02:53,900
So let me send it here.

43
00:02:53,900 --> 00:02:54,500
Or I'm sorry.

44
00:02:54,530 --> 00:02:58,250
Let me save it here, then attach cookies to response.

45
00:02:58,790 --> 00:03:01,640
Then let's go back to next year.

46
00:03:02,000 --> 00:03:09,230
Let's add it, and let's just say that we're looking for a third function that at the response that

47
00:03:09,450 --> 00:03:10,290
gets the response.

48
00:03:10,330 --> 00:03:10,970
OK, awesome.

49
00:03:11,660 --> 00:03:18,110
Then back in the off controller, I don't need any of these runs and just say attach cookies to your

50
00:03:18,110 --> 00:03:18,740
response.

51
00:03:19,280 --> 00:03:22,640
When I want to invoke my function, attach cookies to response.

52
00:03:23,000 --> 00:03:24,830
And here I want to pass into your things.

53
00:03:24,830 --> 00:03:29,330
A response and the user will be my token user.

54
00:03:29,930 --> 00:03:33,260
And for time being, let me just comment this one out.

55
00:03:34,010 --> 00:03:42,410
And then back in the JWT in the attached cookies response versus the first option we have where I can

56
00:03:42,410 --> 00:03:48,500
literally go with the response because I have access to the object correct, so I can go here with a

57
00:03:48,500 --> 00:03:53,150
response and then send you whatever we wrote over here.

58
00:03:53,510 --> 00:03:58,070
Now I'm not going to have access to the status code unless I import the library.

59
00:03:58,340 --> 00:03:59,240
Please keep that in mind.

60
00:03:59,270 --> 00:04:01,720
That's where I will heart code in this case.

61
00:04:01,730 --> 00:04:03,710
So let me take this line of code.

62
00:04:04,710 --> 00:04:09,300
Copy and paste now still attaching the token here.

63
00:04:09,870 --> 00:04:16,200
I'm just going to go where the user is equal to user whatever we're passing it, and now it's this throughout,

64
00:04:16,470 --> 00:04:21,149
whether everything works, whether our refactor is working.

65
00:04:21,570 --> 00:04:26,640
So I'm going to navigate through the postman here, but just try where the registered user.

66
00:04:27,590 --> 00:04:28,670
And call this, Peter.

67
00:04:30,140 --> 00:04:37,640
Peter and Peter and I should get back to Cookie, so status quo does not define, of course, because

68
00:04:37,640 --> 00:04:39,590
I told you that we don't need to do that, but.

69
00:04:40,580 --> 00:04:42,800
Getting the error, so let me go here with your one.

70
00:04:42,900 --> 00:04:43,850
Like I said, I will not go.

71
00:04:43,880 --> 00:04:45,620
That's the first option that you can take.

72
00:04:45,980 --> 00:04:47,750
Let me try one more time, a different one.

73
00:04:48,350 --> 00:04:49,340
Let's say, Anna.

74
00:04:50,550 --> 00:04:52,670
And Anna, all right, here.

75
00:04:52,990 --> 00:04:56,270
But Senate and now I give back the response, awesome.

76
00:04:56,390 --> 00:04:57,530
I have my user.

77
00:04:58,070 --> 00:05:04,580
And also, if I take a look at the cookies, well, where it is, I have my token, which is just awesome.

78
00:05:04,940 --> 00:05:11,450
So if I'm going to go back to your my home route, the best thing one, if I'll send it and if I'll

79
00:05:11,450 --> 00:05:15,050
take a look at the server, I should see the response.

80
00:05:15,050 --> 00:05:21,140
So that's the first way how we can solve this, where essentially we pass in the response and we're

81
00:05:21,140 --> 00:05:21,680
good to go.

82
00:05:22,100 --> 00:05:31,070
Now, later, when we work on refresh tokens, we'll have to call this in multiple places and in there.

83
00:05:31,070 --> 00:05:34,490
We won't have this option where I can just send back the response directly.

84
00:05:34,500 --> 00:05:40,110
So even though it's going to work right now, later, we just want to attach it to your response.

85
00:05:40,130 --> 00:05:41,040
How is that going to look like?

86
00:05:41,060 --> 00:05:44,030
Well, what we can do is just remove this line of code.

87
00:05:44,210 --> 00:05:46,340
So I don't want some response here.

88
00:05:46,730 --> 00:05:50,630
I'll say it and back in the off controller.

89
00:05:51,020 --> 00:05:52,610
I can just go back to my response.

90
00:05:53,030 --> 00:06:02,240
So only thing that this function does, it just attaches the cookie and later cookies to our response.

91
00:06:02,370 --> 00:06:07,700
That said, we can still set up the response separately and again if this doesn't make any sense right

92
00:06:07,700 --> 00:06:08,030
now.

93
00:06:08,390 --> 00:06:14,540
Trust me, when we start talking about refresh tokens and all that cool stuff, it will make all the

94
00:06:14,540 --> 00:06:16,250
sense in the world why we're doing that.

95
00:06:16,250 --> 00:06:18,470
Because again, we want to call this in multiple places.

96
00:06:18,470 --> 00:06:24,740
And even though in this case, we could have got away with the fact that we're just sending back the

97
00:06:24,740 --> 00:06:27,130
response here, it's not going to be an option later.

98
00:06:27,560 --> 00:06:32,510
And what's really cool that we're still attaching this cookie to our response.

99
00:06:32,520 --> 00:06:40,880
So if I take a look at my postman and register one more user may go back to David.

100
00:06:40,920 --> 00:06:41,360
Maybe.

101
00:06:42,420 --> 00:06:43,560
I'll try with David.

102
00:06:43,920 --> 00:06:46,320
Let's send a get back to Cookie.

103
00:06:47,240 --> 00:06:53,320
And then in my testing out, once I send, I'll also have access to the token.

104
00:06:54,080 --> 00:06:54,460
Awesome.

105
00:06:54,800 --> 00:06:56,990
We successfully refactor the code.

106
00:06:57,260 --> 00:07:00,080
So now let's talk about the signed and secure flag.

