Handling Sessions on Google App Engine with Android/IPhone

前端 未结 6 531
借酒劲吻你
借酒劲吻你 2021-02-04 07:11

I\'m starting to write an app whereby a mobile app (Android/IPhone) will communicate with the GAE backend (Python) through a series of Web API calls using JSON.

I can\'t

6条回答
  •  终归单人心
    2021-02-04 07:44

    I think you should use features webapp2 providing to implement your custom registration.

    from webapp2_extras import auth
    from google.appengine.api import users
    
    class RegisterHandler(webapp2.RequestHandler):
        def post(self):
            email=self.request.POST['email']
            password=self.request.POST['password']
    
            #Let webapp2 handle register and manage session
            user = auth.get_auth().store.user_model.create_user('own:'+str(email), password_raw=password,email=email)
            #user (True, User(key=Key('User', 80001), auth_ids=[u'own:useremail@mail.com'],email='useremail@mail.com',password=u'hashed_password',...))
    
            if not user[0]: #user is a tuple
                self.response.write(user[1]) # Error message
            else:
                #You can extend your User Model e.g UserProfile(User): or have a UserProperty in your profile model as the example.  
                profile=UserProfile(user=users.User(user[1].email)).put()
    
                self.response.write(str(profile.key()))
    class LoginHandler(webapp2.RequestHandler): 
    
        def post(self):
    
            email = self.request.POST.get('email')
            email = 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:
                auth.get_auth().get_user_by_password('own:'+email, password)
                #Return user_session with User id, 
            except InvalidPasswordError, InvalidAuthIdError:
                #Error
    

    You can check user logged in by:

    if auth.get_user_by_session():
        #Logged in      
    else:
        #Not logged in
    

    On your client application(Android, IOS). You only have to store the response cookie and send it for every sub sequence requests.

    Good luck :)

提交回复
热议问题