From 592fb17fa14b63793da74f4befcbdd357c0f1885 Mon Sep 17 00:00:00 2001 From: "Timothy Z." Date: Thu, 10 Apr 2025 12:42:41 +0300 Subject: [PATCH] refactor(Apple login): support new endpoint --- src/routes/Intro/Intro.js | 9 +++++---- src/routes/Intro/useAppleLogin.ts | 28 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/routes/Intro/Intro.js b/src/routes/Intro/Intro.js index 0613e1490..d20e3ea60 100644 --- a/src/routes/Intro/Intro.js +++ b/src/routes/Intro/Intro.js @@ -112,16 +112,17 @@ const Intro = ({ queryParams }) => { const loginWithApple = React.useCallback(() => { openLoaderModal(); startAppleLogin() - .then(({ email, password }) => { + .then(({ email, token, sub, name }) => { core.transport.dispatch({ action: 'Ctx', args: { action: 'Authenticate', args: { - type: 'Login', + type: 'AuthWithApple', + token, + sub, email, - password, - apple: true + name } } }); diff --git a/src/routes/Intro/useAppleLogin.ts b/src/routes/Intro/useAppleLogin.ts index 695888bee..a3ec8fa38 100644 --- a/src/routes/Intro/useAppleLogin.ts +++ b/src/routes/Intro/useAppleLogin.ts @@ -1,8 +1,10 @@ import { useCallback, useRef } from 'react'; type AppleLoginResponse = { + token: string; + sub: string; email: string; - password: string; + name: string; }; type AppleSignInResponse = { @@ -13,6 +15,10 @@ type AppleSignInResponse = { }; user: string; email?: string; + fullName?: { + firstName?: string; + lastName?: string; + }; }; const CLIENT_ID = 'com.stremio.one'; @@ -45,16 +51,26 @@ const useAppleLogin = (): [() => Promise, () => void] => { window.AppleID.auth.signIn() .then((response: AppleSignInResponse) => { if (response.authorization) { - const userEmail = response.email || response.user; + const email = response.email || ''; + const sub = response.user; + + let name = ''; + if (response.fullName) { + const firstName = response.fullName.firstName || ''; + const lastName = response.fullName.lastName || ''; + name = [firstName, lastName].filter(Boolean).join(' '); + } - if (!userEmail) { - reject(new Error('No email received from Apple')); + if (!sub) { + reject(new Error('No sub token received from Apple')); return; } resolve({ - email: userEmail as string, - password: response.authorization.id_token + token: response.authorization.id_token, + sub: sub, + email: email, + name: name }); } else { reject(new Error('No authorization received from Apple'));