|
2 | 2 |
|
3 | 3 | import { createClient } from "~/utils/supabase/client"; |
4 | 4 | import { useRouter, useSearchParams } from "next/navigation"; |
5 | | -import { useState, useEffect, useCallback } from "react"; |
| 5 | +import { useState, useEffect, useCallback, useRef } from "react"; |
6 | 6 |
|
7 | 7 | export const LoginWithToken = () => { |
| 8 | + const loginAttempted = useRef(false); |
8 | 9 | const searchParams = useSearchParams(); |
9 | 10 | const router = useRouter(); |
10 | | - const [secretToken] = useState(searchParams.get("t")); |
| 11 | + const [secretToken] = useState(() => { |
| 12 | + const t = searchParams.get("t"); |
| 13 | + if (t && typeof window !== "undefined") { |
| 14 | + const url = new URL(window.location.href); |
| 15 | + url.searchParams.delete("t"); |
| 16 | + window.history.replaceState({}, "", url); |
| 17 | + } |
| 18 | + return t; |
| 19 | + }); |
11 | 20 | const [url] = useState(searchParams.get("url")); |
12 | 21 | const [done, setDone] = useState(false); |
13 | 22 | const [error, setError] = useState<string | null>( |
@@ -62,7 +71,8 @@ export const LoginWithToken = () => { |
62 | 71 | } |
63 | 72 | }, [secretToken, url, router]); |
64 | 73 | useEffect(() => { |
65 | | - if (!error && !done) { |
| 74 | + if (!error && !done && !loginAttempted.current) { |
| 75 | + loginAttempted.current = true; |
66 | 76 | void login(); |
67 | 77 | } |
68 | 78 | }, [error, login, secretToken, done]); |
|
0 commit comments