1
00:00:00,570 --> 00:00:05,490
All right welcome to the final installment of this authentication code along all that we have left to

2
00:00:05,490 --> 00:00:11,520
do is to add in the log out functionality and then also we're going to add in a middleware called is

3
00:00:11,520 --> 00:00:14,610
logged in to check if the user is locked in or not.

4
00:00:14,610 --> 00:00:16,080
So we'll get to that in time.

5
00:00:16,080 --> 00:00:22,170
Let's start with log out so log out is much simpler than log in or register.

6
00:00:22,170 --> 00:00:23,490
We don't need to have two routes.

7
00:00:23,520 --> 00:00:24,600
We don't need a form.

8
00:00:24,750 --> 00:00:30,360
All that we want is a simple link and when we click on that link it should sign out or log out the current

9
00:00:30,360 --> 00:00:31,240
user.

10
00:00:31,710 --> 00:00:32,930
So let's start with the link.

11
00:00:33,000 --> 00:00:41,010
I'll go to the home page to begin an ad in a link to slash log out which doesn't exist and we'll just

12
00:00:41,010 --> 00:00:42,930
say log out.

13
00:00:42,950 --> 00:00:49,350
And let's also add in a link to log in since we haven't done that yet slash log in.

14
00:00:49,830 --> 00:00:57,900
And now let's copy this and let's put it on all the templates just so we have those three links on every

15
00:00:57,900 --> 00:00:58,630
page.

16
00:00:58,960 --> 00:01:00,000
OK.

17
00:01:00,390 --> 00:01:05,040
Now let's go back to our address and we need to handle the log out route.

18
00:01:05,280 --> 00:01:14,460
So we need to add in and after that get slash log out and we can start nice and simple by just sending

19
00:01:14,460 --> 00:01:16,190
something back.

20
00:01:16,300 --> 00:01:17,930
RAZ And.

21
00:01:18,250 --> 00:01:19,260
OK.

22
00:01:19,650 --> 00:01:23,010
I will log you out.

23
00:01:23,010 --> 00:01:26,380
Not yet and save.

24
00:01:26,490 --> 00:01:29,210
And we should see this when we click on that link.

25
00:01:29,520 --> 00:01:31,820
Let's start our server.

26
00:01:32,820 --> 00:01:39,240
Let's try and log out now refresh the page and it doesn't matter if for long been or not we click log

27
00:01:39,240 --> 00:01:40,260
out.

28
00:01:40,260 --> 00:01:42,860
We get that response which is what we want.

29
00:01:43,650 --> 00:01:49,340
Now what we need to do is actually log the user out which is really simple using Passport.

30
00:01:49,740 --> 00:01:55,290
It's a simple line request that log out that's all we need to do.

31
00:01:55,410 --> 00:01:59,640
And remember when we lock someone out we're not actually changing anything in the database.

32
00:01:59,640 --> 00:02:01,640
There's no transaction there.

33
00:02:01,650 --> 00:02:05,890
What's happening is that passport is destroying all the user data in the session.

34
00:02:05,910 --> 00:02:11,270
It's no longer keeping track of this user's data in the session from request to request.

35
00:02:11,280 --> 00:02:16,930
So if we save and then we also want something to happen at the end like a redirect.

36
00:02:17,100 --> 00:02:19,090
So let's redirect to the home page.

37
00:02:19,180 --> 00:02:20,640
This redirect to slash.

38
00:02:20,730 --> 00:02:27,430
Once you've been logged out OK let's restart the server and just give it a shot.

39
00:02:27,690 --> 00:02:34,710
So I'll refresh I'll start by logging in and I'll log in as Colt with the account I created with my

40
00:02:34,710 --> 00:02:36,950
password as password.

41
00:02:36,950 --> 00:02:39,400
I log in I get to the secret page.

42
00:02:39,420 --> 00:02:45,390
Now let's click log out and it takes him back to the home page and that's it.

43
00:02:45,390 --> 00:02:46,940
So log it's pretty simple.

44
00:02:46,980 --> 00:02:48,270
We don't need any of the forms.

45
00:02:48,270 --> 00:02:54,030
It's a single route slash log out and all that we do every time is request out log out.

46
00:02:54,150 --> 00:02:55,860
And that takes care of everything for us.

