#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: What did we learn?

Login or purchase this course to watch this video and the rest of the course contents.
0:00 That's it, we're at the end of the chapter
0:02 Flask-Login, all done.
0:04 Thank you for sticking with it this long.
0:05 It was a pretty technical chapter
0:08 and I'm sure there were problems along the way
0:11 but hopefully you've figured them out.
0:13 Let's cover off what we learnt very quickly.
0:16 This is just the technical stuff
0:18 we haven't really seen before in the course
0:21 and the first thing we did
0:22 was we created our database definitions, the model
0:26 using Flask-SQLAlchemy.
0:29 And what we did was we defined the user class
0:34 which was used to store all of our user data in
0:37 and it actually uses UserMixin
0:41 from Flask-Login
0:43 and that contains predefined methods
0:46 that we were able to use
0:47 without having to actually run through it manually
0:50 and this class was the model for our SQL database.
0:56 In the class, we defined
0:58 the actual database columns and attributes
1:01 so we had our database.column.
1:03 We're able to say it was a string, an integer
1:06 for username and ID
1:08 and we're able to say whether it was nullable or unique.
1:12 And in the case of the ID
1:14 we're able to say it was the primary key.
1:18 And finally, we were able to create a simple class method
1:22 that just returns the username.
1:24 Now, while we didn't use it, I wanted to keep this here
1:27 just so that you could see the possibilities
1:30 and that you can actually use it elsewhere in the program.
1:35 Next, we wanted to add users to the database.
1:39 In order to do this, we had to create a user object
1:42 which use the user model or class that we created
1:46 in the previous slide.
1:49 And we assigned the username and the password
1:53 to a user object.
1:55 And this object was then actually added to the database
2:00 using db.session.add.
2:03 And finally, as with most things, once it was added
2:06 we needed to commit it.
2:08 One little quick thing we added in as well
2:10 was Flask flash, and that was to notify
2:14 that the data was entered successfully.
2:16 That was just the little message
2:17 that appeared on the screen to say, hey, user created.
2:20 The next thing we did that was different
2:22 was to set up the actual login manager.
2:26 Now, before we even started that
2:28 we need to actually link SQLAlchemy to our Flask app.
2:35 Then we needed to actually create the login manager object
2:38 using LoginManager from Flask-Login, pretty simple.
2:43 Then we just had to initialize it against the actual app.
2:46 So, initialize the app using the LoginManager.
2:51 And then this was the specific part.
2:54 We actually had to tell LoginManager
2:57 what the login view was called
3:00 and this was the same name as the actual function
3:04 that ran our login page
3:06 where the login would be instantiated
3:09 where it would be called.
3:12 Finally, we actually performed the login.
3:15 To do this, we just took the actual request form
3:19 check to see if there was a username sent back.
3:22 And if there was, we then query the database
3:25 this is all SQLAlchemy now
3:27 and we queried the database for that provided username.
3:32 If there was a username, we would then check to see
3:36 if the password that was attached to that user object
3:40 in the database, the password matched what the password was
3:45 that was sent back from the form.
3:47 So, if you entered the password
3:49 we check it against the database.
3:51 If it was successful, we would log in the user
3:55 and redirect them using the redirect URL for
3:59 we'd redirect them to the PyBites dashboard.
4:04 That's it.
4:05 So, that was Flask-Login.
4:07 I really hope you enjoyed that one.
4:08 It was one of the more technical ones you'll see
4:11 but hopefully you got something out of it.
4:14 Play with it, head back to the README
4:16 to just cover off some extra things you can do
4:18 if you have extra time.
4:21 I do anticipate this may have taken you all of the days
4:24 depending on how much time you have.
4:26 Either way, it's your turn, so keep calm and code in Python.