LoginManager4:38 with Kenneth Love
We've used the `UserMixin` from Flask-Login. Now, let's use the `LoginManager` to set up our application so we can load users. This will get us ready to create our user CRUD.
LoginManager- An appliance to handle user authentication.
user_loader- A decorator to mark the function responsible for loading a user from whatever data source we use.
Flask-login needs to be able to find the current user or 0:00 any user we're looking for. 0:03 To help create a process, 0:05 we need to create a function that will return an instance of our user model. 0:06 It'll actually take longer to explain than it will to build so 0:08 let's do both at the same time over on workspaces. 0:12 All right. So, 0:15 if we're gonna use this log in manager, we actually need to go ahead and import it. 0:16 So, let's go up here to our imports. 0:20 And we're gonna say from flask.ext.login import LoginManager. 0:21 And we have to add a few more things before we can use this. 0:28 So, one of those things we have to add, because we're gonna be using sessions, is, 0:32 we have to add our secret key. 0:35 And so again, you [UNKNOWN], you just generate some random stuff. 0:39 [SOUND] And there you go, there's your secret key. 0:43 And then we need to create our log in manager. 0:48 So actually we're gonna say login_manager equals LoginManager. 0:50 [SOUND] Create an instance of that. 0:53 And login_manager.init_app of pass of the app. 0:55 So this sets up the log in manager for our application. 1:01 So paying attention to like our views and controlling our user, and 1:05 getting our global object and all that kind of stuff. 1:09 And then we have to say login_manager.login_view 1:13 is gonna be named login. 1:16 Now we don't have that view yet. 1:18 We will later. 1:20 So this is just saying, hey, if they're not logged in, and 1:21 you gotta redirect them somewhere to log in, we're gonna call the view log in. 1:24 So redirect them to there. 1:29 Okay, and then we're going to provide 1:31 a function that the log in manager will use when it needs to look up a user. 1:34 So, we decorate this with login_manager.user_loader, 1:39 creatively named. 1:45 And we'll say load_user. 1:47 And we're gonna get a user ID passed into this. 1:50 All right, so, let's do this. 1:54 We'll say try, [SOUND] return models.User.get, 1:55 models.User.id, is equal to userid. 2:01 And, unless we get models.DoesNotExist, and then return None. 2:07 So where does this models.DoesNotExist comes from? 2:14 That's actually from peewee. 2:17 But since we imported everything from peewee inside models, 2:19 we have access to all of peewee's stuff. 2:21 Kinda cool. 2:24 Though this is an exception that's raised whenever 2:24 you say give me a record that looks like this. 2:28 And the database says, I don't have a record that looks like that. 2:31 So that's why it comes back there. 2:34 We get does not exist. 2:36 We wanna catch that. 2:37 And we'll just return none cause that user doesn't exist. 2:38 We don't have that user. 2:40 So let's actually add something over here in our user, or in our models file. 2:42 Down here at the bottom, let's add an initialize method. 2:48 [SOUND] So def initialize. 2:54 And we'll call DATABASE.connect cuz we wanna connect to the database. 2:58 We'll call DATABASE.create_tables and right now, 3:02 we're just gonna create User cuz that's the only table that we have. 3:06 And of course, we want that to be safe, so, 3:11 safe equals True and when that's done, we wanna call close on the database. 3:14 So then, down here at the bottom of apps.py, or app.py, sorry. 3:19 Let's make sure that the models are initialized, 3:24 [SOUND] and let's automatically create, 3:28 just in case it doesn't exist, let's create a user for ourselves. 3:31 So models.user.create_user, cuz we're not gonna use create, 3:36 we're gonna use create_user, so that it encrypts the password for us. 3:39 Name, I'll just say kennethlove, email is firstname.lastname@example.org. 3:44 Password is, let's go with something super secure, let's use password. 3:53 [SOUND] Cause that's the best. 3:57 And I'm gonna make myself an admin. 3:59 [SOUND] All right, so save. 4:02 So if we were to do pythonapp.py down on our shelf, then, 4:06 it would initialize our models and it would create a super user for 4:10 us just in case that user doesn't exist already. 4:14 So if we wanted to be able to log in without having to create like, 4:18 some fake users, it's not that big of a deal, but, you know, it's nice to have. 4:20 That's a really small function, but it's super important how our app behaves. 4:25 And one of my favorite things about the Python ecosystem, 4:29 we didn't have to write any of the down and dirty code. 4:32 Okay, next up is building forms in Python. 4:35
You need to sign up for Treehouse in order to download course files.Sign up