简体   繁体   中英

How to show user a form to set username when using django with django-social-auth

I've been using django-social-auth (https://github.com/omab/django-social-auth), with some success - logging in from Twitter and Facebook have been set up without difficulty. However, when I log in from some OpenID providers, I am faced with an empty variable for the username, and the social-auth app allows this (despite me having set SOCIAL_AUTH_DEFAULT_USERNAME).

Whilst if SOCIAL_AUTH_DEFAULT_USERNAME worked properly that might be an interim solution, ideally I'd rather that it was either set automatically from the openID provider. Basically, I'm left with two possible solutions:

1) Make a custom method to extract some of the extra data sent from the openID provider to set the username from that. 2) Force the user to set a username when they first login.

Whilst (2) is less elegant, it ensures that a username has been inserted each time, and also obviates the need to have some postpocessing of the automatic information which may not be in a suitable format for the username.

My question amounts to, how can I go about implementing either of the above, Complete answers are not necessary, but pointers to a method would be much appreciated!

The alternative is to play with django-socialregistration and to see whether that makes life easier!

J

1 is probably the way to go. You can override the get_user_details method of the Backend class for the social providers you're having trouble with and pull the username from there.

It'd look something like this, for example for Facebook:

from social_auth.backends.facebook import FacebookBackend
class CustomFacebookBackend(FacebookBackend):
    name = 'facebook'

    def get_user_details(self, response):
        """Return user details from Facebook account"""
        return {'username': response['name'],
                'email': response.get('email', ''),
                'first_name': response.get('first_name', ''),
                'last_name': response.get('last_name', '')}

The "response" variable is the deserialized data returned from the request to the OAuth provider for the user details, so you can pull whatever you need from that, or inject your own values here. social-auth will take whatever you stick in "username" and use that as the username for the new account. If you want more control, you can try overriding the backend's "username" method as well.

When overriding the backend, don't forget to add it to AUTHENTICATION_BACKENDS in settings.py. Also, I'm not sure how this works exactly, but I think you need to add something like this to the file with your custom backend in order to get social-auth to link your backend in properly:

BACKENDS = {
    'facebook': CustomFacebookAuth,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM