1
1

00:00:00,680  -->  00:00:01,580
<v Jose>Hi and welcome back.</v>
2

2

00:00:01,580  -->  00:00:02,810
In this video we're going to be
3

3

00:00:02,810  -->  00:00:05,750
talking about logging in users.
4

4

00:00:05,750  -->  00:00:09,520
We will create a new template inside the users folder,
5

5

00:00:09,520  -->  00:00:13,230
called login dot html and it's going to be
6

6

00:00:13,230  -->  00:00:16,080
basically the same as a register dot html
7

7

00:00:16,080  -->  00:00:18,330
because we still need the email and password
8

8

00:00:18,330  -->  00:00:20,993
in order to actually log in.
9

9

00:00:22,650  -->  00:00:25,290
I'm gonna call this login at the top,
10

10

00:00:25,290  -->  00:00:30,290
login form and login form, there we go,
11

11

00:00:31,750  -->  00:00:36,750
like this and the rest is going to stay the same.
12

12

00:00:37,540  -->  00:00:40,850
So we have our email, and our password
13

13

00:00:40,850  -->  00:00:42,410
and then down here instead of sign up
14

14

00:00:42,410  -->  00:00:47,010
we're gonna say login and then we have our login form.
15

15

00:00:47,010  -->  00:00:51,140
Now in the users view, we're going
16

16

00:00:51,140  -->  00:00:53,050
to be adding a new Enpoint.
17

17

00:00:53,050  -->  00:00:55,090
This Enpoint is going to once again deal
18

18

00:00:55,090  -->  00:00:57,310
with the email and password but this time
19

19

00:00:57,310  -->  00:01:00,180
instead of registering users it's going to check
20

20

00:01:00,180  -->  00:01:03,600
whether the details are correct and if they are,
21

21

00:01:03,600  -->  00:01:06,600
it's just going to populate the session email.
22

22

00:01:06,600  -->  00:01:08,540
If you remember from a previous video,
23

23

00:01:08,540  -->  00:01:11,020
session email is what keeps tract of
24

24

00:01:11,020  -->  00:01:13,020
whether a user has logged in or not.
25

25

00:01:13,020  -->  00:01:16,090
When a browser can send us some data,
26

26

00:01:16,090  -->  00:01:17,970
we will match that data from
27

27

00:01:17,970  -->  00:01:21,360
the cookie with a session in our app.
28

28

00:01:21,360  -->  00:01:24,030
If the session contains an email,
29

29

00:01:24,030  -->  00:01:27,300
that means the user has either recently registered,
30

30

00:01:27,300  -->  00:01:29,180
or recently logged in.
31

31

00:01:29,180  -->  00:01:30,013
Let's do that.
32

32

00:01:31,290  -->  00:01:35,700
We're gonna create a user blueprint dot route
33

33

00:01:35,700  -->  00:01:37,590
and this is gonna be slash login.
34

34

00:01:37,590  -->  00:01:40,310
Once again the methods are gonna be get and post
35

35

00:01:40,310  -->  00:01:42,160
because we'll need that for the form.
36

36

00:01:49,110  -->  00:01:53,120
And there we have the body of our Endpoint
37

37

00:01:53,120  -->  00:01:58,120
and we can do email is this request form email,
38

38

00:01:58,530  -->  00:02:02,640
password as well, and then we will try
39

39

00:02:02,640  -->  00:02:06,280
to say if the user is login valid,
40

40

00:02:06,280  -->  00:02:07,620
there's a new method we're gonna create
41

41

00:02:07,620  -->  00:02:10,690
with the email and password, then we will set
42

42

00:02:10,690  -->  00:02:14,120
the session email to be equal to email.
43

43

00:02:14,120  -->  00:02:16,200
And we would also return email for now.
44

44

00:02:16,200  -->  00:02:19,520
Eventually you're going to want to return something else
45

45

00:02:19,520  -->  00:02:21,850
like a page or redirect them to
46

46

00:02:21,850  -->  00:02:24,130
their alerts or something like that.
47

47

