[Python][Flask] logout_user() doesn't need a user in flask-login

 logout_user() doesn't need a user in flask-login

While working on Flask's login/logout features, it's noticed that flask_login.logout_user() doesn't require an instance of the user while flask_login_login_user() needs one. This question led to then understanding on how Flask-login manages login state.

Explanation

Flask uses session based authentication. Each user that accesses the application gets a session, which Flask tracks using a secure cookie in the browser.

When a user logs in, the following is called.
login_user(user)

This tells Flask-Login that this session belongs to this specific user while storing the user's ID in the session. (so that the user can be retrieved by calling user_loader())

When a user logs out, the following is called.
logout_user()


This tells Flask-Login that any login info stored in this session should be removed. Flask only tracks the currently active session, which is why it doesn't require specifying the user. It already knows via current_user.

current_user

flask_login.current_user is a proxy variable that vies access to the user object for the current session only.

Comments