首页 文章

Django Social Auth - 用户已创建,配置文件已创建,但用户未登录

提问于
浏览
2

因此,当 Headers 出现时,我可以使用Facebook创建新用户并创建新的 Profiles . 但是,我没有登录 . 我总是被重定向到/ login /(最后有一些字符) . 有了这段代码,我还能错过什么?

我的settings.py

SOCIAL_AUTH_DEFAULT_USERNAME = lambda u: slugify(u)
SOCIAL_AUTH_UUID_LENGTH = 10
SOCIAL_AUTH_EXTRA_DATA = False
SOCIAL_AUTH_EXPIRATION = 'expires'
SOCIAL_AUTH_INACTIVE_USER_URL = '/inactive/'
SOCIAL_AUTH_RAISE_EXCEPTIONS = DEBUG
SOCIAL_AUTH_CHANGE_SIGNAL_ONLY = True
SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True
SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'

AUTHENTICATION_BACKENDS = (
    'social_auth.backends.facebook.FacebookBackend',
    'django.contrib.auth.backends.ModelBackend',
)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'social_auth.context_processors.social_auth_by_name_backends',
    'social_auth.context_processors.social_auth_backends',
    'social_auth.context_processors.social_auth_by_type_backends',
    'social_auth.context_processors.social_auth_login_redirect',
)

我的models.py

signals.post_save.connect(create_profile, sender=User)
pre_update.connect(facebook_extra_values, sender=FacebookBackend)

class UserProfile(models.Model):
    user = models.ForeignKey(User)
    photo = ImageField(upload_to="profile_pictures")

    def get_absolute_url(self):
        return ('profiles_profile_detail', (), {'username':self.user.username})

    get_absolute_url = models.permalink(get_absolute_url)

我的receivers.py

def facebook_extra_values(sender, instance, response, details, **kwargs):
    """
    post_save signal from User model; check bzuser.models
    for the connection

    """
    from .models import UserProfile
    UserProfile(user=instance).save()
    return True

我的urls.py

urlpatterns = patterns('',
    url(r'^login/$', custom_login),
    url(r'^logout/$', custom_logout),
    url(r'^social_login/$', SocialLoginView.as_view(), name="social_login"),
    (r'', include('registration.backends.default.urls')),
    url(r'^register$', 'registration.views.register', {
        'form_class': RegistrationFormUniqueEmail,
        'backend': 'registration.backends.default.DefaultBackend'
    }, name='registration_register'),
    (r'^profiles/', include('profiles.urls')),
    url(r'^profile', ProfileView.as_view(), name="profile_private"),
)

和我的views.py

class ProfileView(TemplateView):
    template_name = 'profile.html'

class SocialLoginView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super(SocialLoginView, self).get_context_data(**kwargs)
        context['title'] = _('Log In With Facebook or Twitter')
        return context

    template_name = 'social.html'

def custom_login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('profiles_profile_detail',
            kwargs={"username":request.user.username}))
    else:
        return login(request)

def custom_logout(request):
    return logout_then_login(request)

和我的模板:

{% extends 'base.html' %}

{% block content %}
    <div class="widget">
        <div class="title">
            <img class="titleIcon" src="{{ STATIC_URL }}images/icons/dark/preview.png" />
            <h6>Social Login</h6>
        </div>

        <div class="body textC">
            <a class="wButton bluewB m10"
                href="{% url socialauth_begin 'facebook' %}{% if request.GET.next %}?next={{ request.GET.next }}{% endif %}">
                <span>Log In With Facebook</span>
            </a>
        </div>
    </div>
{% endblock %}

提前致谢!

EDIT :添加了AUTHENTICATION_BACKENDS常量

应用程序中的urls.py

urlpatterns = patterns('',
    # project specific urls
    url(r'', include('base.urls')),
    url(r'', include('bzuser.urls')),

    # 3rd party URLS
    ('^nexus/', include(nexus.site.urls)),
    url(r'', include('social_auth.urls')),
)

EDIT: 添加了社交身份验证包含URLS

1 回答

  • 2

    我遇到了类似的问题并最终解决了 . 您应该使用身份验证pipeline!并确保通过电子邮件关联 . 所以你有类似的东西

    SOCIAL_AUTH_PIPELINE = (
        'social_auth.backends.pipeline.social.social_auth_user',
        'social_auth.backends.pipeline.associate.associate_by_email',
        'social_auth.backends.pipeline.misc.save_status_to_session',
        'people.pipeline.redirect_to_get_phonenumber_form',
        'people.pipeline.phonenumber',
        'people.pipeline.save_new_persons_data',
        'social_auth.backends.pipeline.social.associate_user',
        'social_auth.backends.pipeline.social.load_extra_data',
        'social_auth.backends.pipeline.user.update_user_details',
    )
    

相关问题