diff options
| author | MohamedBassem <me@mbassem.com> | 2024-03-11 14:36:04 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-03-11 14:36:47 +0000 |
| commit | e1c511c7c27034f94b8598b44467782af346b9c1 (patch) | |
| tree | 3b75e0277e3c795d6f03a60007363e54ad73e2ac /packages | |
| parent | b3f081b300125cfe43af38001fee392083a4b880 (diff) | |
| download | karakeep-e1c511c7c27034f94b8598b44467782af346b9c1.tar.zst | |
mobile: Do the session routing in the homescreen
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/mobile/app/dashboard.tsx | 23 | ||||
| -rw-r--r-- | packages/mobile/app/index.tsx | 23 | ||||
| -rw-r--r-- | packages/mobile/lib/session.ts | 20 | ||||
| -rw-r--r-- | packages/mobile/lib/storage-state.ts | 1 |
4 files changed, 50 insertions, 17 deletions
diff --git a/packages/mobile/app/dashboard.tsx b/packages/mobile/app/dashboard.tsx index 8be57615..b4f535c5 100644 --- a/packages/mobile/app/dashboard.tsx +++ b/packages/mobile/app/dashboard.tsx @@ -4,24 +4,27 @@ import { Text, View } from "react-native"; import Logo from "@/components/Logo"; import { Button } from "@/components/ui/Button"; -import useAppSettings from "@/lib/settings"; +import { useSession } from "@/lib/session"; import { api } from "@/lib/trpc"; -export default function Main() { +export default function Dashboard() { const router = useRouter(); - const { settings, setSettings, isLoading } = useAppSettings(); + + const { isLoggedIn, logout } = useSession(); useEffect(() => { - if (!isLoading && !settings.apiKey) { + if (isLoggedIn !== undefined && !isLoggedIn) { router.replace("signin"); } - }, [settings, isLoading]); + }, [isLoggedIn]); - const onLogout = () => { - setSettings({ ...settings, apiKey: undefined }); - }; + const { data, error, isLoading } = api.users.whoami.useQuery(); - const { data } = api.users.whoami.useQuery(); + useEffect(() => { + if (error?.data?.code === "UNAUTHORIZED") { + logout(); + } + }, [error]); return ( <View className="flex h-full items-center justify-center gap-4 px-4"> @@ -29,7 +32,7 @@ export default function Main() { <Text className="justify-center"> Logged in as: {isLoading ? "Loading ..." : data?.email} </Text> - <Button label="Log Out" onPress={onLogout} /> + <Button label="Log Out" onPress={logout} /> </View> ); } diff --git a/packages/mobile/app/index.tsx b/packages/mobile/app/index.tsx index 557417d7..5ce20cda 100644 --- a/packages/mobile/app/index.tsx +++ b/packages/mobile/app/index.tsx @@ -1,11 +1,20 @@ -import { Link } from "expo-router"; +import { useRouter } from "expo-router"; +import { useEffect } from "react"; import { View } from "react-native"; +import { useSession } from "@/lib/session"; + export default function App() { - return ( - <View className="flex-1 items-center justify-center gap-4 bg-white"> - <Link href="signin">Signin</Link> - <Link href="dashboard">Dashboard</Link> - </View> - ); + const router = useRouter(); + const { isLoggedIn } = useSession(); + useEffect(() => { + if (isLoggedIn === undefined) { + // Wait until it's loaded + } else if (isLoggedIn) { + router.replace("dashboard"); + } else { + router.replace("signin"); + } + }, [isLoggedIn]); + return <View />; } diff --git a/packages/mobile/lib/session.ts b/packages/mobile/lib/session.ts new file mode 100644 index 00000000..e2ab245b --- /dev/null +++ b/packages/mobile/lib/session.ts @@ -0,0 +1,20 @@ +import { useCallback, useMemo } from "react"; + +import useAppSettings from "./settings"; + +export function useSession() { + const { settings, isLoading, setSettings } = useAppSettings(); + const isLoggedIn = useMemo(() => { + return isLoading ? undefined : !!settings.apiKey; + }, [isLoading, settings]); + + const logout = useCallback(() => { + setSettings({ ...settings, apiKey: undefined }); + }, [settings]); + + return { + isLoggedIn, + isLoading, + logout, + }; +} diff --git a/packages/mobile/lib/storage-state.ts b/packages/mobile/lib/storage-state.ts index 09917c79..4988f0e0 100644 --- a/packages/mobile/lib/storage-state.ts +++ b/packages/mobile/lib/storage-state.ts @@ -31,6 +31,7 @@ export function useStorageState<T>(key: string): UseStateHook<T> { React.useEffect(() => { SecureStore.getItemAsync(key).then((value) => { if (!value) { + setState(null); return null; } setState(JSON.parse(value)); |
