Skip to content

Commit

Permalink
Merge branch 'iMerica:master' into dontic/fix-registration-email-vali…
Browse files Browse the repository at this point in the history
…dation-on-unverified-accounts
  • Loading branch information
dontic authored Aug 19, 2024
2 parents 86a2dc9 + 5498963 commit 774dc4c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
10 changes: 4 additions & 6 deletions dj_rest_auth/registration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
20 changes: 18 additions & 2 deletions dj_rest_auth/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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:

Expand All @@ -208,20 +208,20 @@ 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
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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 774dc4c

Please sign in to comment.