00:02:24,130  -->  00:02:25,380
We're gonna do that soon.
48

48

00:02:26,230  -->  00:02:30,270
If we failed for some reason, then we're gonna say
49

49

00:02:30,270  -->  00:02:35,270
user errors, user error as e and return e message.
50

50

00:02:35,340  -->  00:02:36,740
Some of the reasons it may fail
51

51

00:02:36,740  -->  00:02:39,220
is if the user doesn't exist for example
52

52

00:02:39,220  -->  00:02:43,480
we may raise an exception or if the email
53

53

00:02:43,480  -->  00:02:46,130
is malformed we could raise an exception potentially.
54

54

00:02:47,090  -->  00:02:49,780
Let's go over to the user model
55

55

00:02:49,780  -->  00:02:54,123
and write our is login valid method.
56

56

00:03:04,810  -->  00:03:07,290
Okay so what this is gonna do is once again,
57

57

00:03:07,290  -->  00:03:11,050
is gonna try to find a user by email.
58

58

00:03:11,050  -->  00:03:13,950
If this fails, it will raise an error
59

59

00:03:14,810  -->  00:03:19,180
and that error will essentially be raised over to the caller
60

60

00:03:19,180  -->  00:03:21,550
which is this is login valid function.
61

61

00:03:21,550  -->  00:03:24,770
Because we're not gonna put this inside a try except log
62

62

00:03:24,770  -->  00:03:27,890
that error would just bubble up to the next caller.
63

63

00:03:27,890  -->  00:03:32,510
So from is login valid it would go up to the login user
64

64

00:03:32,510  -->  00:03:34,930
and there it would be caught by this except block
65

65

00:03:34,930  -->  00:03:36,800
and we would return the message.
66

66

00:03:36,800  -->  00:03:39,610
So this message here would be returned
67

67

00:03:39,610  -->  00:03:41,793
if we cannot find the user by email.
68

68

00:03:42,650  -->  00:03:45,180
Notice that in the register user method,
69

69

00:03:45,180  -->  00:03:49,050
we actually caught that and used it for our benefit
70

70

00:03:49,050  -->  00:03:50,910
but here we're just gonna let it bubble up
71

71

00:03:50,910  -->  00:03:54,463
and be caught by our Endpoint.
72

72

00:03:55,970  -->  00:03:59,000
Then if not utils dot check hashed password
73

73

00:03:59,000  -->  00:04:03,003
for the password and the user's password,
74

74

00:04:05,340  -->  00:04:07,190
then we're gonna raise another error.
75

75

00:04:08,910  -->  00:04:12,027
And that's gonna be an incorrect password error
76

76

00:04:12,027  -->  00:04:15,123
and we're gonna say your password was incorrect.
77

77

00:04:17,820  -->  00:04:19,700
Finally if that doesn't happen,
78

78

00:04:19,700  -->  00:04:24,700
meaning this did match, then we're just gonna return true.
79

79

00:04:28,310  -->  00:04:30,380
We need to create this error here,
80

80

00:04:30,380  -->  00:04:35,083
so we're gonna say class that, user error, do nothing.
81

81

00:04:37,840  -->  00:04:41,530
Then we'll return true down here and that will then go
82

82

00:04:41,530  -->  00:04:43,740
into session email equal email.
83

83

00:04:43,740  -->  00:04:45,690
So what's going on in the is login valid
84

84

00:04:45,690  -->  00:04:48,200
is we first find the user in the database.
85

85

00:04:48,200  -->  00:04:50,950
If the user is not found, that will produce an error
86

86

00:04:50,950  -->  00:04:53,630
which will then be caught by our Endpoint.
87

87

00:04:53,630  -->  00:04:57,000
Then we'll check whether the hashed passwords match.
88

88

00:04:57,000  -->  00:05:00,440
If they don't match, then we will raise another error
89

89

00:05:00,440  -->  00:05:02,860
saying your password was incorrect.
90

90

00:05:02,860  -->  00:05:05,600
If they do match, this will not be ran
91

91

