intercepting login & logout events

66 views
Skip to first unread message

Vlad

unread,
Jun 17, 2019, 9:19:43 AM6/17/19
to web2py-users
What would be the simplest way to intercept login and logout? 
(i.e. I'd like to perform some additional tasks when a user is logged in, and when a user is logged out.)
web2py elegantly hides the functionality in default.user under form=auth() and {{=form}} and somehow I can't think of a simple way to be notified about successful login and logout actions... 

Yoel Benitez Fonseca

unread,
Jun 17, 2019, 9:44:10 AM6/17/19
to web2py
I think you can do it in the default/user function. Having in account that the call to auth() probably raises a HTTP exceptiong in case of a success. This example is only a proof of concept:

def user():
    form = CAT()
    
    try:
        form = auth()
    except HTTP as e:
        if e.status == 303'login' in request.args:
            if ('login' in request.args) and auth.user:
                # probably a success, u can test if auth.user is o not None
                do_your_loging_event_here() 
            elif 'logout' if request.args:
                do_your_logout_event_here()
    
    return dict(form=form)


--
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/b9dbb7bb-4500-48b2-82fd-cd8ea644f1fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Msc. Yoel Benítez Fonseca






Yoel Benitez Fonseca

unread,
Jun 17, 2019, 9:50:23 AM6/17/19
to web2py-users
Got a syntax error there, sorry:


El lunes, 17 de junio de 2019, 9:44:10 (UTC-4), Yoel Benitez Fonseca escribió:
I think you can do it in the default/user function. Having in account that the call to auth() probably raises a HTTP exceptiong in case of a success. This example is only a proof of concept:

def user():
    form = CAT()
    
    try:
        form = auth()
    except HTTP as e:
        if e.status == 303:

Vlad

unread,
Jun 17, 2019, 9:57:12 AM6/17/19
to web2py-users
but isn't such a scenario catching a user in a process of logging in or logging out, but before actual login/logout?
To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

Yoel Benitez Fonseca

unread,
Jun 17, 2019, 10:16:58 AM6/17/19
to web2py
no, it is not.

The first time the default/user is called - in the login process for example - there not a HTTP(303) (redirect) exception, and the user get the form.
The second time, in this scenery, the call to auth() will cause a redirect (HTTP exception) and the user is effectively logged into the system.

The same goes for the logout, but there is not a form in the middle.

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/ee8abe78-346c-44dd-b8a6-e9b243e4eb90%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Vlad

unread,
Jun 17, 2019, 10:31:03 AM6/17/19
to web2py-users
Yoel, where could I look up how this works about the exception 303? Because I don't understand that part at all - I am not aware of exception being raised, why, and how it works in the flow - could you please point me to the direction where it's explained? 

Yoel Benitez Fonseca

unread,
Jun 17, 2019, 11:02:13 AM6/17/19
to web2py
Vlad, in the book:


And i can comment out +/- the code:

def user():
    # just a way to ensure form allways have a value
    form = CAT()
    
    try:
        # the call to auth can return a lot of things
        # it can be a form (a login form for example or a registration form)
        # it can raise a redirect - for exple if the login is correct
        # it can be the login form with erros if the password is incorrect
        # etc
        form = auth()

    except HTTP as e:
        if e.status == 303'login' in request.args:
            if ('login' in request.args) and auth.user:

                # probably a success, the user has logged in
                do_your_loging_event_here() 
                # ensuere u do the redirect, for example a raise shoul do it
            
elif 'logout' if request.args:
                do_your_logout_event_here()
    
    # give the form to the view, can be register form, login form, etc
    return dict(form=form)


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/237c7ee1-72d9-4f3b-a3f5-f09fd34459ad%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Vlad

unread,
Jun 17, 2019, 11:06:15 AM6/17/19
to web2py-users
thank you ! 

Annet

unread,
Jun 18, 2019, 2:25:18 AM6/18/19
to web2py-users
Web2py has auth.settings I used them to solve your problem


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

and then in the functions the task you want to perform, in my case
some set up things and messages, something like:


def __on_login():
    # set up code

    # messages
    if auth.user.first_name:
        name = auth.user.first_name + ' '
    elif auth.user.title:
        name = auth.user.title + ' '
    if auth.user.familyNamePreposition:
        name += auth.user.familyNamePreposition + ' '
    name += auth.user.last_name

    session.messages_logged_out = 'You have successfully logged out, goodbye %s' %(name)
    session.flash =  'You have successfully logged in, welcome %s' %(name)

    return None


def __on_logout():
    if session.forced_logout:
        auth.messages.logged_out = session.forced_logged_out_message
    else:
        auth.messages.logged_out = session.messages_logged_out

    return None


Kind regards,

Annet

Eliezer (Vlad) Tseytkin

unread,
Jun 18, 2019, 6:58:43 AM6/18/19
to web...@googlegroups.com
This is perfect,
I missed it,
Thank you!!

--
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/sai3zh46gvQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/4cb15666-ee77-41c9-b769-70324770b90e%40googlegroups.com.

Yoel Benitez Fonseca

unread,
Jun 18, 2019, 9:12:06 AM6/18/19
to web2py-users
missed that one
Reply all
Reply to author
Forward
0 new messages