How do I prevent going back to previous page after user is logged out?

52 views
Skip to first unread message

Warwick JC

unread,
Jul 27, 2020, 3:00:58 PM7/27/20
to web2py-users
I have a page that when logging in from a previous logout, the login page redirects back to the same page. 

I want to have a default redirect to the index page instead.

Using
auth.settings.login_next = URL('default','index')

does not help.

  1. Is there a better way out in js or some form of settings.
  2. Is there a way of restricting this feature to specific pages only? such as if a user is in natgeo.html, gets logged out after expiration time, should be redirected to index.html and not back to natgeo.html.

Annet

unread,
Jul 31, 2020, 4:49:16 AM7/31/20
to web2py-users
What about the following?

auth.settings.login_onaccept = lambda form: __on_login()
auth.settings.logout_onlogout = lambda user: __on_logout()


def __on_login():
    # whatever you want to do
    return None


def __on_logout():
    # whatever you want to do   
    return None

To solve your problem use the request object to redirect


HTH,

Annet

Warwick JC

unread,
Aug 2, 2020, 5:59:25 AM8/2/20
to web...@googlegroups.com
Hi
Thanks for this.
I put these functions in the model.py, I hope its correct.

def __on_login():
    redirect(URL('index'))
    return None

def __on_logout():
    redirect(URL('index')) 
    return None

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/e80c749a-cc2e-496f-89bd-7de4c0f1ef2fo%40googlegroups.com.

Rahul

unread,
Aug 3, 2020, 2:01:11 PM8/3/20
to web2py-users
Another simple way is  --
To pop out session variables like in code below for all the login session variables and redirect the user wherer-ever you want them to be.
This way since no valid session exists the user would be redirected to page where you intend them to be.

def sign_out():
    session.pop('user_name', None)
    redirect(URL(r=request, f='login'))
    return dict()

Regards,

Rahul Dhakate

Warwick JC

unread,
Aug 4, 2020, 1:54:22 AM8/4/20
to web...@googlegroups.com
Thanks guys.
They both work well. 
Regards 

Reply all
Reply to author
Forward
0 new messages