Skip to content

Commit

Permalink
fix: incorrect assignment of displayName for Apple OAuth (#425)
Browse files Browse the repository at this point in the history
* fix: incorrect assignment of `displayName`

* fix: incorrect param being used to get user info

* fix: profile no longer becomes stringified if user is already existing

* refactor(oauth): parse the user displayName properly

* chore: refactor handler for apple oauth

* chore: add changeset

* chore: add error message when parsing user data from Apple's response

* chore: adjust warning message

---------

Co-authored-by: Tyrone <tyronechris67@gmail.com>
Co-authored-by: Hassan Ben Jobrane <hsanbenjobrane@gmail.com>
  • Loading branch information
3 people authored Nov 6, 2023
1 parent 59e588a commit 2aa0d6d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-mails-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'hasura-auth': patch
---

fix(oauth): correctly parse user profile during Sign-In with Apple
27 changes: 22 additions & 5 deletions src/routes/oauth/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GrantProvider, GrantResponse } from 'grant';
import jwt from 'jsonwebtoken';
import { NormalisedProfile } from './utils';
export const OAUTH_ROUTE = '/signin/provider';
import { logger } from '@/logger';

const azureBaseUrl = 'https://login.microsoftonline.com';
const workosBaseUrl = 'https://api.workos.com/sso';
Expand Down Expand Up @@ -51,12 +52,28 @@ export const PROVIDERS_CONFIG: Record<
response_mode: 'form_post',
},
},
profile: ({ jwt }) => {
profile: ({ jwt, profile }) => {
const payload = jwt?.id_token?.payload;
// * See https://developer.apple.com/forums/thread/118209
const displayName = payload?.name
? `${payload.name.firstName} ${payload.name.lastName}`
: payload.email;

let displayName;

if (profile) {
try {
const userProfile = JSON.parse(profile);

displayName = userProfile.name
? `${userProfile.name.firstName} ${userProfile.name.lastName}`
: displayName;
} catch (error) {
logger.warn(
`Problem trying to parse user data from Apple's response: ${error}. Using the user's email as a fallback.`
);

// use the user's email as fallback
displayName = payload.email;
}
}

return {
id: payload.sub,
displayName,
Expand Down

0 comments on commit 2aa0d6d

Please sign in to comment.