54 lines
2.1 KiB
TypeScript
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>;
|
|
}
|