0

I have a view on my Django website which has a Bootstrap button that performs an important operation, operation which requires the user to be logged in.

However, on our site the cookies have a lifetime of 15 minutes, meaning that a user which is logged in, goes for a cup of tea, and then comes back and clicks the button will think that the operation has gone through, when in fact he/she will be logged out.

Is there any way to check via JQuery or otherwise if the cookies have expired, and disable the button if this is the case?

Thanks,

Alberto

Alberto Deca
  • 155
  • 1
  • 10

2 Answers2

1

It sounds like you need to track how long the user is idle. When the user's session has expired, you probably just want to load the login screen, no?

Here's a solution that reloads the page after 20 minutes of idle time. If your django app redirects the user to the login screen when the session has expired, then the solution could work for you.

Detecting idle time in JavaScript elegantly

Community
  • 1
  • 1
Joe Banks
  • 21
  • 1
  • sorry I'm a bit of a JQuery noob. Would it work if the user was browsing a different site or doing something different on his computer? – Alberto Deca Jan 20 '17 at 12:16
1

You could write a view that checks if the user is authenticated and have jQuery performing periodical ajax call to it. If the view returns a 'not logged in' response, reload the page or do something else to hide the button.

Here's a quick example:

#The view
def check_if_logged_in(request):
    if request.user.is_authenticated:
        return HttpResponse('logged-in')
    else:
        return HttpResponse('not-logged-in')

#The jQuery
function ajax_check(){
    $.get(
        'the-view-url'
    ).done(function(response){
        if (response === 'not-logged-in'){
            location.reload(true);
        }
    }).fail(
        // something else
    );
}

setInterval(ajax_check, 15000)
4140tm
  • 1,561
  • 13
  • 16
  • That's a good idea. I tried it, but the problem is if the user is inactive and the session has expired, it is still returning logged in. Cannot understand why, in the meantime I'm using "login_required()" decorator to make clear to the user that he has to log in again. – Alberto Deca Jan 23 '17 at 17:13