Authentication Problem !

20 views
Skip to first unread message

Yannick

unread,
Feb 22, 2009, 1:04:43 PM2/22/09
to web2py Web Framework, mdip...@cs.depaul.edu
Hello mate,
Here is what I have in my controller:

def login():
formlog = auth.login(next="profile")
return dict(form=formlog)

@auth.requires_login():
def profile():
if not auth.is_logged_in():
return dict(form=auth.login(next="login"))
else:
return dict(msg="Welcome")

I have two problem here:
- First; here basically I'm just trying to login and if the login
failed the page is redirect to the login page with a feedback
message... when the login Authentication pass I can successfully
access the Profile page with no problem BUT when the login
Authentication failed I'm redirect to this link "http://127.0.0.1:8000/
App../default/user/login" with a message "invalid function". I
understand the message because i don't have any action named "User"
define on my controller. I'm wondering why i got redirect to that
link ? is there something I'm doing wrong in my code ? I guess is how
the tools.py is internally implement and i understand that we must not
change this code... Is there something i can do to fix that problem in
my code ??

- Second; the "@auth.requires_login(): " doesn't seems to work since I
have an error message on the colon and even when i remove the colon i
have no error message but still can able to access the profile
action.... Any idea please ?

Do you please have a better way of doing what I'm trying to solve.

Thanks for your attention.

mdipierro

unread,
Feb 22, 2009, 3:07:58 PM2/22/09
to web2py Web Framework
In your code

@auth.requires_login()
def profile():
if not auth.is_logged_in():
return dict(form=auth.login(next="login"))
else:
return dict(msg="Welcome")

because of

@auth.requires_login()

auth.is_logged_in() is always true

The validator, is user is not logged in, redirects to

auth.settings.login_url

you can set it

auth.settings.login_url=URL(r=request,f='login')

Hope this answers the question.

Massimo

Yannick

unread,
Feb 22, 2009, 3:52:09 PM2/22/09
to web2py Web Framework
Oh Thanks Massimo I completely forgot about the
"auth.settings.login_url...".
Here my new code:

auth.settings.login_url=URL(r=request,f='login')

def login():
return dict(form=auth.login(next="profile"))

@auth.requires_login()
def profile():
return dict(Welcome="Welcome")

How can I retrieve the response.flash() message in this case to let
the user know that the login failed since here i recreate a new
request and i guest the rsponse is lost... Do you have any forwarding
mechanism ?

In the login.html file i have this:
{{if response.flash:}}{{=response.flash}}{{pass}}

I guess I don't retrieve the response.flash anymore because it is the
new request.

Do you have any idea how to print the flash message when the login
failed based on my code? or is there any other solution more
efficient ?

Thanks for your help !

mdipierro

unread,
Feb 22, 2009, 4:32:36 PM2/22/09
to web2py Web Framework
I am not sure I fully understand so I give you two answers.

A) Before redirection you should set

session.flash="message"

instead of

response.flash="message"

The former is copied in the latter automatically before redirection.

B) The messages generated by the auth method all set session.flash
before redirection. The messages are customizable. You can find a list
in gluon.tools.py

Massimo

Yarko Tymciurak

unread,
Feb 22, 2009, 8:21:46 PM2/22/09
to web2py Web Framework
yes, and set session flash in your controller (not your view);

Yannick

unread,
Feb 22, 2009, 10:05:48 PM2/22/09
to web2py Web Framework
Thanks Massimo and Yarko,
The problem here is the redirect is done implicitly using the
decorator pattern... It is done inside the method "requires_login()"
of tools.py. I guess one solution should have been to put the
session.flash message inside the "requires_login()" BUT I'm not allow
to do so...
Do you see what i mean ?

thanks !

mdipierro

unread,
Feb 23, 2009, 1:34:38 AM2/23/09
to web2py Web Framework
Just set these:

auth.messages.invalid_login
auth.messages.logged_in

Massimo

On Feb 22, 9:05 pm, Yannick <ytchatch...@gmail.com> wrote:
> Thanks Massimo and Yarko,
> Theproblemhere is the redirect is done implicitly using the
> > > > > > access the Profile page with noproblemBUT when the login

Yannick

unread,
Feb 23, 2009, 9:15:52 PM2/23/09
to web2py Web Framework
Thanks Massimo, I appreciate your help but i think maybe i don't
understand or there is something I'm doing wrong... Here is my code
please let me know where I'm wrong because i did set
"auth.messages.invalid_login"

auth.settings.on_failed_authorization=URL(r=request,f='error')
auth.settings.login_url=URL(r=request,f='login')
auth.messages.invalid_login= 'Bad Login'
auth.messages.logged_in = 'Perfect'

def login():
return dict(form=auth.login(next="profile"))

@auth.requires_login()
def profile():
return dict()


You said i should set "session.flash" before redirect... Where should
I set it since the redirect is done implicitly inside tools.py...
I really want to understand how to make this work without touching
anything in the tools.py code..

Thanks for your attention !

Yannick P.

mdipierro

unread,
Feb 24, 2009, 1:00:37 AM2/24/09
to web2py Web Framework
Your example works great for me and I get the flash (bad login or
perfect).

Send me the whole app (perhaps something else is messing this up) but
let me know in detail what the problem is. Perhaps you expect
something different than I do.

massimo

Yannick

unread,
Feb 26, 2009, 9:10:41 PM2/26/09
to web2py Web Framework
Hello Massimo,
I just sent you the application.
I expect to see a feedback message when the user failed to login.

Thanks for your help and attention.

Cheers,
Yannick P.

Yannick

unread,
Feb 28, 2009, 11:39:16 AM2/28/09
to web2py Web Framework
Thanks Massimo, it work fine but for some reason I can't make it work
with MySql Server DB... Here is the error message I get, please look
this thread:
http://groups.google.com/group/web2py/browse_thread/thread/f24f80e9e299aa88


Thanks,
Yannick P.
Reply all
Reply to author
Forward
0 new messages