2012-05-08 7 views
3

自分のプロジェクトのカスタムユーザーアカウントを使用しています。ユーザーモデルとwebapp2の認証を使用しています。すべてが完璧に実行されますが、私は認証が成功しない部分で立ち往生しています。例についてはpython Google App Engine:Webapp2:認証

#imports 
from webapp2_extras.appengine.auth.models import User 

class LoginHandler(SomeBaseRequestHandler): 
    def get(self): 
    '''self code goes in here''' 

    def post(self): 
    auth_id = 'authentication:id' 
    password = 'somepassword' 

    user = User.get_by_auth_password(authid, password) 
    if user: 
     # code to set a session and redirect to homepage 
    else: 
     # append error list and render a template 

私は、ユーザーが間違ったユーザー名またはパスワードを提供する場合、問題が発生するただし、ユーザーがログインできています。ユーザーが間違った資格情報を提供すると、サーバー側のエラーが発生します。

Traceback (most recent call last): 
File "/opt/google_appengine_1.6.4/lib/webapp2/webapp2.py", line 1536, in __call__ 
rv = self.handle_exception(request, response, e) 
File "/opt/google_appengine_1.6.4/lib/webapp2/webapp2.py", line 1530, in __call__ 
rv = self.router.dispatch(request, response) 
File "/opt/google_appengine_1.6.4/lib/webapp2/webapp2.py", line 1278, in default_dispatcher 
return route.handler_adapter(request, response) 
File "/opt/google_appengine_1.6.4/lib/webapp2/webapp2.py", line 1102, in __call__ 
return handler.dispatch() 
File "/home/tigerstyle/orbit/orbit/orbit/handlers.py", line 36, in dispatch 
webapp2.RequestHandler.dispatch(self) 
File "/opt/google_appengine_1.6.4/lib/webapp2/webapp2.py", line 572, in dispatch 
return self.handle_exception(e, self.app.debug) 
File "/opt/google_appengine_1.6.4/lib/webapp2/webapp2.py", line 570, in dispatch 
return method(*args, **kwargs) 
File "/home/tigerstyle/orbit/orbit/orbit/handlers.py", line 239, in post 
user = User.get_by_auth_password(auth_id, password) 
File "/opt/google_appengine_1.6.4/lib/webapp2/webapp2_extras/appengine/auth/models.py", line 301, in get_by_auth_password 
raise auth.InvalidPasswordError() 
InvalidPasswordError 

答えて

4

あなたのログインの流れを制御する以外/試みる使用することができます。

def post(self): 
    """ 
    username: Get the username from POST dict 
    password: Get the password from POST dict 
    """ 
    username = self.request.POST.get('username') 
    password = self.request.POST.get('password') 
    # Try to login user with password 
    # Raises InvalidAuthIdError if user is not found 
    # Raises InvalidPasswordError if provided password doesn't match with specified user 
    try: 
    self.auth.get_user_by_password(username, password) 
    self.redirect('/secure') 
    except (InvalidAuthIdError, InvalidPasswordError), e: 
    # Returns error message to self.response.write in the BaseHandler.dispatcher 
    # Currently no message is attached to the exceptions 
    return e 
+1

は完璧に動作し、感謝を! – Amyth