So I am writing an app in python for google appengine on the jinja2 templating platform. I have gotten OpenID to work just fine on my site and it allows the user to login and I can display their email/ID up in the top 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 certain posts to areas of the site.
I know I need to create a table in the database with users and everything but what is the correct way to do this with OpenID. I want to automatically create the user when someone logs in for the first time but I am not quite sure how to do this efficiently. I think I can manage it by redirecting them to a page after login that checks their federated_identity in a database and if it exists there already it just redirects them on to the home page but if not it creates a new user.
Is there a more efficient way so that I am not querying the database everytime someone logs in or is that a pretty decent way to do it?
You are right, you have to create your own User model and store the extra information and whatever business logic you have in your application. All you have to do is to store something unique (
federated_id
for example) along with the extrausername
,name
, etc.After logging in with the OpenID, you will check if that particular
federated_id
is in your datastore and you will return that particularuser_db
, or create a new one with default values and return the newly createduser_db
.The important thing is that in your requests you will always deal with your own
user_db
entity and not theusers.get_current_user()
because this is basically read only and limited.Here is a full example, that also demonstrates a base handler class with the
get_user()
function that will return your ownuser_db
entity if the user is logged in andNone
otherwise. If the user is going to login for the first time, a new user_db entity is created with some default values (name
,username
,email
,admin
,federated_id
):There are many different approaches to this problem, but I think this is a good start to get the idea.