Login

Registering Facebook user by username before loggin in

Author:
zjor
Posted:
June 12, 2013
Language:
Python
Version:
1.5
Score:
0 (after 0 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 1 month, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 month, 4 weeks ago
  3. Serializer factory with Django Rest Framework by julio 8 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 2 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 1 week ago

Comments

Please login first before commenting.