Provide openid users with additional information

So, I am writing a python application for the Google appengine on the jinja2 template platform. I got OpenID to work on my site, and it allows the user to log in, and I can display their email address in the upper right corner.

Now I want users to have their own usernames and be able to store some additional data about them, such as site visits and specific posts in areas of the site.

I know that I need to create a table in a database with users and everything, except that this is the right way to do this using OpenID. I want to automatically create a user when someone logs in for the first time, but I'm not quite sure how to do this efficiently. I think I can manage it by redirecting them to a page after logging in, which checks their federated_identity in the database, and if it exists, it already redirects them to the home page, but if it does not create a new user.

Is there a more efficient way so that I don't query the database every time someone logs in or is this a pretty decent way to do this?

+5
source share
1 answer

, -, . , , - (, federated_id), username, name ..

OpenID , federated_id , user_db, user_db.

, user_db , users.get_current_user(), .

, get_user(), user_db, in None . , user_db (name, username, email, admin, federated_id):

from google.appengine.api import users
from google.appengine.ext import ndb
import webapp2

class User(ndb.Model):
  name = ndb.StringProperty(required=True)
  username = ndb.StringProperty(required=True)
  email = ndb.StringProperty()
  federated_id = ndb.StringProperty()
  admin = ndb.BooleanProperty()

class BaseHandler(webapp2.RequestHandler):
  def get_user_db(self):
    federated_user = users.get_current_user()
    if not federated_user:
      return None
    user_db_qry = User.query(User.federated_id == federated_user.user_id())
    user_db_list = user_db_qry.fetch(1)
    #If that returns non empty list, then return the first element which is the user_db
    if user_db_list:
      return user_db_list[0]

    #Otherwise create a new user_db entity, with default values and return
    user_db = User(
        name=federated_user.nickname().title(),
        username=federated_user.nickname(),
        email=federated_user.email(),
        federated_id=federated_user.user_id(),
        admin=users.is_current_user_admin(),
      )
    user_db.put()
    return user_db

class MainHandler(BaseHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/html'
    if self.get_user_db():
      self.response.out.write('Hello, %s!</br>' % self.get_user_db().name)
      self.response.out.write('<a href="%s">Logout</a>' % (users.create_logout_url(self.request.uri)))
    else:
      self.response.out.write('Hello, stranger!<br>')
      self.response.out.write('<a href="%s">Login</a>' % (users.create_login_url(self.request.uri)))

app = webapp2.WSGIApplication([('/', MainHandler)],
                              debug=True)

, , , .

+6

All Articles