Facebook login | Uppercase username conflicts with existing lowercase usernames

68 views
Skip to first unread message

Jack Zhang

unread,
Mar 1, 2018, 12:07:01 AM3/1/18
to Django Social Auth

In my application, during registration, any uppercase letter in the username is converted to lower case. E.g. If I sign up as JohnSmith, the app will create the username johnsmith. This is done so that there is no confusion when accessing a user profile on the website - mysite.com/user/johnsmith and mysite.com/user/JohnSmith will go to the same user's profile.


I have Facebook's social login set up, and Facebook automatically generates a username that capitalizes the first and last name. E.g. if my Facebook name is John Smith, Facebook will generate the username JohnSmith in the application.

This creates a problem. If there is already a username in the database named 'johnsmith', Facebook goes ahead and generates 'JohnSmith' anyways, creating a conflict.


Is that a way I can modify Facebook's registration so that the username is always converted to lowercase?

Matías Aguirre

unread,
Mar 1, 2018, 5:43:53 AM3/1/18
to DSA Group
There are two ways to accomplish this:

* Add a "cleanup_username" pipeline function after "get_username" one,
on this function you would take the generated username and return a
cleaned up version of it following your desired rules, the return must
be in the form: {'username': new_username}. More docs about the
pipeline is available here:
https://python-social-auth.readthedocs.io/en/latest/pipeline.html

* Define "SOCIAL_AUTH_CLEAN_USERNAME_FUNCTION", this setting is an
import-path pointing to a function that will be called to run extra
cleanup rules on the username, it takes the username as input and the
output is used as the new username, it can be called multiple times if
a collision is detected. Docs about it here:
https://python-social-auth.readthedocs.io/en/latest/configuration/settings.html#username-generation
> --
> You received this message because you are subscribed to the Google Groups
> "Django Social Auth" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-social-a...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Matías Aguirre
http://matiasaguirre.net

Jack Zhang

unread,
Mar 1, 2018, 12:39:29 PM3/1/18
to Django Social Auth
Thanks for that.  I played around with the first method, but the second method is much simpler.  For anyone that comes across this thread, here is what I did:

Added the following line to settings.py - `SOCIAL_AUTH_CLEAN_USERNAME_FUNCTION = 'myproject.myapp.views.clean_social_username'`

Then in views.py for myapp, here is my function:

# Remove all spaces in name, then make everything lowercase:
def clean_social_username(username):
    username = username.replace(' ', '').lower()
    return username

That was it!  
Reply all
Reply to author
Forward
0 new messages