Can we track currently active sessions in Python Flask

感情迁移 提交于 2020-12-15 07:01:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!