refactor(Apple login): support new endpoint

This commit is contained in:
Timothy Z. 2025-04-10 12:42:41 +03:00
parent 9a6f01b6ad
commit 592fb17fa1
2 changed files with 27 additions and 10 deletions

View file

@ -112,16 +112,17 @@ const Intro = ({ queryParams }) => {
const loginWithApple = React.useCallback(() => { const loginWithApple = React.useCallback(() => {
openLoaderModal(); openLoaderModal();
startAppleLogin() startAppleLogin()
.then(({ email, password }) => { .then(({ email, token, sub, name }) => {
core.transport.dispatch({ core.transport.dispatch({
action: 'Ctx', action: 'Ctx',
args: { args: {
action: 'Authenticate', action: 'Authenticate',
args: { args: {
type: 'Login', type: 'AuthWithApple',
token,
sub,
email, email,
password, name
apple: true
} }
} }
}); });

View file

@ -1,8 +1,10 @@
import { useCallback, useRef } from 'react'; import { useCallback, useRef } from 'react';
type AppleLoginResponse = { type AppleLoginResponse = {
token: string;
sub: string;
email: string; email: string;
password: string; name: string;
}; };
type AppleSignInResponse = { type AppleSignInResponse = {
@ -13,6 +15,10 @@ type AppleSignInResponse = {
}; };
user: string; user: string;
email?: string; email?: string;
fullName?: {
firstName?: string;
lastName?: string;
};
}; };
const CLIENT_ID = 'com.stremio.one'; const CLIENT_ID = 'com.stremio.one';
@ -45,16 +51,26 @@ const useAppleLogin = (): [() => Promise<AppleLoginResponse>, () => void] => {
window.AppleID.auth.signIn() window.AppleID.auth.signIn()
.then((response: AppleSignInResponse) => { .then((response: AppleSignInResponse) => {
if (response.authorization) { if (response.authorization) {
const userEmail = response.email || response.user; const email = response.email || '';
const sub = response.user;
if (!userEmail) { let name = '';
reject(new Error('No email received from Apple')); if (response.fullName) {
const firstName = response.fullName.firstName || '';
const lastName = response.fullName.lastName || '';
name = [firstName, lastName].filter(Boolean).join(' ');
}
if (!sub) {
reject(new Error('No sub token received from Apple'));
return; return;
} }
resolve({ resolve({
email: userEmail as string, token: response.authorization.id_token,
password: response.authorization.id_token sub: sub,
email: email,
name: name
}); });
} else { } else {
reject(new Error('No authorization received from Apple')); reject(new Error('No authorization received from Apple'));