Custom Login View

閲覧: 68 回
最初の未読メッセージにスキップ

Happy Rob

未読、
2017/03/23 12:35:252017/03/23
To: web2py-users

Hi guys
I'm trying to make a membership site but I'm struggling a little and would love a little help
# 1. How can I select where go if login fails? Can I add something next to the 'next=URL' that's similar?
# 2. How can I select so uses c/b/b3 to login instead of c/default/user/login?
# 3. When I login, it logs me out instantly - how can I not logout instantly?

I'm on Pythonanywhere, and I've got an app called "c"
APP: c
------------------------------------------
MODEL: a.py...
db = DAL ('sqlite://storage.sqlite')
from gluon.tools import Auth
auth = Auth(db, controller='b')

auth.settings.controller = 'b'
auth.settings.on_failed_authorization = URL('c',args='b/b3')
auth.settings.login_url = URL(request.application, 'c', 'b3')
auth.define_tables(username=False,signature=False)
------------------------------------------
CONTROLLER: b.py...
#home page
def b1():
    display_page = XML('<h1>Home Page</h1>')
    return dict(display_page = display_page)

#regn page
def b2():
    display_page = XML('<h1>Registration Page</h1>')
    return dict(display_page = display_page, form=auth.register(next=URL('b3')))

#login page
def b3():
    display_page = XML('<h1>Login Page</h1>')
    return dict(display_page = display_page, form=auth.login(next=URL('b4')))
# 1. How can I select where go if login fails? Can I add something next to the 'next=URL' that's similar?

#members page
@auth.requires_login()
# 2. How can I select so uses c/b/b3 to login instead of c/default/user/login?
def b4():
    auth.settings.login_url = URL('b', 'b3')
    display_page = XML('<h1>Members Page</h1>')
    return dict(display_page = display_page, form=auth.logout(next=URL('b1')))
# 3. When I login, it logs me out instantly (and sends me to b1) - how can I not logout instantly?
------------------------------------------
VIEW: b/b1.html...
{{=display_page}}

VIEW: b/b2 and b3 and b4 are the same...
{{=display_page}}
{{=form}}
------------------------------------------

Any help would be greatly appreciated - especially with question 2.

Marlysson Silva

未読、
2017/03/24 10:10:002017/03/24
To: web2py-users
1. By analysing the web2py's code I find this line:

https://github.com/web2py/web2py/blob/master/gluon/tools.py#L1520

Basically you should define a url to redirect when login fails. And internally açthe parameter ( next=??) that you want to do.

https://github.com/web2py/web2py/blob/master/gluon/tools.py#L4309-L4311

Steps:

1.1 Search by "fail login" string.

https://github.com/web2py/web2py/blob/master/gluon/tools.py#L1520

1.2 Here it call the function call_or_redirect.

https://github.com/web2py/web2py/blob/master/gluon/tools.py#L4310

1.3 That call a function and redirect

https://github.com/web2py/web2py/blob/master/gluon/tools.py#L93

That you can pass a url in string format or a function URL() using parameters..

I don't was tested but I think that it so:

auth.settings.on_failed_authentication = URL("YOUR ACTION")
or
auth.settings.on_failed_authentication = "YOUR ACTION"

2. In these section have what you want.
:
http://web2py.com/books/default/chapter/29/09/access-control#Authentication

Especifically this code:

def mylogin(): return dict(form=auth.login())
def myregister(): return dict(form=auth.register())
def myprofile(): return dict(form=auth.profile())

You can expose auth action that youself created .

3. Logout instantly?

Happy Rob

未読、
2017/03/25 0:14:472017/03/25
To: web2py-users
SOLVED!!!!

Hi Marlysson

Thanks for your help

In the end I didn't need to do any defining or anything too much.
For 1 (failed login), I put the auth settings into the controller instead of the model
For 2. (selecting login page) I also put the auth settings into the controller
For 3. (logout) I just put the logout into a separate view.

So in the end, here is the complete membership site  - yay!


App: C

Model: a.py
..................................................................
db = DAL ('sqlite://storage.sqlite')
from gluon.tools import Auth
auth = Auth(db)
auth.define_tables(username=False,signature=False)
..................................................................

Controller: e.py
..................................................................

#setting stuff
auth.settings.controller = 'e'
auth.settings.on_failed_authorization = URL('e', 'b1')
auth.settings.login_url = URL('b3')

#home page
def b1():
    display_page = XML('<h1>Home Page</h1><a href="b2">Registration</a><br><a href="b4">Login to members</a>')
    return dict(display_page = display_page)

#regn page
def b2():
    display_page = XML('<h1>Registration Page</h1><a href="b4">or Skip this page and Login to members page</a>')
    return dict(display_page = display_page, form=auth.register(next=URL('b4')))

#login page
def b3():
    display_page = XML('<h1>Login Page</h1>')
    return dict(display_page = display_page, form=auth.login(next=URL('b4')))

#members page
@auth.requires_login()
def b4():
    display_page = XML('<h1>Members Page</h1><a href="b5">Logout</a>')
    return dict(display_page = display_page)

def b5():
    display_page = XML('<h1>Logging Out</h1>')
    return dict(display_page = display_page, form=auth.logout(next=URL('b1')))
..................................................................
View: e/b1............
{{=display_page}}
................................
View: e/b2............
{{=display_page}}
{{=form}}
................................
View: e/b3............
{{=display_page}}
{{=form}}
................................
View: e/b4............
{{=display_page}}
................................
View: e/b5............
{{=display_page}}
{{=form}}
................................
全員に返信
投稿者に返信
転送
新着メール 0 件