Unselect account during refresh errors (could happen if server db was cleared)

This commit is contained in:
Zoe Roux 2023-12-05 22:28:43 +01:00
parent 87fdc1a444
commit f4442fad0c
2 changed files with 18 additions and 6 deletions

View File

@ -107,3 +107,9 @@ export const updateAccount = (id: string, account: Account) => {
accounts[idx] = account; accounts[idx] = account;
writeAccounts(accounts); writeAccounts(accounts);
}; };
export const unselectAllAccounts = () => {
const accounts = readAccounts();
for (const acc of accounts) acc.selected = false;
writeAccounts(accounts);
}

View File

@ -20,9 +20,15 @@
import { queryFn } from "./query"; import { queryFn } from "./query";
import { KyooErrors } from "./kyoo-errors"; import { KyooErrors } from "./kyoo-errors";
import { Account, Token, TokenP } from "./accounts"; import { Account, TokenP } from "./accounts";
import { UserP } from "./resources"; import { UserP } from "./resources";
import { addAccount, getCurrentAccount, removeAccounts, updateAccount } from "./account-internal"; import {
addAccount,
getCurrentAccount,
removeAccounts,
unselectAllAccounts,
updateAccount,
} from "./account-internal";
import { Platform } from "react-native"; import { Platform } from "react-native";
type Result<A, B> = type Result<A, B> =
@ -62,9 +68,7 @@ export const login = async (
let running: ReturnType<typeof getTokenWJ> | null = null; let running: ReturnType<typeof getTokenWJ> | null = null;
export const getTokenWJ = async ( export const getTokenWJ = async (account?: Account | null): ReturnType<typeof run> => {
account?: Account | null,
): ReturnType<typeof run> => {
async function run() { async function run() {
if (account === undefined) account = getCurrentAccount(); if (account === undefined) account = getCurrentAccount();
if (!account) return [null, null] as const; if (!account) return [null, null] as const;
@ -81,10 +85,12 @@ export const getTokenWJ = async (
}, },
TokenP, TokenP,
); );
if (Platform.OS === "web" && typeof window !== "undefined") if (Platform.OS !== "web" || typeof window !== "undefined")
updateAccount(account.id, { ...account, token }); updateAccount(account.id, { ...account, token });
} catch (e) { } catch (e) {
console.error("Error refreshing token durring ssr:", e); console.error("Error refreshing token durring ssr:", e);
if (Platform.OS !== "web" || typeof window !== "undefined") unselectAllAccounts();
return [null, null];
} }
} }
return [`${token.token_type} ${token.access_token}`, token] as const; return [`${token.token_type} ${token.access_token}`, token] as const;