47
00:02:55,860 --> 00:03:00,830
That's coming from passport in the unit where we create our log in from scratch.

48
00:03:00,930 --> 00:03:02,930
We'll actually be writing the method log out.

49
00:03:03,030 --> 00:03:04,460
We'll be writing authenticate.

50
00:03:04,590 --> 00:03:06,020
We'll be writing register.

51
00:03:06,180 --> 00:03:10,430
But for now passport makes it super easy for us.

52
00:03:10,590 --> 00:03:15,940
We do have a problem though which is that even though I'm logged out I just quit to log out.

53
00:03:16,020 --> 00:03:19,470
I can still go to slash secret.

54
00:03:19,470 --> 00:03:21,380
There's nothing stopping me from getting there.

55
00:03:21,720 --> 00:03:25,870
So our code right now sends us to slash secret when we sign up.

56
00:03:25,890 --> 00:03:30,870
And when we'd like again but I can still go there even if I'm not signed in.

57
00:03:30,960 --> 00:03:35,480
Log out and I can still go to slash secret to fix that.

58
00:03:35,580 --> 00:03:42,030
We need to add in a middleware in what we want to do is on this secret route before we do anything.

59
00:03:42,030 --> 00:03:46,770
We're going to check if the user is logged in or not if the user is logged in.

60
00:03:46,920 --> 00:03:53,250
Then will render this template if the user is not logged in then will redirect them to slash logon.

61
00:03:53,960 --> 00:03:56,640
And the way that we do that is by writing a middleware.

62
00:03:57,180 --> 00:04:01,920
So we're going to define a function down at the bottom here and this function is going to be called

63
00:04:01,980 --> 00:04:05,490
is logged in.

64
00:04:05,490 --> 00:04:10,890
And it takes three parameters request response and next.

65
00:04:11,220 --> 00:04:15,740
And this is standard for middleware request refers to the request object.

66
00:04:15,900 --> 00:04:17,760
Response is the response object.

67
00:04:17,910 --> 00:04:21,270
And next is actually the next thing that needs to be called.

68
00:04:21,570 --> 00:04:25,830
And we don't have to set any of this up just by adding it in as a middleware which we'll do in just

69
00:04:25,830 --> 00:04:26,580
a moment.

70
00:04:26,670 --> 00:04:30,270
Express takes care of it and it knows what function to call next.

71
00:04:30,270 --> 00:04:32,540
But we'll start by defining the functionality here.

72
00:04:32,670 --> 00:04:40,320
So is logged in we're going to check if a request is authenticated which is something that comes with

73
00:04:40,320 --> 00:04:43,200
passport as well make sure we spelled that right.

74
00:04:43,240 --> 00:04:45,440
Is authenticated.

75
00:04:46,260 --> 00:04:50,660
Then we're going to return next which is a little bit weird looking.

76
00:04:51,330 --> 00:04:52,740
And then I'll come back to that.

77
00:04:52,770 --> 00:04:56,970
Otherwise what we'll do and we don't need an else because we're returning here.

78
00:04:56,970 --> 00:05:05,730
So what we'll do is redstart redirect to the log in form slash log in and then we'll add this as a middleware

79
00:05:06,300 --> 00:05:11,500
is logged in to our slash secret route which is right here.

80
00:05:11,730 --> 00:05:17,190
And it's really simple we just have to add is logged in right here.

81
00:05:17,700 --> 00:05:19,500
And then a comma.

82
00:05:19,500 --> 00:05:25,530
So what this will do is when a request comes in a GET request to slash secret it's going to run this

83
00:05:25,650 --> 00:05:27,720
before it does anything else.

84
00:05:27,740 --> 00:05:31,800
It's going to run is logged in which she'll do this.

85
00:05:31,800 --> 00:05:39,090
It's going to check is the request authenticated if it is return next and run next.

86
00:05:39,150 --> 00:05:40,630
That's what the parentheses here are.

87
00:05:40,980 --> 00:05:44,540
And next it's just going to refer to this right here.

88
00:05:44,760 --> 00:05:48,780
So before we render secret we want to check if the users logged in.

89
00:05:48,780 --> 00:05:50,520
Again that's what we're doing.

90
00:05:50,520 --> 00:05:51,860
If the user logged in.

91
00:05:52,110 --> 00:05:53,670
Basically keep going.

