Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect assignment of displayName for Apple OAuth #425

Merged
merged 8 commits into from
Nov 6, 2023
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}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking but I'd add , default to email

);

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

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