How to get the list of users who are currently logged in Flask-Login?

前端 未结 1 951
生来不讨喜
生来不讨喜 2021-01-17 06:54

In my Flask-SQLAlchemy web application I have implemented Flask-Login for user authentication and authorization. Now I am building an Admin

1条回答
  •  灰色年华
    2021-01-17 07:46

    You will have to implement this feature yourself as there is not a way to do it natively with flask-login.

    I can think of myriad ways to do this, but the quickest is probably just keeping track of the last time a user accessed your site. Then you can set a reasonable timeout where you assume the user is not logged in if they haven't done anything for the last 30 minutes (or however long you feel comfortable with)

    For example:

    @app.before_request
    def update_last_active():
        current_user.last_active = datetime.utcnow()
        db.session.commit()
    

    This would update User.last_active every time the user sends a request to your server.

    0 讨论(0)
提交回复
热议问题