Login

Registering Facebook user by username before loggin in

Author:
zjor
Posted:
June 12, 2013
Language:
Python
Version:
1.5
Tags:
registration auth django-social-auth
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. Facebook Connect Middleware by bretwalker 6 years, 6 months ago
  2. Facebook Authentication Backend by barnardo 5 years ago
  3. Use django-social-auth & Google Accounts for admin login by pmdarrow 2 years, 6 months ago
  4. anonymous_required decorator by yetty 1 year, 12 months ago
  5. Show logged users - keeping track of users login and logout by albertorcf 2 years, 9 months ago

Comments

Please login first before commenting.