[web2py] Re: How Auth works
If you are ok digging through code, in mostly all of my apps I use a CustomAuthntication.py module that is basically a class that mimics web2py's but it is very thin compared to the stock's, I use it this way not because 'mine is better' but because I wanted to control my registration and authentication more closely to my own apps. If you want, take a look at https://bitbucket.org/speedbird/qastack/src/da67a4653854/modules/CustomAuthentication.py this is qa-stack's authentication core, the authenticate() method is really the core and I do use a custom user's table, overall it is not too far from web2py's internal authentication, but just to show how you can implement your own authentication system, controlling every aspect of the user registration/login experience without having to 'reinvent the wheel'. Cheers, Julio On Thursday, February 23, 2012 9:48:49 PM UTC-8, davidkw wrote: Thanks, I wasn't trying to do anything specific. I'm just going through the tutorial and trying to understand how stuff works. Thanks for the link though. I hadn't found the settings info before, and it's very helpful. On Feb 24, 12:19 am, Anthony abasta...@gmail.com wrote: On Thursday, February 23, 2012 11:57:49 PM UTC-5, davidkw wrote: Hmm, I'm still having trouble creating separate actions and views for auth. The ones I create don't seem to override the default ones. Is there any way I can expose auth as a controller? And rewrite the methods in there? No need to do that. Show some code and we'll see if we can figure it out. Note, you'll need to change any links to the old URLs to point to your new actions. You also have to set auth.settings.login_url to your new login action so Auth knows where to redirect when a login is required. Seehttp://web2py.com/books/default/chapter/29/9#Settings-and-messages. Anthony
[web2py] Re: How Auth works
The user() function is called with an additional argument in the URL (e.g., /user/login, /user/register, etc.). This extra argument is accessible via request.args[0]. Now, the Auth class has a __call__ method, so the auth object is callable. When you call it via auth(), the __call__ method essentially acts like a router -- it reads request.args[0] and then calls whatever Auth method is specified (or it redirects to /user/login if there is no request.args[0]). You can see this here: http://code.google.com/p/web2py/source/browse/gluon/tools.py#1143. In particular: return getattr(self,args[0])() gets the Auth method named in args[0] and calls that method. Note, you can also manually create your own actions (or do your own routing based on request.args) and call the individual Auth methods directly. For example, you could create a dedicated register action via: def register(): return dict(form=auth.register()) Crud works in a similar way. Anthony On Thursday, February 23, 2012 8:34:47 PM UTC-5, davidkw wrote: I'm reading through the tutorial and just came across the part that adds the code: from gluon.tools import Auth auth = Auth(db) auth.define_tables() def user(): return dict(form=auth()) And suddenly login, register, etc were enabled. This is the first magic I've come across in web2py that I don't understand well. I understand that auth.define_tables() creates the tables for the user data, but I'm not sure how def user():... suddenly adds all the registration and login pages. Could someone give me a quick walkthrough of what's going on?
[web2py] Re: How Auth works
Thanks. I'm also wondering where the exposed URL's are located (/user/ login, /user/register, etc.). Can I customize these views? On Feb 23, 9:34 pm, Anthony abasta...@gmail.com wrote: The user() function is called with an additional argument in the URL (e.g., /user/login, /user/register, etc.). This extra argument is accessible via request.args[0]. Now, the Auth class has a __call__ method, so the auth object is callable. When you call it via auth(), the __call__ method essentially acts like a router -- it reads request.args[0] and then calls whatever Auth method is specified (or it redirects to /user/login if there is no request.args[0]). You can see this here:http://code.google.com/p/web2py/source/browse/gluon/tools.py#1143. In particular: return getattr(self,args[0])() gets the Auth method named in args[0] and calls that method. Note, you can also manually create your own actions (or do your own routing based on request.args) and call the individual Auth methods directly. For example, you could create a dedicated register action via: def register(): return dict(form=auth.register()) Crud works in a similar way. Anthony On Thursday, February 23, 2012 8:34:47 PM UTC-5, davidkw wrote: I'm reading through the tutorial and just came across the part that adds the code: from gluon.tools import Auth auth = Auth(db) auth.define_tables() def user(): return dict(form=auth()) And suddenly login, register, etc were enabled. This is the first magic I've come across in web2py that I don't understand well. I understand that auth.define_tables() creates the tables for the user data, but I'm not sure how def user():... suddenly adds all the registration and login pages. Could someone give me a quick walkthrough of what's going on?
[web2py] Re: How Auth works
On Thursday, February 23, 2012 10:00:37 PM UTC-5, davidkw wrote: Thanks. I'm also wondering where the exposed URL's are located (/user/ login, /user/register, etc.). Can I customize these views? The welcome app includes a /default/user.html view, which you can edit. You can also create separate views for different actions if desired. Anthony
[web2py] Re: How Auth works
Hmm, I'm still having trouble creating separate actions and views for auth. The ones I create don't seem to override the default ones. Is there any way I can expose auth as a controller? And rewrite the methods in there? Thanks. On Feb 23, 10:08 pm, Anthony abasta...@gmail.com wrote: On Thursday, February 23, 2012 10:00:37 PM UTC-5, davidkw wrote: Thanks. I'm also wondering where the exposed URL's are located (/user/ login, /user/register, etc.). Can I customize these views? The welcome app includes a /default/user.html view, which you can edit. You can also create separate views for different actions if desired. Anthony
[web2py] Re: How Auth works
Apparently it's sort of possible but I don't think for the feint of heart: https://groups.google.com/forum/#!searchin/web2py/override$20auth/web2py/dBRbPxRw8uQ/DLgJs-PgwAQJ It is possible to manipulate from things in the user() function. For example, mine looks like this: def user(): if not is_pricetack(): redirect('https://pricetack.com/%s' % request.env.web2py_original_uri) form = auth() auth.messages.reset_password = \ 'Click on the link https://' + request.env.http_host + \ URL('default','user',args=['reset_password']) + \ '/%(key)s to reset your password' if auth.user: response.cookies['login_email'] = auth.user.email response.cookies['login_email']['expires'] = 24 * 3600 response.cookies['login_email']['path'] = '/' if 'login_email' in request.cookies and request.args(0) in ['login', 'request_reset_password']: form.element(_name='email').update(_value=request.cookies['login_email'].value) return dict(form=form) Auth is pretty complicated so not super easy to override: http://code.google.com/p/web2py/source/browse/gluon/tools.py#751
[web2py] Re: How Auth works
On Thursday, February 23, 2012 11:57:49 PM UTC-5, davidkw wrote: Hmm, I'm still having trouble creating separate actions and views for auth. The ones I create don't seem to override the default ones. Is there any way I can expose auth as a controller? And rewrite the methods in there? No need to do that. Show some code and we'll see if we can figure it out. Note, you'll need to change any links to the old URLs to point to your new actions. You also have to set auth.settings.login_url to your new login action so Auth knows where to redirect when a login is required. See http://web2py.com/books/default/chapter/29/9#Settings-and-messages. Anthony
[web2py] Re: How Auth works
Thanks, I wasn't trying to do anything specific. I'm just going through the tutorial and trying to understand how stuff works. Thanks for the link though. I hadn't found the settings info before, and it's very helpful. On Feb 24, 12:19 am, Anthony abasta...@gmail.com wrote: On Thursday, February 23, 2012 11:57:49 PM UTC-5, davidkw wrote: Hmm, I'm still having trouble creating separate actions and views for auth. The ones I create don't seem to override the default ones. Is there any way I can expose auth as a controller? And rewrite the methods in there? No need to do that. Show some code and we'll see if we can figure it out. Note, you'll need to change any links to the old URLs to point to your new actions. You also have to set auth.settings.login_url to your new login action so Auth knows where to redirect when a login is required. Seehttp://web2py.com/books/default/chapter/29/9#Settings-and-messages. Anthony