1
00:00:05,670 --> 00:00:07,520
Welcome back everyone in this lecture.

2
00:00:07,530 --> 00:00:13,330
We're going start talking about flask log in the flask clogging the library mix adding user authentication

3
00:00:13,330 --> 00:00:14,500
to our web applications.

4
00:00:14,530 --> 00:00:15,650
Very easy.

5
00:00:15,670 --> 00:00:19,990
Basically it just has easily callable decorator's for adding user functionality.

6
00:00:19,990 --> 00:00:24,860
Let's go ahead and begin exploring it in part 1 of our discussion on this library.

7
00:00:24,920 --> 00:00:25,150
LAURA.

8
00:00:25,150 --> 00:00:27,000
Here I am back in my text editor.

9
00:00:27,100 --> 00:00:31,120
I'm going to start off and we're going to try to organize this a little bit similar to what we're doing

10
00:00:31,120 --> 00:00:33,940
before when talk about larger flask applications.

11
00:00:33,940 --> 00:00:39,220
Now technically this floss application is going to be too small to separate forms and models into their

12
00:00:39,220 --> 00:00:40,840
own subcomponents.

13
00:00:40,840 --> 00:00:48,040
But what I will do is I'm going to create a new folder called my project and at the same level of this

14
00:00:48,160 --> 00:00:52,590
my project folder I'll create a file called app pi.

15
00:00:52,690 --> 00:00:56,080
So this going to be a little similar to what we're just doing before in the previous section of the

16
00:00:56,080 --> 00:00:57,960
Course now to begin with.

17
00:00:58,030 --> 00:01:00,860
I'm going to create all the files and folders will be needed.

18
00:01:01,000 --> 00:01:09,940
So inside of my project I'm going to create the underscore underscore in its underscore underscore the

19
00:01:09,940 --> 00:01:16,510
py file and that will allow us to import from my project and then also create a form's thot py file

20
00:01:17,080 --> 00:01:26,510
as well as a model that py file and then underneath my project I'll create a new folder to hold my templates.

21
00:01:26,530 --> 00:01:30,670
So all those age html files and we're going to have a couple of different aist him all files available

22
00:01:30,700 --> 00:01:31,370
to us.

23
00:01:31,480 --> 00:01:37,210
So we should start creating them now will have a base 20:00 essentially to hold on and how far that

24
00:01:37,210 --> 00:01:38,970
will have a home page.

25
00:01:39,080 --> 00:01:43,540
So I'll say homemade T.M. and then we'll have a log in a page.

26
00:01:43,600 --> 00:01:49,670
So once the users register they can log in in an order for them to log and they have to register.

27
00:01:49,680 --> 00:01:55,200
So will create a new register each time I'll say register each time.

28
00:01:55,290 --> 00:02:00,720
And then finally after someone's log then we'll go ahead and redirect them to a welcome user page just

29
00:02:00,720 --> 00:02:02,550
so we can check that everything's working.

30
00:02:02,700 --> 00:02:07,750
So we'll create a new file and call it welcome underscore user that HMO.

31
00:02:08,230 --> 00:02:08,720
OK.

32
00:02:09,000 --> 00:02:11,170
So we have all our files ready to go.

33
00:02:11,250 --> 00:02:15,060
We have our templates and then all our forms and models.

34
00:02:15,300 --> 00:02:22,370
Let's begin by setting up our underscore underscore and it underscore underscore that py file signatures

35
00:02:22,380 --> 00:02:23,180
to make it clear.

36
00:02:23,190 --> 00:02:28,360
Right now I'm editing the file underneath my project folder.

37
00:02:28,890 --> 00:02:32,030
So I'm going to start off of a couple of imports as we've done in the past.

38
00:02:32,070 --> 00:02:42,410
Import os from flask import flask will set up a database source say from flask underscore sequel alchemy

39
00:02:42,950 --> 00:02:53,150
import sequel alchemy from flask underscore migrate import migrate and from flask underscore.

40
00:02:53,160 --> 00:02:54,270
Lock in.

41
00:02:54,270 --> 00:02:56,400
We're going to import loggin manager.

42
00:02:56,580 --> 00:02:59,850
So if you haven't installed flask log in go ahead and do so.

43
00:02:59,850 --> 00:03:03,740
Just a simple Pip install a flask log in and flash laga it comes with.

44
00:03:03,740 --> 00:03:09,960
This really is full class basically that allows you to effectively automate all the management of your

45
00:03:09,960 --> 00:03:11,520
logging systems.

46
00:03:11,520 --> 00:03:14,830
So we'll start off by actually using that anoints say logging.

47
00:03:14,850 --> 00:03:23,040
Underscore manager is equal to an instance of this logging in a manager object that it won't create

