Full Web Apps with FastAPI Transcripts
Chapter: Users and HTML forms
Lecture: Logging out
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's take this whole experience full circle and add the ability to log out. So we're able to register, then we're logged in.
0:07
How do we thought being logged in? How do we do this logout? Well, it turns out it's incredibly simple.
0:12
So when we logged in, what we did is we created this response and then we set some cookie. So here, when when you do a GET,
0:19
it's just going to do the opposite of that. So we're gonna come along and grab this. It's quite similar. And instead of sending them to their account,
0:29
which doesn't make sense because they're not logged in, let's just send them back to the home page.
0:33
And we're gonna again use the 302 so we don't do a POST, we do a GET. And over here, this actually has a logout where you just pass in the response.
0:43
If you look at that, it's incredibly simple. It just goes to response, delete cookie, the name of whatever we called the cookie.
0:51
Let's go ahead and try to rerun this, see if we can now log out. We click here, we're still logged in,
0:56
we still have the cookie, we go to uvicorn, we're still logged in got to account. But now,
1:01
if I hit log out, it should delete that cookie and send us home. Let's see if we got it right. Boom! Cookies deleted, logged in info is gone.
1:10
And now we're back home where we're meant to be. Right? That's it. So we've got our GET > POST > Redirect pattern.
1:15
Think in the form, submitting it, once we're registered, and validated, we create the account, we set the cookie, hangs around for a while until
1:21
we log out and then it's gone.