- Install package:
pip install django-rest-auth- Add
rest_authapp to INSTALLED_APPS in your django settings.py:
INSTALLED_APPS = (
...,
'rest_framework',
'rest_framework.authtoken',
...,
'rest_auth'
)Note
This project depends on django-rest-framework library, so install it if you haven't done yet. Make sure also you have installed rest_framework and rest_framework.authtoken apps
- Add rest_auth urls:
urlpatterns = [
...,
url(r'^rest-auth/', include('rest_auth.urls'))
]- Migrate your database
python manage.py migrateYou're good to go now!
- If you want to enable standard registration process you will need to install
django-allauthby usingpip install django-rest-auth[with_social]. - Add
django.contrib.sites,allauth,allauth.accountandrest_auth.registrationapps to INSTALLED_APPS in your django settings.py: - Add
SITE_ID = 1to your django settings.py
INSTALLED_APPS = (
...,
'django.contrib.sites',
'allauth',
'allauth.account',
'rest_auth.registration',
)
SITE_ID = 1- Add rest_auth.registration urls:
urlpatterns = [
...,
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
]Using django-allauth, django-rest-auth provides helpful class for creating social media authentication view.
Note
Points 1 and 2 are related to django-allauth configuration, so if you have already configured social authentication, then please go to step 3. See django-allauth documentation for more details.
- Add
allauth.socialaccountandallauth.socialaccount.providers.facebookorallauth.socialaccount.providers.twitterapps to INSTALLED_APPS in your django settings.py:
INSTALLED_APPS = (
...,
'rest_framework',
'rest_framework.authtoken',
'rest_auth'
...,
'django.contrib.sites',
'allauth',
'allauth.account',
'rest_auth.registration',
...,
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.twitter',
)- Add Social Application in django admin panel
- Create new view as a subclass of
rest_auth.registration.views.SocialLoginViewwithFacebookOAuth2Adapteradapter as an attribute:
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class FacebookLogin(SocialLoginView):
adapter_class = FacebookOAuth2Adapter- Create url for FacebookLogin view:
urlpatterns += [
...,
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login')
]If you are using Twitter for your social authentication, it is a bit different since Twitter uses OAuth 1.0.
- Create new view as a subclass of
rest_auth.registration.views.SocialLoginViewwithTwitterOAuthAdapteradapter andTwitterLoginSerializeras an attribute:
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
from rest_auth.registration.views import SocialLoginView
from rest_auth.social_serializers import TwitterLoginSerializer
class TwitterLogin(SocialLoginView):
serializer_class = TwitterLoginSerializer
adapter_class = TwitterOAuthAdapter- Create url for TwitterLogin view:
urlpatterns += [
...,
url(r'^rest-auth/twitter/$', TwitterLogin.as_view(), name='twitter_login')
]Note
Starting from v0.21.0, django-allauth has dropped support for context processors. Check out http://django-allauth.readthedocs.org/en/latest/changelog.html#from-0-21-0 for more details.
If you are using GitHub for your social authentication, it uses code and not AccessToken directly.
- Create new view as a subclass of
rest_auth.views.SocialLoginViewwithGitHubOAuth2Adapteradapter, anOAuth2Clientand a callback_url as attributes:
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from rest_auth.registration.views import SocialLoginView
class GithubLogin(SocialLoginView):
adapter_class = GitHubOAuth2Adapter
callback_url = CALLBACK_URL_YOU_SET_ON_GITHUB
client_class = OAuth2Client- Create url for GitHubLogin view:
urlpatterns += [
...,
url(r'^rest-auth/github/$', GitHubLogin.as_view(), name='github_login')
]If you want to allow connecting existing accounts in addition to login, you can use connect views:
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from rest_auth.registration.views import SocialConnectView
from rest_auth.social_serializers import TwitterConnectSerializer
class FacebookConnect(SocialConnectView):
adapter_class = FacebookOAuth2Adapter
class TwitterConnect(SocialConnectView):
serializer_class = TwitterConnectSerializer
adapter_class = TwitterOAuthAdapter
class GithubConnect(SocialConnectView):
adapter_class = GitHubOAuth2Adapter
callback_url = CALLBACK_URL_YOU_SET_ON_GITHUB
client_class = OAuth2ClientIn urls.py:
urlpatterns += [
...,
url(r'^rest-auth/facebook/connect/$', FacebookConnect.as_view(), name='fb_connect')
url(r'^rest-auth/twitter/connect/$', TwitterConnect.as_view(), name='twitter_connect')
url(r'^rest-auth/github/connect/$', GithubConnect.as_view(), name='github_connect')
]You can also use the following views to check all social accounts attached to the current authenticated user and disconnect selected social accounts:
from rest_auth.registration.views import (
SocialAccountListView, SocialAccountDisconnectView
)
urlpatterns += [
...,
url(
r'^socialaccounts/$',
SocialAccountListView.as_view(),
name='social_account_list'
),
url(
r'^socialaccounts/(?P<pk>\d+)/disconnect/$',
SocialAccountDisconnectView.as_view(),
name='social_account_disconnect'
)
]By default django-rest-auth uses Django's Token-based authentication. If you want to use JWT authentication, follow these steps:
- Install djangorestframework-jwt
djangorestframework-jwtis currently the only supported JWT library.
- The
JWT_PAYLOAD_HANDLERandJWT_ENCODE_HANDLERsettings are imported from thedjango-rest-framework-jwtsettings object. - Refer to the library's documentation for information on using different encoders.
- The
- Add the following configuration value to your settings file to enable JWT authentication.
REST_USE_JWT = True