Redirect to previous user's last page after new user login
NickName:nsx Ask DateTime:2013-12-13T01:28:40

Redirect to previous user's last page after new user login

I am currently developing a Django website and I am using django.contrib.auth and django-security-session for closing user sessions automatically.

In case a user leaves its session open and a new user arrives at the same navigator after that session expired, if the new user attempts to use that same session, the session will be automatically closed. However, if now this new user logs in again, he is redirected to the last page where the previous user was.

I have taken a look at the code from django-security-session and I found the following at middleware.py "process_request":

from django.contrib.auth import logout
...
def process_request(self, request):
    ...
    delta = now - get_last_activity(request.session)
    if delta.seconds >= EXPIRE_AFTER:
        logout(request)
    ...

So it seems that django-security-session relies on django.contrib.auth for closing the session. This logout flushes the current session and removes user id's from the request. However, the current page for the user that has just being logged out is still preserved for redirection after the following login. This login, in my case, is performed by the decorator @login_required, which relies on method "django.contrib.auth.views.redirect_to_login":

def redirect_to_login(next, login_url=None, \
                        redirect_field_name=REDIRECT_FIELD_NAME):
    ...
    resolved_url = resolve_url(login_url or settings.LOGIN_URL)
    login_url_parts = list(urlparse(resolved_url))
    if redirect_field_name:

        querystring = QueryDict(login_url_parts[4], mutable=True)
        querystring[redirect_field_name] = next
        login_url_parts[4] = querystring.urlencode(safe='/')

    return HttpResponseRedirect(urlunparse(login_url_parts))

... where the page for redirection is generated utilizing the url from the last user that is already logged out and that might not be the same as the one that is logging in right now.

Before starting freaking out around the code, is there anything I am missing? Is there anyway in which I can instruct django.contrib.auth not to do this and, in case a different user logs in, just redirect the new user to its home page?

Copyright Notice:Content Author:「nsx」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20550170/redirect-to-previous-users-last-page-after-new-user-login

More about “Redirect to previous user's last page after new user login” related questions

ZK - redirect the user back to previous page

Any ideas on how to redirect a user back to the previous page cleanly and robustly? I'm only concerned about redirecting them back if the previous page was within the app itself. I've thought about

Show Detail

redirect user back to previous page after authenticating user

I can't seem to figure out how to return to previous page after authenticating the user. I tried return RedirectToLocal(returnUrl) but that always takes the user back to the main home index page. ...

Show Detail

How to redirect WordPress user to previous page after login

I have a WordPress site where some/most of the pages can be viewed by anyone (not logged in). However a user can request to have their own private page, so I set up a page that can only be accesse...

Show Detail

Laravel - Redirect user to previous page after successful login

I am trying to redirect the user back to the previous page after a successful login. For that, in the LoginController.php I added a redirectTo() function as such : protected function redirectT...

Show Detail

How to Redirect user to Previous URL After registering in Laravel

Using the built in Auth system in Laravel 5.6, I have multiple pages on my website where I need a user to register or login to perform an action such as making a purchase, etc... I have my login

Show Detail

Redirect user to previous page

I want to redirect users to the page they came from , but not using javascript go.back and stuff like that , because it doesn't load the page again . I found some code <?php $ref1 = urlencode($

Show Detail

After session timeout redirect to last visited page for previous user

When the session is timeout, I redirect to login page. I need that, whether the login user name is same user name, which was logged in before session timeout, then the page will be redirect to prev...

Show Detail

Wordpress - Redirect to previous page after login

On Woocommerce for Wordrpress, when you're on a product page and click to login, when you're login, you're redirected to your Dashboard page from your account and not the previous page that you was

Show Detail

Laravel If user is not logged redirect to login, after login redirect him to previous route

I have a route that is protected by Auth::check(). So, if it passes, user can access it, if no - user is redirect()->route('login); When user enters his credentials I want to redirect him back ...

Show Detail

redirect_to the previous page but changing the language

What am I trying to do? To redirect the user in the previous page with the website translated in new language selected My previous script was (language_controller): class LanguagesController <

Show Detail