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
0:06 throughout the whole application,
0:08 but because most of the time we are going to begin working with users,
0:12 at the level of the controllers or the web action methods,
0:15 it makes a lot of sense to me to add a few properties to the base controller
0:19 so that throughout our entire application as we add new controllers and everything,
0:23 we always have access to this logged in user
0:27 and we don't even have to think about it.
0:29 So we can add two properties, in this case,
0:32 we are going to add one called is_logged,
0:34 before we had the logged in id and in this slide I guess
0:37 I decided you don't really need the id unless you really want to know the user itself,
0:40 so here is just a bollean you can test is user logged in,
0:43 and to compute it it's super easy, we go to our cookie auth,
0:46 we say get the user by the auth cookie, and if that returns something,
0:49 hey they are logged in, if it's None,
0:52 which the cookie is not there or it's tampered with or something, then it is None.
0:55 So then they are not logged in. So, that's a really easy thing we can test with,
0:59 if we want the actual user itself, we can use the property logged_in_user,
1:04 which will get the user id from the cookie_auth layer,
1:07 it will check to see whether or not it's a value, right,
1:10 if there is nobody there, nobody logged in, no id, then just return no user as well,
1:13 but if there is a user id, let's go to the account service,
1:16 do a SQLAlchemy query to the database and retrieve that account,
1:19 Once you have this on the base controller, you have it on all of the views, right,
1:22 so then in our layout template, we can actually do a tal conditional
1:27 to change what shows up in our navigation.
1:30 So here we did a tal condition "not view.is_logged_in,
1:33 and a tal conditional "view.is_logged_in.
1:36 So, when they are not logged in we see sign in / register,
1:39 when they are logged in, they see account and log out.