1
00:00:00,300 --> 00:00:01,170
Not bad and bad.

2
00:00:01,380 --> 00:00:09,120
We successfully delivered our JWT in a cookie, and before we refactor our code, let's also cover how

3
00:00:09,120 --> 00:00:11,640
we can access cookies on the server.

4
00:00:12,240 --> 00:00:16,590
So now I'm talking about accessing the cookies coming back from the browser.

5
00:00:17,280 --> 00:00:24,420
And also, I want to test whether the frontend really attached to the cookies to all of the upcoming

6
00:00:24,420 --> 00:00:24,960
requests.

7
00:00:25,260 --> 00:00:29,010
Otherwise, I'm full of baloney because this is exactly what I've been telling you.

8
00:00:29,640 --> 00:00:36,480
And let's start by just accessing the cookies on a server you see as far as adding them to the response.

9
00:00:37,110 --> 00:00:38,040
Very straightforward.

10
00:00:38,640 --> 00:00:39,900
It's built in to express.

11
00:00:39,900 --> 00:00:44,490
We just go with the rest that cookie name value and then the options.

12
00:00:44,910 --> 00:00:51,870
Now, as far as access, we will have to install extra package like always packages already installed

13
00:00:51,870 --> 00:00:52,860
in the starter.

14
00:00:53,400 --> 00:00:59,340
And the package that you're looking for is this one, the cookie parser.

15
00:00:59,820 --> 00:01:04,410
So you need to go with npm install for your own project and then in the app.

16
00:01:04,500 --> 00:01:05,040
Yes.

17
00:01:05,430 --> 00:01:10,200
First, we want to require and I'm going to go right after the Morgan, so consider.

18
00:01:11,100 --> 00:01:13,950
Cookie and passer.

19
00:01:15,210 --> 00:01:16,380
That's going to be my variable.

20
00:01:16,580 --> 00:01:23,940
We may look for in a package cookie parser here, and then I'm going to place it right off there, Jason,

21
00:01:24,090 --> 00:01:24,670
where I go.

22
00:01:25,260 --> 00:01:29,190
I'm not yours and I just invoke the cookie parser.

23
00:01:29,790 --> 00:01:32,820
Let's invoke it, and then I'll set up a test drug.

24
00:01:33,360 --> 00:01:38,640
Just so we can see that we can access cookies coming back from the browser.

25
00:01:39,710 --> 00:01:43,640
And I think the easiest one is just going to be a homepage.

26
00:01:43,820 --> 00:01:50,180
Now, of course, eventually we will set it up the whole procedure in the authentication middleware

27
00:01:50,570 --> 00:01:55,460
because we'll have certain routes that will have restricted access.

28
00:01:55,880 --> 00:01:58,970
And that's where we'll implement that functionality for now.

29
00:01:59,510 --> 00:02:01,490
I just want to showcase how it's going to work.

30
00:02:02,120 --> 00:02:05,720
So in the home route, I simply want to log a cookie.

31
00:02:06,290 --> 00:02:08,840
So let's go here with long and then Eric.

32
00:02:09,320 --> 00:02:15,710
And once we add the middleware, basically once we add the package, we're going to be available on

33
00:02:15,980 --> 00:02:19,250
rack and cookies like show.

34
00:02:19,610 --> 00:02:21,020
So let's say it here.

35
00:02:21,880 --> 00:02:27,070
And just to stay on the safe side, I will restart the server, you know, I don't think it should matter.

36
00:02:27,610 --> 00:02:32,980
And now let's go back to our browser basically our post not correct.

37
00:02:33,430 --> 00:02:34,570
We have the cookie.

38
00:02:34,990 --> 00:02:40,690
So let's set up a dummy or out to our homepage, our move that route, but just for kicks, what's created

39
00:02:40,690 --> 00:02:40,960
here?

40
00:02:41,530 --> 00:02:43,980
So let's go to tabs.

41
00:02:44,050 --> 00:02:51,010
Let's grab a new one and say the URL here, and I believe my one was through the API.

42
00:02:51,280 --> 00:02:52,030
If I'm correct.

43
00:02:52,870 --> 00:02:55,930
Let me double check, so this one is API version one.

44
00:02:56,290 --> 00:02:57,640
So why don't we do this?

