Skip to content

Logout View

Logging out means removing the user id from the session.

Flask-Login provides:

  • logout_user()
diagram logging out is about clearing state, not showing a page mermaid
logout_user removes the user id from the session. The remember-me cookie has to be cleared too, or the very next request logs them straight back in -- which is the bug people hit when they only pop the session key by hand.
python
from flask import redirect, url_for
from flask_login import logout_user, login_required
 
 
@app.route("/logout")
@login_required
def logout():
    logout_user()
    return redirect(url_for("home"))

It’s not strictly required, but:

  • it keeps behavior consistent
  • it avoids confusion when anonymous users hit /logout

It’s common to flash a message:

  • “You’ve been logged out.”

And redirect to home.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading