Search code examples
pythonpylonsauthenticationrepoze.who

How do I log a user in using repoze.who?


I have a working repoze.who/what setup (on a Pylons app). I now want to automatically log new users in after signup, without them having to use the login form. I've browsed the repoze.who docs and source code and, maybe I'm just missing it, but I can't find out how to set the logged-in user from code, without a new post request going through the middleware. Is this possible?


Solution

  • I have been with similar issue all the morning and this is what I've found: You must know that right now there's two versions of repoze.who (1 and 2):

    In v 1.x:

    rememberer = request.environ['repoze.who.plugins']['cookie']
    identity = {'repoze.who.userid': user.username}
    response.headerlist = response.headerlist + \
            rememberer.remember(request.environ, identity) 
    

    In v 2.x (it is easier because version 2 provides an api):

    from repoze.who.api import get_api
    
    who_api = get_api(request.environ)
    creds = {}
    creds['login'] = yourusername
    creds['password'] = yourpassword
    authenticated, headers = who_api.login(creds)
    

    Resources: For v1: http://www.deanlee.cn/programming/repoze-who-perform-login-programmatically/ For v2: http://docs.repoze.org/who/2.0/api.html