问题
In my Flask Web-app, a new session is created whenever a new user logs in. At the server-end, is there a way to track the currently active sessions?
回答1:
If you are using Flask-Login for your user session management then is_authenticated
property of Flask-login tells you if the user is logged in or not:
if not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
If you want to protect your views you can use @login_required
decorator. By default, when a user attempts to access a login_required view without being logged in, Flask-Login will flash a message and redirect them to the login view. (If the login view is not set, it will abort with a 401 error.)
@app.route("/settings")
@login_required
def settings():
pass
See the documentation
来源:https://stackoverflow.com/questions/58300073/can-we-track-currently-active-sessions-in-python-flask