Fix account validity check on android

This commit is contained in:
Zoe Roux 2024-03-03 23:03:00 +01:00
parent 952beccafc
commit af422e62e1
7 changed files with 10 additions and 21 deletions

View File

@ -19,12 +19,10 @@
*/
import { ConnectionErrorContext, useAccount } from "@kyoo/models";
import { CircularProgress } from "@kyoo/primitives";
import { NavbarRight, NavbarTitle } from "@kyoo/ui";
import { useNetInfo } from "@react-native-community/netinfo";
import { Redirect, Stack } from "expo-router";
import * as SplashScreen from "expo-splash-screen";
import { useContext, useEffect } from "react";
import { useContext } from "react";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useTheme } from "yoshiki/native";
@ -33,14 +31,9 @@ export default function SignGuard() {
const theme = useTheme();
// TODO: support guest accounts on mobile too.
const account = useAccount();
const { loading, error } = useContext(ConnectionErrorContext);
const { error } = useContext(ConnectionErrorContext);
const netInfo = useNetInfo();
useEffect(() => {
if (!loading) SplashScreen.hideAsync();
}, [loading]);
if (loading) return <CircularProgress />;
if (error && netInfo.isConnected) return <Redirect href={"/connection-error"} />;
if (!account) return <Redirect href="/login" />;
return (

View File

@ -36,7 +36,7 @@ import {
Poppins_400Regular,
Poppins_900Black,
} from "@expo-google-fonts/poppins";
import { ReactNode, useState } from "react";
import { ReactNode, useEffect, useState } from "react";
import { useColorScheme } from "react-native";
import { initReactI18next } from "react-i18next";
import { ThemeProvider as RNThemeProvider } from "@react-navigation/native";
@ -117,6 +117,10 @@ export default function Root() {
if (theme === "auto") theme = systemTheme ?? "light";
useEffect(() => {
if (fontsLoaded) SplashScreen.hideAsync();
}, [fontsLoaded]);
if (!fontsLoaded) return null;
return (
<PersistQueryClientProvider

View File

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

View File

@ -136,7 +136,7 @@ export const AccountProvider = ({
<AccountContext.Provider value={accounts}>
<ConnectionErrorContext.Provider
value={{
error: user.error,
error: selected ? user.error : null,
loading: user.isLoading,
retry: () => {
queryClient.invalidateQueries({ queryKey: ["auth", "me"] });

View File

@ -26,7 +26,6 @@ import {
addAccount,
getCurrentAccount,
removeAccounts,
unselectAllAccounts,
updateAccount,
} from "./account-internal";
import { Platform } from "react-native";
@ -38,7 +37,6 @@ type Result<A, B> =
export const login = async (
action: "register" | "login",
{ apiUrl, ...body }: { username: string; password: string; email?: string; apiUrl?: string },
timeout?: number,
): Promise<Result<Account, string>> => {
try {
const controller = new AbortController();
@ -117,7 +115,6 @@ export const getTokenWJ = async (account?: Account | null): ReturnType<typeof ru
updateAccount(account.id, { ...account, token });
} catch (e) {
console.error("Error refreshing token durring ssr:", e);
if (Platform.OS !== "web" || typeof window !== "undefined") unselectAllAccounts();
return [null, null];
}
}

View File

@ -55,7 +55,7 @@ export const queryFn = async <Parser extends z.ZodTypeAny>(
type?: Parser,
token?: string | null,
): Promise<z.infer<Parser>> => {
const url = context.apiUrl ?? (Platform.OS === "web" ? kyooUrl : getCurrentAccount()!.apiUrl);
const url = context.apiUrl ?? (Platform.OS === "web" ? kyooUrl : getCurrentAccount()?.apiUrl);
kyooApiUrl = url;
if (token === undefined && context.authenticated !== false) token = await getToken();

View File

@ -26,6 +26,7 @@ import { useYoshiki } from "yoshiki/native";
export const ErrorView = ({ error }: { error: KyooErrors }) => {
const { css } = useYoshiki();
console.log(error)
return (
<View
{...css({