92
00:05:53,670 --> 00:05:55,450
Move along you're fine.

93
00:05:55,610 --> 00:06:01,140
Otherwise if the user is not authenticated then we have a problem and we're going to redirect you back

94
00:06:01,140 --> 00:06:02,420
to slash log in.

95
00:06:02,830 --> 00:06:05,040
And this other code will never run.

96
00:06:05,670 --> 00:06:09,260
So if we redirect inside of is logged in we're done.

97
00:06:09,570 --> 00:06:10,890
And this doesn't happen.

98
00:06:11,340 --> 00:06:14,850
OK let's save restart the server.

99
00:06:15,300 --> 00:06:17,320
Make sure we don't have any syntax errors.

100
00:06:17,700 --> 00:06:22,010
Let's go back and let's start on the root page.

101
00:06:22,020 --> 00:06:28,680
Make sure I'm not logged in and now I'll try and go to slash secret and it takes me to slash log in

102
00:06:28,680 --> 00:06:30,410
automatically.

103
00:06:30,420 --> 00:06:31,530
See that.

104
00:06:31,530 --> 00:06:35,030
Now it's loggin colt with my password.

105
00:06:35,030 --> 00:06:38,370
You called the password log in.

106
00:06:38,370 --> 00:06:40,920
Now I can go to the secret page.

107
00:06:40,980 --> 00:06:48,120
Now let's go back to the root and try to go to secret again and it works because I'm logged in so I

108
00:06:48,120 --> 00:06:49,390
can go back and forth.

109
00:06:49,470 --> 00:06:54,490
I can go to whatever routes I want sign up and then go back to secret.

110
00:06:54,870 --> 00:07:01,850
But then as soon as i log out and I try to go back to secret it doesn't work anymore.

111
00:07:02,730 --> 00:07:04,750
Let's just double check that or sign up works.

112
00:07:04,860 --> 00:07:13,530
Let's make a new account called Tator and password will also be password and let's submit that makes

113
00:07:13,530 --> 00:07:14,460
us a new account.

114
00:07:14,460 --> 00:07:17,850
I can now visit secret and then I can log out.

115
00:07:18,540 --> 00:07:23,240
And that destroys the session and I can no longer go to secret.

116
00:07:23,250 --> 00:07:23,750
All right.

117
00:07:24,060 --> 00:07:28,230
So we've covered a lot in this series five different parts to create this user auth.

118
00:07:28,470 --> 00:07:32,100
And this is even using those tools that I said would help us out.

119
00:07:32,130 --> 00:07:36,090
It's definitely a bit of an undertaking but it's obviously worth it.

120
00:07:36,090 --> 00:07:41,910
Most web apps really benefit from having in because as we'll see and Yelp camp our next video we'll

121
00:07:41,910 --> 00:07:43,020
be able to add log in.

122
00:07:43,020 --> 00:07:49,280
And it's not just to put stuff behind a loggin wall but we can actually associate data with a user.

123
00:07:49,290 --> 00:07:53,210
Right now we just have the secrets page hidden unless you're logged in.

124
00:07:53,580 --> 00:07:59,360
But no camp will actually associate specific comments with a user so that we can see who exactly made

125
00:07:59,360 --> 00:08:00,500
a comment.

126
00:08:01,230 --> 00:08:06,900
So before I sign off here let's go back and just do a quick recap of the most important part which is

127
00:08:06,900 --> 00:08:10,470
our middleware so is logged in is a function we defined.

128
00:08:10,860 --> 00:08:15,600
And we can define as many middleware as we want and they all take these three parameters the request

129
00:08:15,690 --> 00:08:18,310
the response and the next function.

130
00:08:18,570 --> 00:08:24,280
And then if things are fine if we want to move on to the next middleware we just call next.

131
00:08:24,480 --> 00:08:25,620
Just like that.

132
00:08:25,800 --> 00:08:32,310
And in our case if things are not fine if the request is not authenticated that returns false then we

133
00:08:32,310 --> 00:08:35,050
short circuit and redirect to slash like it.

134
00:08:35,440 --> 00:08:36,050
All right.

135
00:08:36,120 --> 00:08:38,640
Hopefully you enjoy learning about user authentication.

136
00:08:38,660 --> 00:08:39,680
I'll see you in the next video
