diff --git a/client/src/pages/Login/index.js b/client/src/pages/Login/index.js index 8cf82c7..2180207 100644 --- a/client/src/pages/Login/index.js +++ b/client/src/pages/Login/index.js @@ -58,6 +58,10 @@ const Login = () => { const openGoogleLoginPage = useCallback(() => { const googleAuthUrl = 'https://accounts.google.com/o/oauth2/v2/auth'; + + // Note that the trailing slash in the redirect uri below should be removed + // if the authorized redirect uri in the Google console doesn't contain a trailing slash + // otherwise redirection to the uri will fail with a 400 redirect_uri mismatch error const redirectUri = 'api/v1/auth/login/google/'; const scope = [ diff --git a/server/api/urls.py b/server/api/urls.py index ecc4108..f45e5dc 100644 --- a/server/api/urls.py +++ b/server/api/urls.py @@ -1,5 +1,6 @@ from django.urls import path, include +app_name = 'api' v1_patterns = [ path('auth/', include(('auth.urls', 'auth'))), diff --git a/server/auth/apis.py b/server/auth/apis.py index fe5fdde..beb2aa8 100644 --- a/server/auth/apis.py +++ b/server/auth/apis.py @@ -51,6 +51,12 @@ def get(self, request, *args, **kwargs): domain = settings.BASE_BACKEND_URL api_uri = reverse('api:v1:auth:login-with-google') + + # Note that this redirect_uri has a trailing slash coming from api_uri above, + # if your authorized redirect uri in google console does not have a trailing slash, + # you can remove the one in api_uri above by replacing api_uri definition above with: + # api_uri = reverse('api:v1:auth:login-with-google')[:-1], + # otherwise you'll get a 400 redirect_uri mismatch error while trying to get the access token redirect_uri = f'{domain}{api_uri}' access_token = google_get_access_token(code=code, redirect_uri=redirect_uri) @@ -59,8 +65,8 @@ def get(self, request, *args, **kwargs): profile_data = { 'email': user_data['email'], - 'first_name': user_data.get('givenName', ''), - 'last_name': user_data.get('familyName', ''), + 'first_name': user_data.get('given_name', ''), + 'last_name': user_data.get('family_name', ''), } # We use get-or-create logic here for the sake of the example. diff --git a/server/users/models.py b/server/users/models.py index 74dff32..2aae1d5 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -10,7 +10,7 @@ class User(AbstractUser): secret_key = models.CharField(max_length=255, default=get_random_secret_key) USERNAME_FIELD = 'email' - REQUIRED_FIELDS = [] + REQUIRED_FIELDS = ['username'] class Meta: swappable = 'AUTH_USER_MODEL' diff --git a/server/users/selectors.py b/server/users/selectors.py index d5e064a..9e4d30d 100644 --- a/server/users/selectors.py +++ b/server/users/selectors.py @@ -9,8 +9,9 @@ def user_get_me(*, user: User): } -def jwt_response_payload_handler(token, user=None, request=None): +def jwt_response_payload_handler(token, user=None, request=None, issued_at=None): return { 'token': token, 'me': user_get_me(user=user), + 'issued_at': issued_at }