diff --git a/dj_rest_auth/registration/views.py b/dj_rest_auth/registration/views.py index 10f1f325..8079a111 100644 --- a/dj_rest_auth/registration/views.py +++ b/dj_rest_auth/registration/views.py @@ -53,10 +53,10 @@ def get_response_data(self, user): 'refresh': self.refresh_token, } return api_settings.JWT_SERIALIZER(data, context=self.get_serializer_context()).data - elif api_settings.SESSION_LOGIN: - return None - else: + elif self.token_model: return api_settings.TOKEN_SERIALIZER(user.auth_token, context=self.get_serializer_context()).data + + return None def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) @@ -82,9 +82,7 @@ def perform_create(self, serializer): allauth_account_settings.EmailVerificationMethod.MANDATORY: if api_settings.USE_JWT: self.access_token, self.refresh_token = jwt_encode(user) - elif not api_settings.SESSION_LOGIN: - # Session authentication isn't active either, so this has to be - # token authentication + elif self.token_model: api_settings.TOKEN_CREATOR(self.token_model, user, serializer) complete_signup( diff --git a/dj_rest_auth/tests/test_api.py b/dj_rest_auth/tests/test_api.py index 571669ed..41cc7555 100644 --- a/dj_rest_auth/tests/test_api.py +++ b/dj_rest_auth/tests/test_api.py @@ -530,12 +530,28 @@ def test_registration_with_jwt(self): @override_api_settings(SESSION_LOGIN=True) @override_api_settings(TOKEN_MODEL=None) def test_registration_with_session(self): + import sys + from importlib import reload + from django.contrib.sessions.middleware import SessionMiddleware + from django.contrib.messages.middleware import MessageMiddleware + reload(sys.modules['dj_rest_auth.models']) + reload(sys.modules['dj_rest_auth.registration.views']) + from dj_rest_auth.registration.views import RegisterView + user_count = get_user_model().objects.all().count() self.post(self.register_url, data={}, status_code=400) - result = self.post(self.register_url, data=self.REGISTRATION_DATA, status_code=204) - self.assertEqual(result.data, None) + factory = APIRequestFactory() + request = factory.post(self.register_url, self.REGISTRATION_DATA) + + for middleware_class in (SessionMiddleware, MessageMiddleware): + middleware = middleware_class(lambda request: None) + middleware.process_request(request) + + response = RegisterView.as_view()(request) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(response.data, None) self.assertEqual(get_user_model().objects.all().count(), user_count + 1) self._login(status.HTTP_204_NO_CONTENT) diff --git a/docs/installation.rst b/docs/installation.rst index ebd0f297..c38a3ad2 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -44,7 +44,7 @@ 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 'dj-rest-auth[with_social]'``. +1. If you want to enable standard registration process you will need to install ``django-allauth`` by using ``pip install 'dj-rest-auth[with-social]'``. 2. Add ``django.contrib.sites``, ``allauth``, ``allauth.account``, ``allauth.socialaccount`` and ``dj_rest_auth.registration`` apps to INSTALLED_APPS in your django settings.py: @@ -182,12 +182,12 @@ If you are using GitHub for your social authentication, it uses code and not Acc ..., path('dj-rest-auth/github/', GitHubLogin.as_view(), name='github_login') ] - - + + Google ###### If you are using Google for your social authentication, you can choose ``Authorization Code Grant`` or ``Implicit Grant`` (deprecated). -Serializer of dj-rest-auth accepts both ``code`` and ``token`` +Serializer of dj-rest-auth accepts both ``code`` and ``token`` 1. Add ``allauth.socialaccount`` and ``allauth.socialaccount.providers.google`` apps to INSTALLED_APPS in your django settings.py: @@ -208,7 +208,7 @@ Serializer of dj-rest-auth accepts both ``code`` and ``token`` 'allauth.socialaccount.providers.google', ) - + 3. Create new view as a subclass of ``dj_rest_auth.views.SocialLoginView`` with ``GoogleOAuth2Adapter`` adapter, an ``OAuth2Client`` and a callback_url as attributes: .. code-block:: python @@ -216,12 +216,12 @@ Serializer of dj-rest-auth accepts both ``code`` and ``token`` from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter from allauth.socialaccount.providers.oauth2.client import OAuth2Client from dj_rest_auth.registration.views import SocialLoginView - + class GoogleLogin(SocialLoginView): # if you want to use Authorization Code Grant, use this adapter_class = GoogleOAuth2Adapter callback_url = CALLBACK_URL_YOU_SET_ON_GOOGLE client_class = OAuth2Client - + class GoogleLogin(SocialLoginView): # if you want to use Implicit Grant, use this adapter_class = GoogleOAuth2Adapter @@ -234,7 +234,7 @@ Serializer of dj-rest-auth accepts both ``code`` and ``token`` path('dj-rest-auth/google/', GoogleLogin.as_view(), name='google_login') ] -5. Retrive code (or token) +5. Retrieve code (or token) By accessing Google's endpoint, you can get ``code`` or ``token`` If you're using Authorization Code Grant, you can get code from following URL diff --git a/setup.py b/setup.py index 91f50c4b..63353b8c 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ 'djangorestframework>=3.13.0', ], extras_require={ - 'with_social': ['django-allauth>=0.56.0,<0.62.0'], + 'with-social': ['django-allauth>=0.56.0,<0.62.0'], }, tests_require=[ 'coveralls>=1.11.1',