Django and login "remember me": How can I view that status for other code?

Viewed 234

I am using Django with the Allauth login code (and still a bit wet) to perform authentication. It appears that if you check "remember me", it does the following in the allauth login form:

allauth forms.py (class LoginForm)

if remember:
        request.session.set_expiry(app_settings.SESSION_COOKIE_AGE)
    else:
        request.session.set_expiry(0)
    return ret

I am trying to use the "remember me" for other features in my app. It appears that the only 'flag' I have to determine if a user wants to be remembered or not is the above value.

In my view code, I tried to read this 'flag' with the following command:

request.session.get_expiry_age()

Unfortunately, it always returns 1209600. Whether the user checks the "remember me" box or not, this value is 1209600.

I need to find this flag so that I can use it in several places. What system variable is set and exposed when a user checks this 'Remember me' box that I am not seeing?

1 Answers

Reading set_expiry and get_expiry_age() will be helpful.

Relevant parts:

set_expiry(value)

If value is 0, the user’s session cookie will expire when the user’s Web browser is closed.


get_expiry_age()

Returns the number of seconds until this session expires. For sessions with no custom expiration (or those set to expire at browser close), this will equal SESSION_COOKIE_AGE.

It indicates that when we do:

request.session.set_expiry(0)

Below part:

request.session.get_expiry_age()

Will return SESSION_COOKIE_AGE.


Looking at the source code will help.

Related