From 3f39e3d1307bc7984ce490c49ca9e53eaaaacfbb Mon Sep 17 00:00:00 2001 From: Basit Date: Wed, 8 Jun 2022 20:25:15 +0100 Subject: [PATCH 1/5] Changed 'givenName' and 'familyName' to 'given_name' and 'family_name' respectively in profile_data in auth/apis.py/GoogleLoginApi because Google returns the user_data in the latter format --- server/auth/apis.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/auth/apis.py b/server/auth/apis.py index fe5fdde..3d02008 100644 --- a/server/auth/apis.py +++ b/server/auth/apis.py @@ -59,8 +59,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. From 5607bc6f8adbe9f60b16755aa702bb65c35ff4fc Mon Sep 17 00:00:00 2001 From: Basit Date: Thu, 9 Jun 2022 00:42:34 +0100 Subject: [PATCH 2/5] added app_name variable in api/urls.py to prevent namespae error --- server/api/urls.py | 1 + 1 file changed, 1 insertion(+) 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'))), From b70385e5d79a29c3acd7506a88c6a68cdd0f04e8 Mon Sep 17 00:00:00 2001 From: Basit Date: Thu, 9 Jun 2022 00:50:07 +0100 Subject: [PATCH 3/5] Added warnings on redirect_uri mismatch errors as comments in Login/index.js and auth/apis.py --- client/src/pages/Login/index.js | 4 ++++ server/auth/apis.py | 6 ++++++ 2 files changed, 10 insertions(+) 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/auth/apis.py b/server/auth/apis.py index 3d02008..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) From 23538127a36cae4d7a769c40f0a0989ab7852f11 Mon Sep 17 00:00:00 2001 From: Basit Date: Thu, 9 Jun 2022 01:16:21 +0100 Subject: [PATCH 4/5] Added username to REQUIRED_FILELDS in User model to make it possible to create superuser for testing purposes, not doing this raises TypeError of a missing positional argument 'username' in UserManager.createsuperuser() --- server/users/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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' From f841b5d20064ab2fe315e601455e803a2c35eb3c Mon Sep 17 00:00:00 2001 From: Basit Date: Fri, 10 Jun 2022 00:13:54 +0100 Subject: [PATCH 5/5] Added fourth issued_at as fourth argument in jwt_response_payload_handler in users/selectors.py to fix TypeError due to too many arguments coming into the function --- server/users/selectors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 }