From 2713c8b46d5a174995c0ed369933b0db3aaa7013 Mon Sep 17 00:00:00 2001 From: "Timothy Z." Date: Thu, 10 Apr 2025 21:31:34 +0300 Subject: [PATCH] fix(useapplelogin): jwt errors --- src/routes/Intro/useAppleLogin.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/routes/Intro/useAppleLogin.ts b/src/routes/Intro/useAppleLogin.ts index 6f934a1c6..ec86700fc 100644 --- a/src/routes/Intro/useAppleLogin.ts +++ b/src/routes/Intro/useAppleLogin.ts @@ -1,5 +1,5 @@ import { useCallback, useRef } from 'react'; -import jwtDecode from 'jwt-decode'; +import { jwtDecode, JwtPayload } from 'jwt-decode'; type AppleLoginResponse = { token: string; @@ -20,6 +20,12 @@ type AppleSignInResponse = { lastName?: string; }; }; + +type CustomJWTPayload = JwtPayload & { + sub: string; + email?: string; +}; + const CLIENT_ID = 'com.stremio.services'; const useAppleLogin = (): [() => Promise, () => void] => { @@ -49,14 +55,14 @@ const useAppleLogin = (): [() => Promise, () => void] => { window.AppleID.auth.signIn().then((response: AppleSignInResponse) => { if (response.authorization) { - console.log('Apple Sign-In response:', response); // eslint-disable-line no-console + console.log('Apple Sign-In response:', response.authorization); // eslint-disable-line no-console try { const idToken = response.authorization.id_token; - const email = response.email || ''; - const payload = jwtDecode.jwtDecode(response.authorization.id_token); + const payload: CustomJWTPayload = jwtDecode(idToken); console.log('Decoded id_token:', payload); // eslint-disable-line no-console const sub = payload.sub; + const email = payload.email ?? response.email ?? ''; let name = ''; if (response.fullName) {