django-rest-auth/docs/installation.rst

209 lines
6.4 KiB
ReStructuredText
Raw Normal View History

2014-10-09 15:01:47 +04:00
Installation
============
2014-10-10 18:39:46 +04:00
1. Install package:
.. code-block:: python
pip install django-rest-auth
2. Add ``rest_auth`` app to INSTALLED_APPS in your django settings.py:
2014-10-09 15:01:47 +04:00
.. code-block:: python
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
2014-10-10 18:39:46 +04:00
3. Add rest_auth urls:
2014-10-09 15:01:47 +04:00
.. code-block:: python
2017-10-03 17:17:48 +03:00
urlpatterns = [
2014-10-09 15:01:47 +04:00
...,
url(r'^rest-auth/', include('rest_auth.urls'))
2017-10-03 17:17:48 +03:00
]
2014-10-09 15:01:47 +04:00
4. Migrate your database
2014-10-09 15:01:47 +04:00
.. code-block:: python
python manage.py migrate
2014-10-09 15:01:47 +04:00
You're good to go now!
Registration (optional)
-----------------------
1. If you want to enable standard registration process you will need to install ``django-allauth`` by using ``pip install django-rest-auth[with_social]``.
2014-10-09 15:01:47 +04:00
2. Add ``django.contrib.sites``, ``allauth``, ``allauth.account`` and ``rest_auth.registration`` apps to INSTALLED_APPS in your django settings.py:
3. Add ``SITE_ID = 1`` to your django settings.py
2014-10-09 15:01:47 +04:00
.. code-block:: python
INSTALLED_APPS = (
...,
'django.contrib.sites',
2014-10-09 15:01:47 +04:00
'allauth',
'allauth.account',
'rest_auth.registration',
)
SITE_ID = 1
2014-10-09 15:01:47 +04:00
3. Add rest_auth.registration urls:
.. code-block:: python
2017-10-03 17:17:48 +03:00
urlpatterns = [
2014-10-09 15:01:47 +04:00
...,
2015-12-11 14:08:39 +03:00
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
2017-10-03 17:17:48 +03:00
]
2014-10-09 15:01:47 +04:00
Social Authentication (optional)
2014-10-09 15:01:47 +04:00
--------------------------------
Using ``django-allauth``, ``django-rest-auth`` provides helpful class for creating social media authentication view.
2014-10-09 15:01:47 +04:00
.. 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.
2014-10-09 17:27:24 +04:00
1. Add ``allauth.socialaccount`` and ``allauth.socialaccount.providers.facebook`` or ``allauth.socialaccount.providers.twitter`` apps to INSTALLED_APPS in your django settings.py:
2014-10-09 15:01:47 +04:00
.. code-block:: python
INSTALLED_APPS = (
...,
'rest_framework',
'rest_framework.authtoken',
'rest_auth'
...,
'django.contrib.sites',
2014-10-09 15:01:47 +04:00
'allauth',
'allauth.account',
'rest_auth.registration',
...,
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
2016-02-23 14:45:57 +03:00
'allauth.socialaccount.providers.twitter',
2014-10-09 15:01:47 +04:00
)
2015-11-19 12:36:55 +03:00
2. Add Social Application in django admin panel
2014-10-09 17:27:24 +04:00
Facebook
########
2015-11-19 12:36:55 +03:00
3. Create new view as a subclass of ``rest_auth.registration.views.SocialLoginView`` with ``FacebookOAuth2Adapter`` adapter as an attribute:
2014-10-09 15:01:47 +04:00
.. code-block:: python
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from rest_auth.registration.views import SocialLoginView, SocialConnectView
2014-10-09 15:01:47 +04:00
2015-08-07 13:54:45 +03:00
class FacebookLogin(SocialLoginView):
2014-10-09 15:01:47 +04:00
adapter_class = FacebookOAuth2Adapter
# Add a connect view if you want to allow connecting existing accounts
class FacebookConnect(SocialConnectView):
adapter_class = FacebookOAuth2Adapter
2015-11-19 12:36:55 +03:00
4. Create url for FacebookLogin view:
2014-10-09 15:01:47 +04:00
.. code-block:: python
2017-10-03 17:17:48 +03:00
urlpatterns += [
2014-10-09 15:01:47 +04:00
...,
2015-04-27 12:01:19 +03:00
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login')
url(r'^rest-auth/facebook/connect/$', FacebookConnect.as_view(), name='fb_connect')
2017-10-03 17:17:48 +03:00
]
2015-11-19 12:36:55 +03:00
2016-02-23 14:45:57 +03:00
Twitter
#######
If you are using Twitter for your social authentication, it is a bit different since Twitter uses OAuth 1.0.
2016-02-23 14:45:57 +03:00
3. Create new view as a subclass of ``rest_auth.views.LoginView`` with ``TwitterOAuthAdapter`` adapter and ``TwitterLoginSerializer`` as an attribute:
2016-02-23 14:45:57 +03:00
.. code-block:: python
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
from rest_auth.registration.views import SocialLoginView
from rest_auth.social_serializers import TwitterLoginSerializer, TwitterConnectSerializer
2016-02-23 14:45:57 +03:00
class TwitterLogin(SocialLoginView):
2016-02-23 14:45:57 +03:00
serializer_class = TwitterLoginSerializer
adapter_class = TwitterOAuthAdapter
# Add a connect view if you want to allow connecting existing accounts
class TwitterConnect(SocialConnectView):
serializer_class = TwitterConnectSerializer
adapter_class = TwitterOAuthAdapter
4. Create url for TwitterLogin view:
2016-02-23 15:01:11 +03:00
.. code-block:: python
2017-10-03 17:17:48 +03:00
urlpatterns += [
2016-02-23 15:01:11 +03:00
...,
url(r'^rest-auth/twitter/$', TwitterLogin.as_view(), name='twitter_login')
url(r'^rest-auth/twitter/connect/$', TwitterConnect.as_view(), name='twitter_connect')
2017-10-03 17:17:48 +03:00
]
2015-11-19 12:36:55 +03:00
.. 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.
Additional Social Connect Views
###############################
If you are using social connect views, you can also use additional views to check all social accounts attached to the current authenticated user and disconnect selected social accounts.
.. code-block:: python
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'
)
]
JWT Support (optional)
----------------------
By default ``django-rest-auth`` uses Django's Token-based authentication. If you want to use JWT authentication, follow these steps:
1. Install `django-rest-framework-jwt <http://getblimp.github.io/django-rest-framework-jwt/>`_
- ``django-rest-framework-jwt`` is currently the only supported JWT library.
2. The ``JWT_PAYLOAD_HANDLER`` and ``JWT_ENCODE_HANDLER`` settings are imported from the ``django-rest-framework-jwt`` settings object.
- Refer to `the library's documentation <http://getblimp.github.io/django-rest-framework-jwt/#additional-settings>`_ for information on using different encoders.
3. Add the following configuration value to your settings file to enable JWT authentication.
.. code-block:: python
REST_USE_JWT = True