Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine when user logs via a regular login or via a remember_me

I would like to store the time and date for each user log in. Putting this code into the regular login view is simple.

The problem arises when Flask-Login's remember_me is used - login is no longer called giving me no way of knowing if the login is fresh.

I have tried the user_logged_in, user_login_confirmed signals provided by Flask-Login:

 def  user_logged_in_callback(*args, **kwargs):
      import ipdb;ipdb.set_trace()

 user_logged_in.connect(user_logged_in_callback, app)
 user_login_confirmed.connect(user_logged_in_callback, app)

user_logged_in is only called on regular logins and user_login_confirms doesn't seem to be called at all.

like image 561
TheOne Avatar asked Oct 26 '25 03:10

TheOne


1 Answers

Getting the user again after login is called "user load" and is not really a login event, it's just the same user still. There is a signal for it, however it's not documented.

You can use the user_loaded_from_cookie signal:

from flask_login import user_loaded_from_cookie

user_loaded_from_cookie.connect(user_login_callback, app)

user_login_confirm is for when a login is refreshed - a fresh login is required and the user re-enters their password.


If all you want to do is record when a user last visited the site, you can just use app.before_request, you don't need to deal with the login events.

@app.before_request
def track_user():
    if current_user.is_authenticated():
        current_user.last_seen = datetime.utcnow()
like image 119
davidism Avatar answered Oct 29 '25 03:10

davidism



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!