Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Prevent possible runtime errors #7

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/src/pages/#/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/#/google/';

const scope = [
Expand Down
1 change: 1 addition & 0 deletions server/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.urls import path, include

app_name = 'api'

v1_patterns = [
path('auth/', include(('auth.urls', 'auth'))),
Expand Down
10 changes: 8 additions & 2 deletions server/auth/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion server/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion server/users/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}