This snippet allows to create user in django auth system without logging in. After this user logs in via Facebook account social user is created and bound to existing user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | my_auth.py:
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from social_auth.exceptions import AuthException
from django.contrib.auth.models import User
def associate_by_username(details, user=None, *args, **kwargs):
"""Return user entry with same email address as one returned on details."""
if user:
return None
username = details.get('username')
if username:
try:
return {'user': User.objects.get(username=username)}
except MultipleObjectsReturned:
raise AuthException(kwargs['backend'], 'Not unique email address.')
except ObjectDoesNotExist:
pass
settings.py:
...
SOCIAL_AUTH_PIPELINE = (
'social_auth.backends.pipeline.social.social_auth_user',
'my_app.my_auth.associate_by_username',
'social_auth.backends.pipeline.user.get_username',
'social_auth.backends.pipeline.user.create_user',
'social_auth.backends.pipeline.social.associate_user',
'social_auth.backends.pipeline.social.load_extra_data',
'social_auth.backends.pipeline.user.update_user_details', )
...
Usage:
1. Lookup UserSocialAuth.objects.filter(uid=target_facebook_id)
2. Registration User(username=target_username)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 1 year ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year ago
- Serializer factory with Django Rest Framework by julio 1 year, 7 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 8 months ago
- Help text hyperlinks by sa2812 1 year, 9 months ago
Comments
Please login first before commenting.