48
00:03:23,040 --> 00:03:25,780
my application just as we've always done we'll see.

49
00:03:25,810 --> 00:03:31,060
Underscore underscore name underscore underscore we'll do a couple of configurations here.

50
00:03:31,140 --> 00:03:35,940
So I will say app config and let me get some more space here.

51
00:03:38,310 --> 00:03:40,590
Will say app config for secret key

52
00:03:44,000 --> 00:03:48,160
is equal to my secret key.

53
00:03:48,570 --> 00:03:55,130
Or really whatever string you want at this point and then will set a base directory equal to OS path

54
00:03:55,850 --> 00:04:05,090
absolute path and the here will say OS that path directory name or C underscore underscore file underscore

55
00:04:05,090 --> 00:04:11,110
underscore just as we've always done and then we'll set up the configurations for our school like database

56
00:04:11,110 --> 00:04:15,580
all say squawker me underscore database.

57
00:04:15,800 --> 00:04:27,220
You are I said that equal to school like colon three forward slashes concatenated with O S path where

58
00:04:27,230 --> 00:04:31,950
a base directory is and then we'll call it data thought sequel.

59
00:04:32,600 --> 00:04:34,930
So we've actually already done all this before.

60
00:04:34,940 --> 00:04:39,170
The only thing new here is that query that log in the manager object which we haven't used yet but we

61
00:04:39,170 --> 00:04:40,460
will in just a little bit.

62
00:04:40,460 --> 00:04:50,090
Last thing I want to set up is sequel alchemy underscore track modifications and set equal to false

63
00:04:52,170 --> 00:04:52,720
OK.

64
00:04:52,980 --> 00:04:59,760
Next step is to actually create our database by saying sequel alchemy passing in our application and

65
00:04:59,850 --> 00:05:01,620
I want to be able to perform migrations on it.

66
00:05:01,620 --> 00:05:05,580
So I'll say migrate past in the application and our new database.

67
00:05:05,820 --> 00:05:09,690
And now I can pass on the application to our logon manager.

68
00:05:09,840 --> 00:05:15,750
So the way this works is you've taken that logging manager object you created and you call the method

69
00:05:16,490 --> 00:05:18,670
in underscore app method off of it.

70
00:05:19,020 --> 00:05:25,350
And this is going to basically configure your application to have management of lock in users so you

71
00:05:25,350 --> 00:05:32,160
pass in your app and then in order to tell users what view to go to when they log in will say log in

72
00:05:33,080 --> 00:05:39,240
manager log in view is equal to log in.

73
00:05:39,270 --> 00:05:45,540
So what that means is in our actual app the py file or setting up our views we're going to have a view

74
00:05:45,540 --> 00:05:52,470
called log in and that will essentially link this up to our logging manager so that we can tell what

75
00:05:52,470 --> 00:05:54,910
users view they need to go to to log in.

76
00:05:55,050 --> 00:05:57,110
So everything else is pretty much the same.

77
00:05:57,120 --> 00:06:02,250
I want to highlight the differences and that is from Flast log in import logging manager.

78
00:06:02,250 --> 00:06:08,820
Create an instance of this logging manager object and then scrolling down here we initiate the app to

79
00:06:08,820 --> 00:06:14,340
be able to have log in management and then we set our log in to view equal to logon and that will match

80
00:06:14,340 --> 00:06:16,540
up with the view we create later on in our app.

81
00:06:16,580 --> 00:06:17,680
Hi file.

82
00:06:17,700 --> 00:06:19,320
So save those changes.

83
00:06:19,530 --> 00:06:24,900
And up next we need to do is actually begin creating our model for a user.

84
00:06:24,960 --> 00:06:27,050
So let's go over here to models not.

85
00:06:29,050 --> 00:06:34,680
Right now I'm in models not pi and I'm going to set up everything we need to contain an actual user

86
00:06:34,680 --> 00:06:35,790
in our database.

87
00:06:36,810 --> 00:06:44,380
I'll say from my project import database and that log in a manager object that we created.

88
00:06:44,550 --> 00:06:46,780
Again that's being imported from that in it py file.

89
00:06:46,790 --> 00:06:53,800
So we were just in then I'll be using Virk search for my password and hashing and checking.

90
00:06:54,300 --> 00:06:56,280
And you have to apologize for my pronunciation of that.

91
00:06:56,310 --> 00:07:05,040
I don't really speak German so I'll say that security in poor generate password hash and then check

92
00:07:05,070 --> 00:07:06,330
password hash.

93
00:07:06,480 --> 00:07:08,160
I don't want it to collapse this sidebars.

94
00:07:08,160 --> 00:07:13,290
We get a little bit of room here so I have the ability to generate the passwords and check the passwords.

95
00:07:13,290 --> 00:07:19,310
And then finally also from a flask logon import user mixin.

