|
@@ -1,17 +1,20 @@
|
|
|
"use client";
|
|
"use client";
|
|
|
|
|
|
|
|
-import { useEffect, useRef } from "react";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
import { useRouter } from "next/navigation";
|
|
|
-import { useMutation } from "@tanstack/react-query";
|
|
|
|
|
|
|
+import { useQuery } from "@tanstack/react-query";
|
|
|
import { getSupabaseBrowserClient } from "@/lib/supabase/client";
|
|
import { getSupabaseBrowserClient } from "@/lib/supabase/client";
|
|
|
import { RecoveryCodeDisplay } from "@/components/auth/recovery-code-display";
|
|
import { RecoveryCodeDisplay } from "@/components/auth/recovery-code-display";
|
|
|
|
|
|
|
|
|
|
+// useQuery (not useMutation) so TanStack Query dedupes across StrictMode
|
|
|
|
|
+// double-mount and post-signup navigation churn. The server overwrites
|
|
|
|
|
+// users.recovery_code on each call, so re-firing on a true page reload is
|
|
|
|
|
+// fine — but within one browser session we want exactly one fire.
|
|
|
export default function RecoveryPage() {
|
|
export default function RecoveryPage() {
|
|
|
const router = useRouter();
|
|
const router = useRouter();
|
|
|
- const hasRequested = useRef(false);
|
|
|
|
|
|
|
|
|
|
- const generateMutation = useMutation({
|
|
|
|
|
- mutationFn: async () => {
|
|
|
|
|
|
|
+ const { data, isPending, error } = useQuery({
|
|
|
|
|
+ queryKey: ["recovery-code-generate"],
|
|
|
|
|
+ queryFn: async () => {
|
|
|
const supabase = getSupabaseBrowserClient();
|
|
const supabase = getSupabaseBrowserClient();
|
|
|
const {
|
|
const {
|
|
|
data: { session },
|
|
data: { session },
|
|
@@ -25,25 +28,20 @@ export default function RecoveryPage() {
|
|
|
Authorization: `Bearer ${session.access_token}`,
|
|
Authorization: `Bearer ${session.access_token}`,
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
- const data = (await res.json()) as { code?: string; error?: string };
|
|
|
|
|
-
|
|
|
|
|
- if (!res.ok) {
|
|
|
|
|
- throw new Error(data.error || "Failed to generate recovery code");
|
|
|
|
|
|
|
+ const body = (await res.json()) as { code?: string; error?: string };
|
|
|
|
|
+ if (!res.ok || !body.code) {
|
|
|
|
|
+ throw new Error(body.error || "Failed to generate recovery code");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return data.code!;
|
|
|
|
|
|
|
+ return body.code;
|
|
|
},
|
|
},
|
|
|
|
|
+ staleTime: Infinity,
|
|
|
|
|
+ gcTime: Infinity,
|
|
|
|
|
+ retry: 1,
|
|
|
|
|
+ refetchOnWindowFocus: false,
|
|
|
|
|
+ refetchOnMount: false,
|
|
|
|
|
+ refetchOnReconnect: false,
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- useEffect(() => {
|
|
|
|
|
- if (!hasRequested.current) {
|
|
|
|
|
- hasRequested.current = true;
|
|
|
|
|
- generateMutation.mutate();
|
|
|
|
|
- }
|
|
|
|
|
- // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
- }, []);
|
|
|
|
|
-
|
|
|
|
|
function handleConfirm() {
|
|
function handleConfirm() {
|
|
|
router.push("/");
|
|
router.push("/");
|
|
|
}
|
|
}
|
|
@@ -52,21 +50,17 @@ export default function RecoveryPage() {
|
|
|
<>
|
|
<>
|
|
|
<h1 className="mb-6 text-2xl font-bold text-center">Recovery Code</h1>
|
|
<h1 className="mb-6 text-2xl font-bold text-center">Recovery Code</h1>
|
|
|
|
|
|
|
|
- {generateMutation.isPending && (
|
|
|
|
|
|
|
+ {isPending && (
|
|
|
<p className="text-center text-foreground/60">Generating your recovery code...</p>
|
|
<p className="text-center text-foreground/60">Generating your recovery code...</p>
|
|
|
)}
|
|
)}
|
|
|
|
|
|
|
|
- {generateMutation.error && (
|
|
|
|
|
|
|
+ {error && (
|
|
|
<p className="text-center text-red-500" role="alert">
|
|
<p className="text-center text-red-500" role="alert">
|
|
|
- {generateMutation.error instanceof Error
|
|
|
|
|
- ? generateMutation.error.message
|
|
|
|
|
- : "Something went wrong."}
|
|
|
|
|
|
|
+ {error instanceof Error ? error.message : "Something went wrong."}
|
|
|
</p>
|
|
</p>
|
|
|
)}
|
|
)}
|
|
|
|
|
|
|
|
- {generateMutation.data && (
|
|
|
|
|
- <RecoveryCodeDisplay code={generateMutation.data} onConfirm={handleConfirm} />
|
|
|
|
|
- )}
|
|
|
|
|
|
|
+ {data && <RecoveryCodeDisplay code={data} onConfirm={handleConfirm} />}
|
|
|
</>
|
|
</>
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|