Logout View
Logging out means removing the user id from the session.
Flask-Login provides:
logout_user()logout_user()
Example
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"))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"))Why protect logout with login_required?
It’s not strictly required, but:
- it keeps behavior consistent
- it avoids confusion when anonymous users hit
/logout/logout
UX note
It’s common to flash a message:
- “You’ve been logged out.”
And redirect to home.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
