Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Probably the simplest view we'll write for the entire app. We'll use a handy method from Flask-Login to log users out.
Use flask_login
instead of flask.ext.login
New Terms
-
logout_user()
- Method to remove a user's session and sign them out. -
@login_required
- Decorator to mark a view as requiring a user to be logged in before they can access the view.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Logging a user out doesn't require us to
create a new form or anything like that.
0:00
So, we can do it all with one simple view.
0:04
Okay, so we want to be able to log users
out.
0:07
This is something we're gonna, we're not
gonna write the HTML for this just yet
0:11
because we're gonna do that when we build
our whole layout.
0:14
But we do wanna go ahead and make a view
for this.
0:19
So let's go back over here to app.py, and
0:21
we actually need to do a couple of
imports.
0:24
So if we come up here to our login line 3.
0:26
So we have a login_user.
0:31
We need to import logout_user as well.
0:33
And then, because we don't want, it
doesn't really matter, but
0:37
we don't want people to go to the logout
view unless they're already logged in.
0:40
So, we're gonna bring in a new decorator,
which is login_required.
0:44
And I can see that I'm at 82 characters,
so we're gonna break this up
0:48
just cuz I'm super, super picky about this
stuff.
0:53
Okay.
0:58
So there's those.
0:59
So let's go add our new view.
0:59
And,we'll put it down here after login.
1:03
And it will be app.route, logout.
1:06
And we don't need to specify any methods
like get or
1:11
post because it's only gonna be through
get.
1:14
And then we're gonna add a second
decorator cuz we can add more than
1:16
one decorator.
1:19
And we're gonna say login_required.
1:21
So, you get to this view, you have to be
logged in.
1:23
If you're not logged in, we'll send you to
the login page, which is kind of
1:25
funny cuz you'll go to the log in page
just so you can soon hit the logout page.
1:29
But whatever.
1:33
And we'll say def logout.
1:35
And we're gonna call logout_user.
1:37
And we're gonna call return redirect,
url_for, index.
1:42
So let's talk for a second about what
logout_user here and
1:48
login_user here are doing.
1:52
What are those doing?
1:55
They're creating sessions on the user's
browser.
1:56
And they're giving them a cookie.
2:00
And the cookie references their user
account.
2:02
And it says, hey, this user's logged in.
2:05
Now, this isn't necessarily the absolute,
most secure way of doing this.
2:07
If you go look at Flask login, you'll find
some other stuff that we want to do.
2:11
We might even want to start storing like
sections in the database instead of
2:15
just in this cookie.
2:19
But we're making a note that they're
logged in.
2:21
So now they're able to do stuff.
2:23
And we know who the user is.
2:25
So that's what login_user is doing.
2:27
logout_user deletes that cookie, gets rid
of that information.
2:28
So now we no longer know who they are.
2:32
They've been kicked out.
2:35
We don't need them anymore.
2:36
So, that's a fairly nice thing to have
because it gives us
2:38
a great way of handling very easily, hey,
this user's in or hey, this user's out.
2:41
We don't want them anymore.
2:46
And something else we might do is we might
actually add a flash here that says,
2:47
You've been logged out!
2:52
If we wanna say, Come back soon!,
2:56
just because we can.
2:58
And we'll do a success on that too.
3:00
So now when we get our Flask messages
showing up all the time, then we can just
3:02
[SOUND] throw this stuff up, and it'll be
a great experience for our users.
3:07
I love when things only take a couple of
lines to achieve.
3:12
In our next stage,
3:16
we'll tackle the main part of our app,
letting users post new status updates.
3:17
We'll need to use all of our current
skills and a couple more.
3:21
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up