00:05:05,600  -->  00:05:06,993
so we'll just return true.
92

92

00:05:07,870  -->  00:05:12,210
If we return true, then the session's email
93

93

00:05:12,210  -->  00:05:15,023
will be populated and we will return email.
94

94

00:05:22,280  -->  00:05:24,770
Hopefully that makes sense, let's run our app
95

95

00:05:24,770  -->  00:05:26,140
and see what happens.
96

96

00:05:26,140  -->  00:05:28,180
So I'm gonna go over to Chrome
97

97

00:05:29,084  -->  00:05:31,710
and recently registered bob@example.com
98

98

00:05:31,710  -->  00:05:36,060
so we're going to go to users log in
99

99

00:05:36,060  -->  00:05:39,920
and we're gonna do bob@example.com, 1, 2, 3, 4,
100

100

00:05:39,920  -->  00:05:42,040
and there we have Bob's email.
101

101

00:05:42,040  -->  00:05:45,500
If we go back and we try with bob2@example.com
102

102

00:05:45,500  -->  00:05:49,400
we get an error saying a user with this email was not found.
103

103

00:05:49,400  -->  00:05:51,140
That's it, this is everything we need
104

104

00:05:51,140  -->  00:05:54,550
in order to add log in to our app.
105

105

00:05:54,550  -->  00:05:57,460
Because we've got the user's data already stored
106

106

00:05:57,460  -->  00:06:00,320
from the registration, all we have to do is check it.
107

107

00:06:00,320  -->  00:06:03,530
Make sure the user's email and password exist
108

108

00:06:03,530  -->  00:06:06,070
and match what we've got saved
109

109

00:06:06,070  -->  00:06:08,773
and we can populate the session.
110

110

00:06:09,910  -->  00:06:13,970
Remember, as long as the app does not restart,
111

111

00:06:13,970  -->  00:06:18,362
and the browser is the same, then the session
112

112

00:06:18,362  -->  00:06:22,080
will be matched with the data the browser sends us
113

113

00:06:22,080  -->  00:06:25,700
and we can use this session anywhere else in our application
114

114

00:06:25,700  -->  00:06:28,870
to see if the user is logged in or not.
115

115

00:06:28,870  -->  00:06:32,020
By that I mean, if in any of our other Endpoints
116

116

00:06:32,020  -->  00:06:35,040
we check session and it has an email,
117

117

00:06:35,040  -->  00:06:36,380
that means the user logged in
118

118

00:06:36,380  -->  00:06:39,480
because the only way that the session could have an email,
119

119

00:06:39,480  -->  00:06:42,410
is if they logged in or registered.
120

120

00:06:42,410  -->  00:06:44,730
There's no other way for session to be populated
121

121

00:06:44,730  -->  00:06:47,630
with an email so that tells us the user logged in.
122

122

00:06:47,630  -->  00:06:49,600
And we're going to use that later on
123

123

00:06:49,600  -->  00:06:52,830
to show a user's alerts for example.
124

124

00:06:52,830  -->  00:06:55,820
We will be able to get all the alerts
125

125

00:06:55,820  -->  00:06:57,860
like we do here in the alert index
126

126

00:06:57,860  -->  00:07:01,410
but filter them so that we only get the user
127

127

00:07:01,410  -->  00:07:04,030
that is currently logged in's alerts.
128

128

00:07:04,030  -->  00:07:06,420
And that's gonna make this much more powerful
129

129

00:07:06,420  -->  00:07:08,030
and it's actually gonna do what we wanted
130

130

00:07:08,030  -->  00:07:13,030
which is cater each web to the user that is using it
131

131

00:07:13,460  -->  00:07:16,910
and not show every user all of the alerts
132

132

00:07:16,910  -->  00:07:19,960
including for items that they're not interested in.
133

133

00:07:19,960  -->  00:07:21,430
Thanks for joining me in this video,
134

134

00:07:21,430  -->  00:07:23,220
I hope you've enjoyed it and learned something.
135

135

00:07:23,220  -->  00:07:24,763
I'll see you in the next one.
