diff --git a/apps/amethyst/app/auth/callback.tsx b/apps/amethyst/app/auth/callback.tsx index d0d60b0f..3ecf536d 100644 --- a/apps/amethyst/app/auth/callback.tsx +++ b/apps/amethyst/app/auth/callback.tsx @@ -1,56 +1,37 @@ -import React, { useEffect, useRef } from "react"; +import React, { useEffect, useSyncExternalStore } from "react"; import { View } from "react-native"; -import { Link, router, Stack } from "expo-router"; -import { useLocalSearchParams } from "expo-router/build/hooks"; +import { Link, router, Stack, useLocalSearchParams } from "expo-router"; +import { useOAuthCallback } from "@/lib/atp/useOAuthCallback"; import { Text } from "@/components/ui/text"; import { Icon } from "@/lib/icons/iconWithClassName"; import { useStore } from "@/stores/mainStore"; import { PencilLine } from "lucide-react-native"; -const toSearchParams = ( - params: Record, -): URLSearchParams => { - const searchParams = new URLSearchParams(); - for (const [key, value] of Object.entries(params)) { - if (Array.isArray(value)) { - for (const item of value) { - searchParams.append(key, item); - } - } else if (value !== undefined) { - searchParams.set(key, value); - } - } - return searchParams; -}; - -export default function AuthOptions() { +export default function AuthCallback() { const status = useStore((state) => state.status); - const params = useLocalSearchParams<"iss" | "state" | "code">(); - const { state } = params; - const calledRef = useRef(false); - - useEffect(() => { - // The OAuth state in IndexedDB is deleted on first read (replay prevention). - // Must only call oauthCallback once -- a second call will fail with - // "Unknown authorization session". - if (calledRef.current || !params) return; - calledRef.current = true; - const searchParams = toSearchParams(params); - useStore.getState().oauthCallback(searchParams); - }, [params]); + const complete = useStore((state) => state.oauthCallback); + const loginError = useStore((state) => state.login.error); + const params = useLocalSearchParams(); + const hydrated = useSyncExternalStore( + useStore.persist.onFinishHydration, + useStore.persist.hasHydrated, + () => false, + ); + useOAuthCallback(params, complete, hydrated); + const providerError = params.error !== undefined; useEffect(() => { - if (status === "loggedIn") { - requestAnimationFrame(() => { - router.replace("/"); - }); + if (status === "loggedIn" && !providerError) { + const frame = requestAnimationFrame(() => router.replace("/")); + return () => cancelAnimationFrame(frame); } - }, [status]); + }, [status, providerError]); - if (!params) { + if (providerError || (status === "loggedOut" && loginError)) { return ( - + Sign-in did not complete. + Try signing in again ); } @@ -67,10 +48,7 @@ export default function AuthOptions() { {status === "loggedIn" ? "Success!" : "Fetching your data..."} - This may take a few seconds {status} - - - {state} + This may take a few seconds. ); diff --git a/apps/amethyst/app/auth/login.tsx b/apps/amethyst/app/auth/login.tsx index 3c8f9157..a4b35541 100644 --- a/apps/amethyst/app/auth/login.tsx +++ b/apps/amethyst/app/auth/login.tsx @@ -171,6 +171,8 @@ const LoginScreen = () => { ); const handleLogin = async () => { + if (isLoading || isRedirecting) return; + // reset state if (debounceTimeoutRef.current) { clearTimeout(debounceTimeoutRef.current); @@ -200,8 +202,15 @@ const LoginScreen = () => { "http://127.0.0.1:8081/login", ); if (res.type === "success") { - const params = new URLSearchParams(res.url.split("?")[1]); + const params = new URL(res.url).searchParams; + if (params.has("error")) { + throw new Error("Sign-in did not complete. Try signing in again."); + } await oauthCallback(params); + if (useStore.getState().status !== "loggedIn") { + throw new Error("Sign-in did not complete. Try signing in again."); + } + router.replace("/"); } else if (res.type === "cancel" || res.type === "dismiss") { setErr("Login cancelled by user."); setIsRedirecting(false); @@ -314,6 +323,7 @@ const LoginScreen = () => { + {err ? {err} : null}