Python for Entrepreneurs Transcripts
Chapter: User accounts and identity
Lecture: Concept: Logged in status

Login or purchase this course to watch this video and the rest of the course contents.
0:02 We could go about using this cookie_auth module throughout the whole application,
0:09 but because most of the time we are going to begin working with users, at the level of the controllers or the web action methods,
0:16 it makes a lot of sense to me to add a few properties to the base controller
0:20 so that throughout our entire application as we add new controllers and everything, we always have access to this logged in user
0:28 and we don't even have to think about it. So we can add two properties, in this case, we are going to add one called is_logged,
0:35 before we had the logged in id and in this slide I guess I decided you don't really need the id unless you really want to know the user itself,
0:41 so here is just a bollean you can test is user logged in, and to compute it it's super easy, we go to our cookie auth,
0:47 we say get the user by the auth cookie, and if that returns something, hey they are logged in, if it's None,
0:53 which the cookie is not there or it's tampered with or something, then it is None.
0:56 So then they are not logged in. So, that's a really easy thing we can test with,
1:00 if we want the actual user itself, we can use the property logged_in_user, which will get the user id from the cookie_auth layer,
1:08 it will check to see whether or not it's a value, right, if there is nobody there, nobody logged in, no id, then just return no user as well,
1:14 but if there is a user id, let's go to the account service, do a SQLAlchemy query to the database and retrieve that account,
1:20 Once you have this on the base controller, you have it on all of the views, right, so then in our layout template, we can actually do a tal conditional
1:28 to change what shows up in our navigation. So here we did a tal condition "not view.is_logged_in, and a tal conditional "view.is_logged_in.
1:37 So, when they are not logged in we see sign in / register, when they are logged in, they see account and log out.

Talk Python's Mastodon Michael Kennedy's Mastodon