96
00:07:19,320 --> 00:07:25,620
So it's really cool is by inheriting from the user mixin class we're going to get access to a lot of

97
00:07:25,620 --> 00:07:29,350
built in attributes which will then be able to call interactional views.

98
00:07:29,430 --> 00:07:35,310
So that will come later or messing around for views in the app pie but I want you to keep that in mind.

99
00:07:35,610 --> 00:07:36,270
Let's continue.

100
00:07:36,270 --> 00:07:41,570
By starting off to create our user class this won't be too different from before.

101
00:07:41,580 --> 00:07:47,590
We're again going to inherit from B model but now we're actually going to inherit from multiple classes.

102
00:07:47,850 --> 00:07:53,190
And the other one we're going to inherit from is user mixin essentially user Mixon has all those management

103
00:07:53,190 --> 00:07:56,220
features of logging in users and authorizing them.

104
00:07:56,220 --> 00:07:58,630
So we don't have to take care of that ourselves.

105
00:07:58,650 --> 00:08:01,670
So we inherit from DVH model and user Mixon.

106
00:08:02,130 --> 00:08:05,130
Let's give this a table name parameter.

107
00:08:05,130 --> 00:08:14,620
So I'll say wups table name and I'm going to set equal to users and let's have a have an ID column.

108
00:08:14,790 --> 00:08:16,470
So say this is a D-B column

109
00:08:19,140 --> 00:08:20,730
D-B integer.

110
00:08:20,730 --> 00:08:23,140
So those here I'm not doing the actual imports from D-B.

111
00:08:23,140 --> 00:08:25,590
Instead of saying D-B column D integer.

112
00:08:26,220 --> 00:08:27,650
And this should be a primary key.

113
00:08:27,660 --> 00:08:34,860
So every user should have a unique primary key so that we don't have overlapping primary keys then we'll

114
00:08:34,860 --> 00:08:37,760
have e-mails for each user.

115
00:08:37,850 --> 00:08:39,830
So we'll save their e-mails as well.

116
00:08:39,830 --> 00:08:41,060
This will be a strain.

117
00:08:41,420 --> 00:08:46,040
And if you want you can pass in a number into the string to limit how long the e-mail can be in case

118
00:08:46,040 --> 00:08:47,260
you're worried about spam.

119
00:08:47,330 --> 00:08:53,900
So we can go ahead and say 64 characters most often you'll probably want to set Unique equal to true

120
00:08:53,900 --> 00:08:59,210
for this column because you don't want to have two different users with the same e-mail address because

121
00:08:59,210 --> 00:09:02,500
if something happens where we want to reset their password by sending them an e-mail.

122
00:09:02,660 --> 00:09:04,270
Our system will get confused.

123
00:09:04,370 --> 00:09:09,410
So we should have unique e-mails and we can also say index to be true

124
00:09:12,620 --> 00:09:18,140
and also that username equal to the B column.

125
00:09:18,600 --> 00:09:21,620
And this will also be a string.

126
00:09:21,760 --> 00:09:27,710
So we'll say string Well also said it's 64 characters their user name should probably also be unique.

127
00:09:27,710 --> 00:09:32,500
So you don't have two people with the same username Otherwise you make it confusing trying to log in.

128
00:09:33,290 --> 00:09:35,930
And also indexical to true.

129
00:09:35,990 --> 00:09:40,290
And then finally we're going to save the hashed version of the password.

130
00:09:40,670 --> 00:09:46,650
So say password hash is equal to D-B column D-B.

131
00:09:46,900 --> 00:09:48,460
And this is also going to be a string.

132
00:09:48,700 --> 00:09:51,380
However the hashes we're going to say are actually pretty long.

133
00:09:51,580 --> 00:09:55,340
So we should have at least 128 space for that string.

134
00:09:56,320 --> 00:09:57,000
OK.

135
00:09:57,090 --> 00:10:02,670
Then in order to actually create an instance of a user object we'll say the F in it and this will take

136
00:10:02,670 --> 00:10:13,220
in self email username and password so then we'll say self e-mail is equal the email it's passed in

137
00:10:13,220 --> 00:10:20,600
for that user self username is equal to the user name that's passed and for that user and then self

138
00:10:20,600 --> 00:10:23,540
password hash is equal to.

139
00:10:23,780 --> 00:10:26,160
And this is where it's going to be actually a little different.

140
00:10:26,180 --> 00:10:31,010
So when the user is being created they're going to provide their real password which will be a string

141
00:10:31,010 --> 00:10:33,220
like my secret password one two three.

142
00:10:33,230 --> 00:10:35,440
However we don't want to say that actual string.

143
00:10:35,570 --> 00:10:37,710
We want to save the hashed version of that string.

