Creating unique username with SimpleAuth

106 views
Skip to first unread message

Jed Christiansen

unread,
Aug 10, 2013, 8:09:10 PM8/10/13
to gae-sim...@googlegroups.com
Hi, everyone.  I recently posted this question to StackOverflow: http://stackoverflow.com/questions/18167504/adding-a-username-property-to-webapp2-user-model-after-entity-has-been-created

In a nutshell, I'm already using SimpleAuth and want to edit existing Users to add a unique username.  I see there are methods in webapp2's User model that should allow exactly this, but don't know how to configure them.  I'd appreciate any help/pointers.

(Posted to StackOverflow to give karma there; but happy to get feedback on e-mail, too.)  Thanks in advance for any help!

Cheers,
Jed

--
--------------------------------------------
Jed D. Christiansen
+44 (0) 796 358 3663
http://about.me/jedc

alex

unread,
Aug 18, 2013, 10:36:04 AM8/18/13
to Jed Christiansen, gae-sim...@googlegroups.com
Hey Jed, 

sorry for late reply, just got back from a vacation. I tried to answer it on SO but thought I'd copy&paste it here too:

I think you could extend webapp2_extras.appengine.auth.models.User and add username property, e.g.


from webapp2_extras.appengine.auth.models import User as Webapp2User

class User(Webapp2User):
    username = ndb.StringProperty(required=True)

Then, to create a webapp2 app you'd need a config which includes this:


APP_CFG = {
  'webapp2_extras.auth': {
    'user_model': User,  # default is webapp2_extras.appengine.auth.models.User
    'user_attributes': ['username']  # list of User model properties
  }
}

app = webapp2.WSGIApplication(config=APP_CFG)

Havign the above, creating a new user using the following code will ensure username is unique (ensured by Unique model):


auth_id = 'some-auth-id'  # e.g. 'google:123456789', see simpleauth example.
ok, props = User.create_user(auth_id, unique_properties=['username'],
                                      username='some-username',
                                      ...)

Problem is, with this configuration you are bound to set username during the creation time.

If you wanted to make username optional, or let users set/change it later on, you would probably want to change the above code to something like this:


class User(Webapp2User):
    username = ndb.StringProperty(required=True)

# when creating a new user:
auth_id = 'some-auth-id'  # e.g. 'google:123456789', see simpleauth example.
ok, props = User.create_user(auth_id, unique_properties=[], ...)

Basically, unique_properties will be empty list (or you can just skip it).

Then, in a "change/set username" form handler, you could check if a username already exists:


def handle_change_username(self):
  user = ...  # get the user who wants to change their username
  username = self.request.get('username')
  uniq = 'User.username:%s' % username
  ok = User.unique_model.create(uniq)
  if ok:
    user.username = username
    user.put()
  else:
    # notify them that this username
    # is already taken
    ...


Hope this helps!



--
You received this message because you are subscribed to the Google Groups "SimpleAuth for GAE" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gae-simpleaut...@googlegroups.com.
Visit this group at http://groups.google.com/group/gae-simpleauth.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

alex

unread,
Aug 20, 2013, 6:44:21 AM8/20/13
to Jed Christiansen, gae-sim...@googlegroups.com
Sorry, there was a little mistake. Instead of:

If you wanted to make username optional, or let users set/change it later on, you would probably want to change the above code to something like this:


class User(Webapp2User):
    username = ndb.StringProperty(required=True)

It should be:

If you wanted to make username optional, or let users set/change it later on, you would probably want to change the above code to something like this (no required=True):


class User(Webapp2User):
    username = ndb.StringProperty()  # note, there's no required=True

alex

unread,
Aug 20, 2013, 6:56:13 AM8/20/13
to Jed Christiansen, gae-sim...@googlegroups.com
Actually, I've just updated my SO answer with some little more info. It's probably best to look there.
Reply all
Reply to author
Forward
0 new messages