简体   繁体   中英

How do i verify user in database in google appengine app ( can anyone recommend the best way to do user authentication for google appengine app)?

I have following code in models.py i can sort database by only key but not by string ?

from google.appengine.ext import ndb

class Roles(ndb.Model):
name = ndb.StringProperty()
owner = ndb.KeyProperty(kind='User')
created = ndb.DateTimeProperty(required=True, auto_now_add = True)
class RESTMeta:
    user_owner_property = 'owner'
    include_output_properties = ['name']

class Users(ndb.Model):
name = ndb.StringProperty()
email = ndb.StringProperty()
password = ndb.StringProperty()
roles = ndb.KeyProperty(kind='Roles')
owner = ndb.KeyProperty(kind='User')
created = ndb.DateTimeProperty(required=True, auto_now_add = True)
class RESTMeta:
    user_owner_property = 'owner'
    include_output_properties = ['name']

And the following in api.py

app = webapp2.WSGIApplication([
RESTHandler(
  '/api/roles', # The base URL for this model's endpoints
  models.Roles, # The model to wrap
  permissions={
    'GET': PERMISSION_ANYONE,
    'POST': PERMISSION_ANYONE,
    'PUT': PERMISSION_OWNER_USER,
    'DELETE': PERMISSION_ADMIN
  },
  # Will be called for every PUT, right before the model is saved (also supports callbacks for GET/POST/DELETE)
  put_callback=lambda model, data: model
),
RESTHandler(
  '/api/users', # The base URL for this model's endpoints
  models.Users, # The model to wrap
  permissions={
    'GET': PERMISSION_ANYONE,
    'POST': PERMISSION_ANYONE,
    'PUT': PERMISSION_OWNER_USER,
    'DELETE': PERMISSION_ADMIN
  },
  # Will be called for every PUT, right before the model is saved (also supports callbacks for GET/POST/DELETE)
  put_callback=lambda model, data: model
)],debug=True, config = config)

I can successfully get by key in api\\users?q=roles=key('key')

How do i get specific user by String api\\users?q=email=String('String')

The Question is how do I do user auth for google appengine app

You seem to be asking so many questions in one.

To get user by email, simply do this:

users = Users.query(Users.email=='query_email').fetch(1)
#note fetch() always returns a list
if users:
    user_exists = True
else:
    user_exists = False

Please note, you may need to update your datastore index to support that query. The easiest way to do it is to first run the code in your local development server, and the index will be automatically updated for you.

To answer your second questions, for user authentication I would recommend Django's in-built User Authentication . Please note that you can always run vanilla django on appengine with a Flexible VM using CloudSQL instead of the Datastore.

Alternatively, you could use the Appengine Users API , though your users would need to have Google Accounts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM