|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { createClient } from "~/utils/supabase/client"; |
| 4 | +import { useRouter, useSearchParams } from "next/navigation"; |
| 5 | +import { useState, useEffect, useCallback, useRef } from "react"; |
| 6 | +import useInternalError from "~/utils/internalError"; |
| 7 | + |
| 8 | +export const LoginWithToken = () => { |
| 9 | + const loginAttempted = useRef(false); |
| 10 | + const searchParams = useSearchParams(); |
| 11 | + const internalError = useInternalError(); |
| 12 | + const router = useRouter(); |
| 13 | + const [secretToken] = useState(() => searchParams.get("t")); |
| 14 | + useEffect(() => { |
| 15 | + if (secretToken && typeof window !== "undefined") { |
| 16 | + const url = new URL(window.location.href); |
| 17 | + url.searchParams.delete("t"); |
| 18 | + window.history.replaceState({}, "", url); |
| 19 | + } |
| 20 | + }, [secretToken]); |
| 21 | + const [url] = useState(searchParams.get("url")); |
| 22 | + const [done, setDone] = useState(false); |
| 23 | + const [error, setError] = useState<string | null>( |
| 24 | + secretToken === null ? "Please provide token" : null, |
| 25 | + ); |
| 26 | + |
| 27 | + const login = useCallback(async () => { |
| 28 | + try { |
| 29 | + const client = createClient(); |
| 30 | + const result = await client.rpc("get_secret_token", { |
| 31 | + token: secretToken!, |
| 32 | + }); |
| 33 | + if (result.error) { |
| 34 | + setError("Could not connect to DiscourseGraphs"); |
| 35 | + internalError({ error: result.error, type: "get-secret-token" }); |
| 36 | + return; |
| 37 | + } |
| 38 | + if (result.data == null) { |
| 39 | + setError("Could not retrieve information, please try again."); |
| 40 | + internalError({ error: "missing token", type: "get-secret-token" }); |
| 41 | + return; |
| 42 | + } |
| 43 | + if (typeof result.data !== "string") { |
| 44 | + setError( |
| 45 | + "DiscourseGraphs configuration error, the team has been warned", |
| 46 | + ); |
| 47 | + internalError({ |
| 48 | + error: "payload-not-string", |
| 49 | + type: "get-secret-token", |
| 50 | + }); |
| 51 | + return; |
| 52 | + } |
| 53 | + const data = JSON.parse(result.data) as { |
| 54 | + /* eslint-disable @typescript-eslint/naming-convention */ |
| 55 | + access_token: string; |
| 56 | + refresh_token: string; |
| 57 | + /* eslint-enable @typescript-eslint/naming-convention */ |
| 58 | + }; |
| 59 | + if ( |
| 60 | + !data || |
| 61 | + typeof data !== "object" || |
| 62 | + !data.access_token || |
| 63 | + !data.refresh_token |
| 64 | + ) { |
| 65 | + setError( |
| 66 | + "DiscourseGraphs configuration error, the team has been warned", |
| 67 | + ); |
| 68 | + internalError({ error: "misshaped-payload", type: "get-secret-token" }); |
| 69 | + return; |
| 70 | + } |
| 71 | + const response = await client.auth.setSession(data); |
| 72 | + if (response.error) { |
| 73 | + setError(response.error.message); |
| 74 | + } else if (url) { |
| 75 | + router.replace(url); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + setError("Unkown error while logging you in."); |
| 79 | + internalError({ error, type: "token-login-exception" }); |
| 80 | + } finally { |
| 81 | + setDone(true); |
| 82 | + } |
| 83 | + }, [secretToken, url, router, internalError]); |
| 84 | + useEffect(() => { |
| 85 | + if (!error && !done && !loginAttempted.current) { |
| 86 | + loginAttempted.current = true; |
| 87 | + void login(); |
| 88 | + } |
| 89 | + }, [error, login, secretToken, done]); |
| 90 | + return ( |
| 91 | + <div className="flex min-h-svh w-full items-center justify-center p-6 md:p-10"> |
| 92 | + <div className="w-full max-w-sm"> |
| 93 | + {error ? "Error: " + error : done ? "Logged in" : "Logging in"} |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +}; |
0 commit comments