144
00:10:37,880 --> 00:10:39,540
So that's what we're going to use right here.

145
00:10:39,740 --> 00:10:41,580
Generate password hash.

146
00:10:41,630 --> 00:10:46,740
Get a copy that and put it down here so it's a general password hash.

147
00:10:46,740 --> 00:10:51,510
From the password that's provided upon initializing a user.

148
00:10:51,520 --> 00:10:57,480
So again we're saving the past rehash not the actual password and then in order to check when the users

149
00:10:57,480 --> 00:11:06,130
log in that is to correct user We're going to have a method called check password and it's going to

150
00:11:06,130 --> 00:11:08,490
be self and it's going to take any password.

151
00:11:10,280 --> 00:11:20,640
And we're going to return the result of check password hash self password hash and password.

152
00:11:20,800 --> 00:11:25,240
So all this is doing is it's using that check password hash function that we talked about earlier and

153
00:11:25,240 --> 00:11:26,850
it's going to accept some passwords.

154
00:11:26,860 --> 00:11:31,780
So this is when a user is logging in maybe a second time it's going to check their stream password hash

155
00:11:31,790 --> 00:11:37,120
it and then check it against the self-taught password hash that was stored earlier for that particular

156
00:11:37,120 --> 00:11:38,610
instance of the user.

157
00:11:38,620 --> 00:11:39,050
OK.

158
00:11:39,310 --> 00:11:42,280
So let me zoom out a little bit so you can see the whole picture here.

159
00:11:42,310 --> 00:11:48,160
We have our user class table name these four columns ID email user name and password hash.

160
00:11:48,160 --> 00:11:53,440
We accept email username and password but remember when you're accepting that password we generate the

161
00:11:53,430 --> 00:11:56,730
password hash and then we have this method for checking it.

162
00:11:56,770 --> 00:12:03,020
Finally what we need to do is add in a quick little function here called load user and the user loaded

163
00:12:03,020 --> 00:12:04,450
that that we're using here.

164
00:12:04,480 --> 00:12:08,990
This is basically going to allow flask log in to load the current user and grab their ID.

165
00:12:09,010 --> 00:12:14,700
So once someone is logged in we'll be able to show them pages specific to their logon ID.

166
00:12:14,910 --> 00:12:16,360
And this is actually quite simple.

167
00:12:16,380 --> 00:12:18,910
We're going to zoom in a bit here so we can see it.

168
00:12:19,290 --> 00:12:25,930
You say at loggin manager thought user underscore loader.

169
00:12:25,950 --> 00:12:30,510
So again this is a built in decorator into our logging manager.

170
00:12:30,720 --> 00:12:42,880
And then we say DPF load user you pass in the User ID and then we're going to return off of our user

171
00:12:42,880 --> 00:12:43,780
database.

172
00:12:43,930 --> 00:12:48,490
We're going to query and get that user ID.

173
00:12:48,580 --> 00:12:52,900
So that means we have this ability to load whatever current user that we're looking at based off their

174
00:12:52,900 --> 00:12:58,470
ID so we can query our user table based off that user ID and grab that specific user.

175
00:12:58,600 --> 00:13:01,260
That way we can later show information specific to that user.

176
00:13:01,390 --> 00:13:03,950
In our case we'll just be saying something like Hello username.

177
00:13:04,120 --> 00:13:06,490
So that's how we're going to be using load user.

178
00:13:06,490 --> 00:13:09,600
So save all those changes and that's it for this lecture.

179
00:13:09,610 --> 00:13:15,880
So far what we've done is set up all the directories and files and then we set up our in a file here

180
00:13:16,180 --> 00:13:20,440
essentially very similar to what we've done before except now we have this log in a manager object that

181
00:13:20,440 --> 00:13:22,900
we're able to also initiate our app.

182
00:13:22,900 --> 00:13:28,330
We set up our log in view and we have our models up by file here and we're using those generation and

183
00:13:28,330 --> 00:13:29,980
checking a password hashes.

184
00:13:29,980 --> 00:13:33,350
We have this load user function to check for the currently logged in user.

185
00:13:33,490 --> 00:13:37,900
And we also have this user class and notice here we're using this user mixin that's going to come in

186
00:13:37,900 --> 00:13:43,030
handy later on because it's going to allow us to call different attributes directly in our views like

187
00:13:43,090 --> 00:13:45,990
is authenticated is active and get ID.

188
00:13:46,000 --> 00:13:50,610
So we'll see that later on when we start messing with her app that py file and building out the views.

189
00:13:50,740 --> 00:13:53,420
Before that we'll probably want to actually create some forms.

190
00:13:53,530 --> 00:13:56,350
We're going to do all of this and more coming up in the next lecture.

191
00:13:56,500 --> 00:13:57,040
We'll see if there.
