6

I have started using https://github.com/omab/django-social-auth and been successfully able to login via twitter, google and facebook.

Needed

I need to query about the logged in user in order to do more things, which Model I shall be using for that?

I don't see any examples for that

Thank you

Update

@Omab, I did not understand how this would work, can you please help. When I login with twitter, the callback goes to following code

@login_required
def done(request):
    """Login complete view, displays user data"""
    ctx = {
        'version': version,
        'last_login': request.session.get('social_auth_last_login_backend')
    }
    logging.warn('context - ' + str(ctx))
    logging.warn('request - ' + str(request))
    return render_to_response('home.html', ctx, RequestContext(request))

Can you tell me how can I access to user instance here?

Thank you

daydreamer
  • 87,243
  • 191
  • 450
  • 722

2 Answers2

8

The app stores the social account details using the UserSocialAuth model, to retrieve any instance just do:

user.social_auth.filter(provider="...")

Where:

  • user is a User instance (request.user for current logged in user)
  • provider is a string with the provider name (facebook, twitter, etc)

The UserSocialAuth instance stores the needed tokens to call the needed API:

print user_social_auth.tokens
{...}
omab
  • 3,721
  • 19
  • 23
  • I updated the code, still not getting it clearly, can you please help? – daydreamer Jun 29 '12 at 00:00
  • Hi @omab, can you please help with this. I still don't get anything, I have updated the code as well – daydreamer Jun 29 '12 at 20:51
  • 1
    @daydreamer, in a view you can access the user instance doing `request.user`. If it's logged in, then the instance will respond with `True` when calling `request.user.is_authenticated()`. – omab Jun 30 '12 at 05:40
  • @omab in my case this method does not return the same instance as directly importing UserSocialAuth from social_auth.models. With this method I miss some fields like extra_data, tokens... What am I getting wrong? – Bastian Jul 02 '13 at 15:49
  • @Bastian, not sure, depends of the instance type you get, maybe there's another app clashing? – omab Feb 21 '14 at 14:38
1

As suggested by K-man, you can try this (Identifying the backend provider of a logged in user):

    request.user.social_auth.values_list('provider')

Other values that you can find in the values_list include: id, uid (for ex. facebook user id), user, extra_data (which contains the access_token)

Community
  • 1
  • 1