问题
I am trying to learn how to integrate python social auth into a django project.
It seems to work, because I can log into my test application, but it seams that the user is never stored to the db and I can't understand why, since I don't get any error.
Here's my conf
INSTALLED_APPS = (
...
'social.apps.django_app.default',
'messaging'
)
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.email.EmailAuth',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_FACEBOOK_KEY='XX'
SOCIAL_AUTH_FACEBOOK_SECRET='YYY'
FACEBOOK_EXTENDED_PERMISSIONS = ['email']
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/home/'
LOGOUT_REDIRECT_URL = '/'
URL_PATH = ''
SOCIAL_AUTH_STRATEGY = 'social.strategies.django_strategy.DjangoStrategy'
SOCIAL_AUTH_STORAGE = 'social.apps.django_app.default.models.DjangoStorage'
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
)
My idea is just to use django default model for the user, so I haven't defined any extension for it.
Do you have any idea why users are not created at login?
UPDATE
I noticed something that looks weird, at least to me. The first admin user which is created when doing syncdb has the same first and last name of the facebook account I am using to test the application. I thought that it was not going to be used and that another user had to be created, but while checking the database i saw that auth_user.last_login was updated accordingly to the login performed with facebook.
So I have these two questions:
- is it correct that python social auth checks if a user with the same first and last name is already present in auth_user then it uses that particular user instead of creating another one?
- how can I avoid the situation where user Foo Bar on facebook and user Foo Bar on Twitter are not the same person?
回答1:
Quick answers to your questions:
- No
- Nothing is needed to accomplish that.
To me it looks like you were logged in with that admin user before clicking your Login with Facebook link. python-social-auth will take the current logged in user and associate the social account to it (if there's any user logged in), otherwise it will create a new account. But, if the social account was already used in your site, it will login the related user, or drop an exception indicating that the social account is already in use.
You can check the related user to a social account with this:
from social.apps.django_app.default.models import UserSocialAuth
for social in UserSocialAuth.objects.all():
print social.provider, social.user.id, social.user.username
This is what I would do in your case:
- Trash the database, or delete the content in the table
social_auth_usersocialauth - Go to
/adminand ensure that you are logged out - Try to login with Facebook again
This time a new user account should be created.
来源:https://stackoverflow.com/questions/23442307/users-are-not-created-after-login-with-facebook-probably-an-homonymy-case