I solved the ajax login the follwing way

controllers/user.py
def login():
    '''
    ajax login function
    return login form error/success message and serialized user 
    as JSON object 
    '''
    form = SQLFORM.factory(
        Field('email', requires=[IS_EMAIL()]),
        Field('password', 'password')
        )
    user = dict()
    message = ''
    success = None
    if form.accepts(request.vars, session):
        success = auth.login_bare(form.vars.email, form.vars.password)
        if success:
            from gluon.contrib import simplejson
            if not auth.is_logged_in():
                return simplejson.dumps({'id': None})
            user = dict([(k, v)
                        for k, v in auth.user.iteritems() if k in 'first_name 
last_name email id'.split()])
            user['groups'] = [row.auth_group.role for row in db(db.
auth_membership.user_id == auth.user.id)(
                db.auth_membership.group_id == db.auth_group.id).select()]
            success = True;
        else:
            message = 'invalid login'
    from gluon.contrib import simplejson
    return simplejson.dumps({'user': user, 'form': str(form), 'message':message
, 'success': success})

in index.html
<div id="login-form"></div>

in my JavaScript lib
    function trapForm(target, url, success_callback) {
      $(target + ' form').submit(function(e) {
        getForm(target, url, $(this).serialize(), success_callback);
        e.preventDefault();
      });
    }


    function getForm(target, url, fdata, success_callback) {
      $.ajax({
        url: url,
        data: fdata,
        method: 'post',
        dataType: 'json',
        success: function(data) {
          if (data.message) {
            $.jGrowl(data.message); // I juse jGrown instead of the build 
in .flash
          }
          $(target).html(data.form)
          trapForm(target, url, success_callback);
          if (data.success == true) {
            success_callback(data.user)
          }
        },
        error: function() {
          $.jGrowl('getForm failed');
        }
      });
    }
    /**
     * get the login form via ajax and render it into the page
     */
    function getLoginForm() {
      // load login form
      getForm('#login-form', localconfig.baseurl + '/user/login', undefined, 
function(user) {
        framework.this_user.update(user);// this is where I process the 
returned result
        ... // further front end actions that should be done on login 
follow here
      });
    }

Hope this helps other facing this problem.

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to