How to prevent web2py redirect if login is not valid?

317 views
Skip to first unread message

thiago...@comp.ufla.br

unread,
Feb 9, 2014, 1:51:04 PM2/9/14
to web...@googlegroups.com
The problem is that I have a page for the login form, that only can redirect to the home page if the login is valid. But, I can't use @auth.requires_login() because, the home page can be accessed by visitors and in this app just pages like profile cannot be accessed by visitors.

In this case, why the web2py redirects to the home page  even if login was not valid, when I don't use @auth.requires_login()?

How to prevent web2py redirect if login is not valid?

thiago...@comp.ufla.br

unread,
Feb 10, 2014, 8:23:46 AM2/10/14
to web...@googlegroups.com
Anyone?

Anthony

unread,
Feb 10, 2014, 9:02:46 AM2/10/14
to web...@googlegroups.com
You'll have to show some code. By default, web2py does not redirect anywhere when login fails -- it just reloads the login page.

Anthony

thiago...@comp.ufla.br

unread,
Feb 10, 2014, 9:17:39 AM2/10/14
to web...@googlegroups.com
Ok, no problem.

Obs.: perfil <=> profile, autenticacao <=> authentication

On db.py I have this login settings:

## login settings
auth.settings.login_url = URL('autenticacao', 'index')
auth.settings.logout_next = URL('autenticacao', 'index')
auth.settings.logged_url = URL('default', 'perfil')

---------------
On index function from the controller autenticacao I have this code:

def index():
    return dict(form_login=auth.login())

---------------
On index, and perfil functions from the controller default I have this:

def index():
    return dict()

@auth.requires_login()
def perfil():
    usuario = db(db.auth_user.id==auth.user_id).select()[0]
    usuario.nome_completo = ' '.join([usuario.first_name, usuario.last_name])
    fazendas = db(db.fazendas.usuario==auth.user_id).select()
    return dict(
        nome_completo=usuario.nome_completo.strip(),
        email=usuario.email.strip(),
        rg=usuario.rg.strip(),
        cpf=usuario.cpf.strip(),
        data_nascimento=usuario.dataNascimento.strip(),
        fazendas=fazendas
    )
Message has been deleted

Tim Richardson

unread,
Feb 10, 2014, 2:22:32 PM2/10/14
to web...@googlegroups.com
in your controller, you can access the global variable auth.

You can ask
auth.is_logged_in() is True if the user is logged in, for example.
auth.user returns a Row telling you who is logged in.

http://web2py.com/books/default/chapter/29/09/access-control#Authorization




Thiago Nobre

unread,
Feb 10, 2014, 2:33:11 PM2/10/14
to web...@googlegroups.com
I already know auth.is_logged_in(), but unfortunately when the user enter like visitor, he's not logged in, he just entered at the site without a login.

I need something like a login_onreject, because the visitor actually don't sign up.

=/


--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/s2jOtL0niZM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Anthony

unread,
Feb 10, 2014, 4:58:40 PM2/10/14
to web...@googlegroups.com
Can you be more clear about what is happening? Are you saying if someone goes to the login page and enters incorrect credentials, they are being redirected to the index page? Auth shouldn't be doing that by default. Can you show your code?

Thiago Nobre

unread,
Feb 10, 2014, 5:47:58 PM2/10/14
to
Yeah, it's exactly what's going on and I don't know why. I attached the db.py file and the controllers used on the problem, as you requested.

To be more clear, I'll try to explain again:

The problem is that I want that just some pages are blocked to the visitors, so I can't use @auth.requires_login() in the index function of the default controller. But if I don't use this decorator, when the login is not valid occurs an undesirable redirect to the home page, instead of continue in the login page until the user enters with the correct name and password, or until this one enters like a visitor.

And sorry for the troubles on my confused explanations, it's hard to me speak in english.
code_files.rar

thiago...@comp.ufla.br

unread,
Feb 12, 2014, 3:09:43 PM2/12/14
to
There is no way to check if the user has entered incorrect credentials (login was rejected) and treat it my way?

Anthony

unread,
Feb 12, 2014, 7:26:45 PM2/12/14
to web...@googlegroups.com
If you change the Auth controller from 'default', you must specify that:

auth = Auth(db, controller='autenticacao')

Anthony

Thiago Nobre

unread,
Feb 12, 2014, 7:49:23 PM2/12/14
to web...@googlegroups.com
It's just that simple? I didn't imagine that was the reason of the strange login behavior. When I get home, I'll test it and tell you if it worked.


--

thiago...@comp.ufla.br

unread,
Feb 12, 2014, 8:54:28 PM2/12/14
to web...@googlegroups.com
It worked! Thank you!
Reply all
Reply to author
Forward
0 new messages