The UserMixin From Flask-Login5:50 with Kenneth Love
There's no reason to do all of this work ourselves when projects like Flask-Login exist to do so much of it for us. Let's use the `UserMixin` mixin to add the necessary properties to our model so users can log in.
pip install flask-login
from flask_login import UserMixin for newer versions of Python.
Mixins are small classes that add some specific feature. Since they're not the final class that we want to extend, they go at the beginning of our inheritance chain.
The UserMixin docs.
As programmers, we're often a pretty lazy lot. 0:00 At least, I know I am. 0:02 Especially in areas where someone has done the work 0:04 better than I probably would have. 0:05 So when I'm presented with the chance to use a package to handle user sessions or 0:07 to write the code to handle sessions, cookies, remember me functionality, 0:11 and all sorts of other things. 0:14 I'm gonna pick that package every single time. 0:16 Flask-login is one of these packages and 0:19 you'll want to install it with pip at home if you're not doing this in workspaces. 0:21 Flask-login gives us a lot of goodies, but 0:24 we're just going to use one of them right now, the user mixin. 0:27 What's a mixin? 0:31 A mixin is a class that gives some small in scope piece of functionality that 0:32 isn't standalone. 0:36 In this case, it gives us a few properties to tell if a user is logged in or not, and 0:37 a method for retrieving the user's ID. 0:41 This won't change the actual database table though. 0:43 Let's go see how to use it. 0:46 So, to do the magic power of this user mixin, 0:47 which isn't magic at all, you can always go look at it and see all the code. 0:52 We need to import it. 0:57 So, let's go ahead and do that. 0:58 I'm gonna do that here from flask.ext.login, import UserMixin. 0:59 This brings us to our first kinda weird thing. 1:06 We installed this package called flask-login. 1:10 So, shouldn't it be, you know, from flask-login, import UserMixin, 1:13 something like that? 1:17 You would think so. 1:18 But, flask the flask ecosystem has this kind of neat 1:19 pattern where the packages get installed to this ext module. 1:24 And then inside of that, you get the package so, 1:29 we have the ext module inside of there, is log in. 1:31 So, everything's kind of underneath flask in this ext, 1:34 which stands for external area, which is kinda cool. 1:37 You'll see that when we use decrypt and stuff later, same location. 1:40 So, it's a really nice kinda thing, it's neat. 1:45 Okay, so we had to mix the imported, how do we use it? 1:49 So, we have to add that to our inheritance chain on the model. 1:52 So, this right here, you remember specifies what our parent class is. 1:57 But we can have more than one parent class. 2:02 So we're going to add in our UserMixin right here. 2:05 Now, since mix ins don't implement an entire thing, they're small, 2:10 they're mixin. 2:15 It's like putting chocolate chips or caramel chips or 2:16 something like that in your cookies. 2:19 You mix them in. 2:20 It's not the whole cookie. 2:21 So we put them before the thing that is our ultimate parent class. 2:22 So in this case, our user is ultimately a model, 2:27 even though the user mixin mixes and modifies it. 2:31 So user mixin goes first, model goes second. 2:36 So what does this user mixin gain us? 2:40 Well, let's take a look at it. 2:44 Let me open up the docs here, and let's take a look at what we get. 2:47 So, what we get is if we want to use flask-login and 2:52 we wanna use our own model, which we have to, flask-login doesn't come with a model, 2:55 then we would have to implement these four things on our own class, on our own model. 3:00 I don't want to model all these things myself. 3:05 I don't have to, and I'm okay with how they work and the user mixin. 3:07 I could change these, and write my own if I wanted to. 3:11 But let's look at what these are. 3:14 So, it's three different attributes, and then, a method. 3:15 So, the first one is this, is_authenticated. 3:19 So it checks whether or not a user has given the correct credentials to 3:24 whatever the log in process is. 3:29 By default, it always returns true. 3:32 If we needed to change this to check some other way of doing that check, 3:34 then we could override this. 3:38 We don't need to, though. 3:39 The way it works is fine, 3:40 especially because we're gonna be using this log in required with it. 3:41 The next one here, is_active. 3:44 You can see that it returns true, this is an active user. 3:47 So, if you've been on websites before where you sign up and 3:50 they send you an email with a link in it. 3:54 And you have to click that link, in order to turn on your account, 3:56 to actually be able to log in. 3:59 That's what this is. 4:01 So we could set users being inactive, and 4:02 then they have to click a link to make themselves active, or they have to, 4:05 I don't know, answer a question or send us $5 or something like that. 4:09 By default though, we're just gonna say that all the users are active. 4:14 We could make them have that ability if we wanted but we don't. 4:17 So just kinda make them all active. 4:20 And then the last attribute here is, is_anonymous. 4:22 So we can have anonymous users which are not logged in, 4:26 we don't know who these users are, but they are user account. 4:30 So we could have anonymous users and maybe we create records that belong to these 4:34 anonymous users, and when they sign up, they actually log in or they actually 4:38 create an account, then we suddenly assign all those records to that anonymous user. 4:42 For us, it's going to return false because they're not anonymous, so we're cool. 4:47 We're fine on that. 4:53 And then the last one here is a method called get_idD. 4:55 And, so flask-login wants a unicode of the user's ID. 4:58 Or something that uniquely identifies them. 5:03 Maybe it's their user name, maybe it's their email address, 5:06 maybe its a UUID that we generated for each of them. 5:08 Maybe it's some sort of digital fingerprint. 5:12 But this is what comes back. 5:14 So, by default, it sends back an int. 5:16 Or, sorry, unicode of the integer, which is the, the ID. 5:18 Which is a, a field that our, 5:22 is automatically created on our models by peewee. 5:24 That's fine, that's what we want that to do. 5:26 But these four things give us a lot of room to configure our user 5:28 models to whatever particular use case we have on building a certain app. 5:33 Hopefully, you found the user mix in to be pretty user-friendly. 5:39 We'll revisit this a couple more times for flask-login later. 5:42 Now, I think we should move on to getting our accounts properly protected 5:45 by hashing their passwords. 5:48
You need to sign up for Treehouse in order to download course files.Sign up