spacemeowx2 2023-06-02 17:13:31 +08:00
parent 812e7ab611
commit e1b3703401
1 changed files with 20 additions and 9 deletions

View File

@ -175,13 +175,14 @@ export async function getGToken(
}, },
); );
const uiRespJson = await uiResp.json(); const uiRespJson = await uiResp.json();
const { nickname, birthday, language, country } = uiRespJson; const { nickname, birthday, language, country, id: userId } = uiRespJson;
const getIdToken2 = async (idToken: string) => { const getIdToken2 = async (idToken: string) => {
const { f, request_id: requestId, timestamp } = await callImink({ const { f, request_id: requestId, timestamp } = await callImink({
fApi, fApi,
step: 1, step: 1,
idToken, idToken,
userId,
env, env,
}); });
const resp = await fetch.post( const resp = await fetch.post(
@ -210,23 +211,28 @@ export async function getGToken(
); );
const respJson = await resp.json(); const respJson = await resp.json();
const idToken2 = respJson?.result?.webApiServerCredential?.accessToken; const idToken2: string = respJson?.result?.webApiServerCredential
?.accessToken;
const coralUserId: number = respJson?.result?.user?.id;
if (!idToken2) { if (!idToken2 || !coralUserId) {
throw new APIError({ throw new APIError({
response: resp, response: resp,
json: respJson, json: respJson,
message: "No idToken2 found", message:
`No idToken2 or coralUserId found. Please try again later. ('${idToken2}', '${coralUserId}')`,
}); });
} }
return idToken2 as string; return [idToken2, coralUserId] as const;
}; };
const getGToken = async (idToken: string) => { const getGToken = async (idToken: string, coralUserId: number) => {
const { f, request_id: requestId, timestamp } = await callImink({ const { f, request_id: requestId, timestamp } = await callImink({
step: 2, step: 2,
idToken, idToken,
fApi, fApi,
userId,
coralUserId,
env, env,
}); });
const resp = await fetch.post( const resp = await fetch.post(
@ -266,8 +272,8 @@ export async function getGToken(
return webServiceToken as string; return webServiceToken as string;
}; };
const idToken2 = await retry(() => getIdToken2(idToken)); const [idToken2, coralUserId] = await retry(() => getIdToken2(idToken));
const webServiceToken = await retry(() => getGToken(idToken2)); const webServiceToken = await retry(() => getGToken(idToken2, coralUserId));
return { return {
webServiceToken, webServiceToken,
@ -403,13 +409,16 @@ type IminkResponse = {
timestamp: number; timestamp: number;
}; };
async function callImink( async function callImink(
{ fApi, step, idToken, env }: { params: {
fApi: string; fApi: string;
step: number; step: number;
idToken: string; idToken: string;
userId: string;
coralUserId?: number;
env: Env; env: Env;
}, },
): Promise<IminkResponse> { ): Promise<IminkResponse> {
const { fApi, step, idToken, userId, coralUserId, env } = params;
const { post } = env.newFetcher(); const { post } = env.newFetcher();
const resp = await post({ const resp = await post({
url: fApi, url: fApi,
@ -420,6 +429,8 @@ async function callImink(
body: JSON.stringify({ body: JSON.stringify({
"token": idToken, "token": idToken,
"hash_method": step, "hash_method": step,
"na_id": userId,
"coral_user_id": coralUserId,
}), }),
}); });