#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: Test it! Login to the app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One last thing I just want to run through quickly is the fact that our user class does have a few
0:08 pre-defined methods and properties that we can implement a lot easier without having to specify them manually if you we use, user mixin from Flask.
0:20 So let's quickly pop into our models file here and let's add user_mixin into our class here. And this will allow us to actually authenticate
0:31 the user, check to see if the user's active that stuff without having to manually code it in. And this works sort of hand in hand with that login
0:41 required decorator that we saw previously. So, in our user class definition here let's have user mixin and we need to import that from Flask logins.
0:55 From Flask login, import user_mixin. Simple as that, let's get out of there and let's also add that in to our routes.pi file pop on the end here.
1:09 Append user_mixin onto that and that will allow us to actually authenticate a lot easier. Last but not least, before we really get started.
1:20 Let's pop down here to our login page. What I wanted to call out that I failed to note in the previous video was this here. Login manager login view.
1:31 This here, login page, what we call here needs to be the same name as our function that people log in with. All right, these need to be the same here.
1:46 So login page and login page will match and then we're actually able to log in. So with that said, let's actually log in. Close out of that. Flask run.
1:58 Did everything work? Yes it does work so let's bring up our browser. Load the home page. There you go, this is the home page.
2:05 You do not need to be logged in to access this page. So let's head over to login page. There we go, this is the page where user's
2:15 will log into the site. Now let's throw in a bogus name. Let's throw in, you know, test, log in, Zelda.
2:25 Only because I'm looking at a Zelda picture on my left. Submit. Invalid user name or password. So, one other thing to point out here
2:35 let's go to our dashboard before we log in and notice because we're not logged in it actually redirects us to the log in page.
2:45 Now this is because of that log in page view that we set up with the log in manager. So the log in manager controls all of this.
2:55 It takes care of all of this for us. It noted that we were trying to access a page that was behind a login.
3:01 So it took us to the page we've defined as the log in page. Now let's actually enter in a username that exists in our database. Julian ABC123 Submit.
3:13 And now we're logged in. So I can go back to the index page. Yep, you do not need to be logged in and I can go back to the Pi bites dashboard
3:26 and we're still logged in. And there you have it. There is our Flask login. We can create another page, you don't have to
3:33 but we will create another page in the next video. Really quickly, just to log us out and then we'll demonstrate how this all works.


Talk Python's Mastodon Michael Kennedy's Mastodon