Full Web Apps with FastAPI Transcripts
Chapter: Users and HTML forms
Lecture: Logging out
0:00 Let's take this whole experience full circle and add the ability to log out. So we're
0:04 able to register, then we're logged in.
0:06 How do we thought being logged in?
0:08 How do we do this logout?
0:09 Well, it turns out it's incredibly simple.
0:11 So when we logged in, what we did is we created this response and then
0:15 we set some cookie. So here,
0:17 when when you do a GET,
0:18 it's just going to do the opposite of that.
0:20 So we're gonna come along and grab this.
0:24 It's quite similar. And instead of sending them to their account,
0:28 which doesn't make sense because they're not logged in,
0:30 let's just send them back to the home page.
0:32 And we're gonna again use the 302 so we don't do a POST,
0:35 we do a GET. And over here,
0:37 this actually has a logout where you just pass in the response.
0:42 If you look at that, it's incredibly simple.
0:44 It just goes to response, delete cookie,
0:46 the name of whatever we called the cookie.
0:50 Let's go ahead and try to rerun this,
0:52 see if we can now log out.
0:53 We click here, we're still logged in,
0:55 we still have the cookie, we go to uvicorn, we're still logged in
0:59 got to account. But now,
1:00 if I hit log out, it should delete that cookie and send us home.
1:03 Let's see if we got it right.
1:05 Boom! Cookies deleted, logged in info is gone.
1:09 And now we're back home where we're meant to be.
1:11 Right? That's it. So we've got our GET > POST > Redirect pattern.
1:14 Think in the form, submitting it, once we're registered,
1:17 and validated, we create the account, we set the cookie, hangs around for a while until
1:20 we log out and then it's gone.