45
00:02:58,210 --> 00:03:03,010
I'll leave the string one here or move it, and let's copy and paste.

46
00:03:03,370 --> 00:03:11,290
I just want to keep that base from there on Page one and let just go to API and then version one and

47
00:03:11,290 --> 00:03:12,700
then log the cookies.

48
00:03:12,710 --> 00:03:16,420
Sorry about the detour, and then let's send it.

49
00:03:16,600 --> 00:03:17,770
Let's see what happens.

50
00:03:17,980 --> 00:03:19,120
So we get back.

51
00:03:19,160 --> 00:03:20,290
E-commerce API.

52
00:03:20,590 --> 00:03:21,070
OK.

53
00:03:21,550 --> 00:03:22,600
That's an awesome stock.

54
00:03:22,630 --> 00:03:25,480
Now what I'm really interested is voice.

55
00:03:26,200 --> 00:03:27,880
This is what I wanted to show you.

56
00:03:28,390 --> 00:03:29,440
So now check it out.

57
00:03:29,560 --> 00:03:37,450
Once we had the middleware of the cookie parser, every time the browser is going to be sending the

58
00:03:37,450 --> 00:03:42,880
request with the cookie, we'll have access to it in rec that cookies.

59
00:03:43,120 --> 00:03:44,230
Now what we can do then?

60
00:03:44,740 --> 00:03:50,200
Well, in the middle, where what will do we use our function will validate what our token is valid

61
00:03:50,590 --> 00:03:52,270
and all that cool stuff.

62
00:03:52,840 --> 00:03:58,930
So hopefully this proves both points where first, in order to access those cookies, we need to install

63
00:03:58,930 --> 00:04:01,570
middleware by the name of cookie parser.

64
00:04:01,780 --> 00:04:02,950
So extra package.

65
00:04:03,460 --> 00:04:12,130
And second, once we make that first request, once I get back that cookie like we did in the previous

66
00:04:12,130 --> 00:04:19,450
video, then with each request, the browser will send that cookie.

67
00:04:19,510 --> 00:04:20,890
And this is very, very important.

68
00:04:20,890 --> 00:04:27,430
And what that means, whether that is in the postman or whether that is going to be a front end application,

69
00:04:27,760 --> 00:04:31,510
you literally don't need to do anything once you get that cookie.

70
00:04:31,870 --> 00:04:37,870
That said, browser will automatically pick it up and send it back to the server.

71
00:04:38,140 --> 00:04:39,760
So let me try one more time.

72
00:04:40,450 --> 00:04:46,630
In this case, I remove the cookie, and as a side note, I removed my user so I can start right from

73
00:04:46,690 --> 00:04:47,140
scratch.

74
00:04:47,770 --> 00:04:52,900
And now let's send it getting back the cookie, and now let me remove it manually in the postman.

75
00:04:53,560 --> 00:04:54,450
So I have the cookie.

76
00:04:54,490 --> 00:05:00,100
I can clearly see that I have it, but let's say that I go here and I remove that token so that one

77
00:05:00,100 --> 00:05:00,820
doesn't exist.

78
00:05:01,210 --> 00:05:04,490
So now, once I make that request, I shouldn't.

79
00:05:04,510 --> 00:05:06,520
And by the way, let me double check.

80
00:05:06,820 --> 00:05:08,890
I shouldn't get back into think.

81
00:05:08,900 --> 00:05:13,120
Yeah, and I don't notice now the object is null and all that.

82
00:05:13,840 --> 00:05:15,160
So that just proves the point.

83
00:05:15,170 --> 00:05:18,940
If we're successful, if we actually get back.

84
00:05:20,170 --> 00:05:23,140
Yep, everything is going to be sent to us, blah blah blah, blah blah.

85
00:05:23,410 --> 00:05:26,680
Now this already says that user exists, or it may go with Susan.

86
00:05:27,800 --> 00:05:34,820
And Susan, here, and we'll get back to key, and then if I go to my dumb ear out and if I send it

87
00:05:35,240 --> 00:05:36,710
now, we're going to have the token.

88
00:05:37,020 --> 00:05:42,950
And very, very important and very, very cool as far as cookies that we really don't need to do any

89
00:05:42,950 --> 00:05:45,530
acrobatics on the front end once we send it.

90
00:05:45,740 --> 00:05:47,870
Basically, browser does the rest.

