Files
buzz-sheet/components/auth/sudloh-sign-in-redirect.tsx
T
gunshiz 445865bf42
CI / Verify (push) Successful in 1m31s
CI / Build immutable images and deploy (push) Successful in 2m4s
fix(auth) : bind Sudloh sessions and stop OAuth redirect loops
2026-10-06 14:28:59 +07:00

54 lines
2.1 KiB
TypeScript

"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import { authClient } from "@/lib/auth/client";
import { claimAutomaticSudlohSignIn, markSudlohSignInAttempt, sudlohStartErrorMessage } from "@/lib/auth/sudloh-redirect";
export function SudlohSignInRedirect({ nextPath }: { nextPath: string }) {
const started = useRef(false);
const [message, setMessage] = useState("กำลังไปที่ Sudloh Account…");
const [showRetry, setShowRetry] = useState(false);
const start = useCallback(async () => {
markSudlohSignInAttempt();
setShowRetry(false);
setMessage("กำลังไปที่ Sudloh Account…");
try {
const result = await authClient.signIn.social({
provider: "sudloh",
callbackURL: nextPath,
errorCallbackURL: `/auth/login?next=${encodeURIComponent(nextPath)}&sudloh=1`,
});
if (result.error) {
setMessage(sudlohStartErrorMessage(result.error.status));
setShowRetry(true);
}
} catch {
setMessage(sudlohStartErrorMessage());
setShowRetry(true);
}
}, [nextPath]);
useEffect(() => {
const timer = window.setTimeout(() => {
if (started.current) return;
started.current = true;
if (claimAutomaticSudlohSignIn()) void start();
else {
setMessage("การเข้าสู่ระบบยังไม่เสร็จ กรุณาลองอีกครั้ง");
setShowRetry(true);
}
}, 0);
return () => window.clearTimeout(timer);
}, [start]);
return <div className="grid min-h-[calc(100svh-4rem)] place-items-center p-4">
<div className="flex flex-col items-center gap-4 text-center">
<p>{message}</p>
<p className="max-w-sm text-sm text-muted-foreground">เข้าสู่ระบบหรือสมัครสมาชิกที่ Sudloh Account แล้วระบบจะพาคุณกลับมายัง Buzz Guide</p>
{showRetry && <Button onClick={() => void start()}>ลองอีกครั้ง</Button>}
</div>
</div>;
}