I've been very confused. I've been working with Google's User object, as can be found here:
After reading a few resources, I've been told that storing the User object itself is not a good idea, as emails can change. What Google recommends, is that you should store the User_Id itself, as it is persistently safe to do so (i.e., it remains static - always).
When I get the current user and try to get the User_Id value using the user_id()
method on the user object, I get a None
value response.
current_user = endpoints.get_current_user()
print current_user.user_id()
>> None
However, if I save the current_user as a UserProperty in NDB and then retrieve the same value, I do get the user_id. For example:
google_user = ndb.UserProperty(required = True)
print google_user.user_id()
>> 12345678901234567890
How are google user objects suppose to be handled? And more importantly, why is the user_id value None before NDB has saved the value? Is this a type of account/email verification?
You can find the advice from Google here:
https://cloud.google.com/appengine/docs/python/users/userobjects
"We strongly recommend that you do not store a UserProperty, because it includes the email address along with the user's unique ID. If a user changes their email address and you compare their old, stored User to the new User value, they won't match. Instead, consider using the User user ID value as the user's stable unique identifier."
Edit: I should mention, I'm testing on localhost
. Is endpoints.get_current_user()
handled differently when the applcation is deployed?
This is the example Google uses:
class ModelWithUser(ndb.Model):
user_id = ndb.StringProperty()
color = ndb.StringProperty()
@classmethod
def get_by_user(cls, user):
return cls.query().filter(cls.user_id == user.user_id()).get()
If I'm querying the ModelWithUser
via the user_id from the currently authenticated user (which I don't have access to), how am I suppose to get the user from ModelWithUser
? Gah.