Facebook Auth with AngularJS and Django REST Framework

前端 未结 3 758
别那么骄傲
别那么骄傲 2021-01-30 07:25

I am developing a SPA application with AngularJS which uses Django backend for the server. The way that I communicate with the server from the SPA is with django-rest-framework.

3条回答
  •  感情败类
    2021-01-30 08:05

    I'm using tools just like you, but I provide my login/register/.... with django-allauth package, and then use django-rest-auth for API handling.

    You just need follow the installation instruction, then use them for your rest APIs.

    Adding allauth and rest-auth to your INSTALLED_APPS:

    INSTALLED_APPS = (
        ...,
        'rest_framework',
        'rest_framework.authtoken',
        'rest_auth'
        ...,
        'allauth',
        'allauth.account',
        'rest_auth.registration',
        ...,
        'allauth.socialaccount',
        'allauth.socialaccount.providers.facebook',
    )
    

    Then add your custom urls:

    urlpatterns = patterns('',
        ...,
        (r'^auth/', include('rest_auth.urls')),
        (r'^auth/registration/', include('rest_auth.registration.urls'))
    )
    

    Finally, add this line:

    TEMPLATE_CONTEXT_PROCESSORS = (
        ...,
        'allauth.account.context_processors.account',
        'allauth.socialaccount.context_processors.socialaccount',
        ...
    )
    

    These two packages works like a charm, and you don't need to have concern about any type of login.registration, because allauth package handles both django model login and oAuth login.

    I hope it helps

提交回复
热议问题