fix(useapplelogin): get sub id from token

This commit is contained in:
Timothy Z. 2025-04-10 21:10:50 +03:00
parent 50edda2557
commit 846445001c
4 changed files with 31 additions and 24 deletions

10
package-lock.json generated
View file

@ -23,6 +23,7 @@
"filter-invalid-dom-props": "3.0.1",
"hat": "^0.0.3",
"i18next": "^24.0.5",
"jwt-decode": "^4.0.0",
"langs": "github:Stremio/nodejs-langs",
"lodash.debounce": "4.0.8",
"lodash.intersection": "4.4.0",
@ -10233,6 +10234,15 @@
"node": ">=4.0"
}
},
"node_modules/jwt-decode": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz",
"integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==",
"license": "MIT",
"engines": {
"node": ">=18"
}
},
"node_modules/keyv": {
"version": "4.5.4",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",

View file

@ -27,6 +27,7 @@
"filter-invalid-dom-props": "3.0.1",
"hat": "^0.0.3",
"i18next": "^24.0.5",
"jwt-decode": "^4.0.0",
"langs": "github:Stremio/nodejs-langs",
"lodash.debounce": "4.0.8",
"lodash.intersection": "4.4.0",

View file

@ -1,4 +1,5 @@
import { useCallback, useRef } from 'react';
import jwtDecode from 'jwt-decode';
type AppleLoginResponse = {
token: string;
@ -9,10 +10,9 @@ type AppleLoginResponse = {
type AppleSignInResponse = {
authorization: {
code?: string;
id_token: string;
};
authorizedData: {
userId: string;
state?: string;
};
email?: string;
fullName?: {
@ -20,7 +20,6 @@ type AppleSignInResponse = {
lastName?: string;
};
};
const CLIENT_ID = 'com.stremio.services';
const useAppleLogin = (): [() => Promise<AppleLoginResponse>, () => void] => {
@ -48,13 +47,15 @@ const useAppleLogin = (): [() => Promise<AppleLoginResponse>, () => void] => {
usePopup: true,
});
window.AppleID.auth
.signIn()
.then((response: AppleSignInResponse) => {
if (response.authorization) {
console.log('Apple Sign-In response:', response); // eslint-disable-line no-console
window.AppleID.auth.signIn().then((response: AppleSignInResponse) => {
if (response.authorization) {
console.log('Apple Sign-In response:', response); // eslint-disable-line no-console
try {
const idToken = response.authorization.id_token;
const email = response.email || '';
const sub = response.authorizedData.userId;
const payload = jwtDecode.jwtDecode(response.authorization.id_token);
const sub = payload.sub;
let name = '';
if (response.fullName) {
@ -69,22 +70,18 @@ const useAppleLogin = (): [() => Promise<AppleLoginResponse>, () => void] => {
}
resolve({
token: response.authorization.id_token,
token: idToken,
sub: sub,
email: email,
name: name,
});
} else {
reject(new Error('No authorization received from Apple'));
} catch (error) {
reject(new Error(`Failed to parse id_token: ${error}`));
}
})
.catch((error: Error) => {
console.error('Error during Apple Sign-In:', error);
reject(error);
})
.finally(() => {
started.current = false;
});
} else {
reject(new Error('No authorization received from Apple'));
}
});
});
}, []);

View file

@ -38,10 +38,9 @@ declare global {
}) => void;
signIn: () => Promise<{
authorization: {
code?: string;
id_token: string;
};
authorizedData: {
userId: string;
state?: string;
};
user: string;
email?: string;