fix(auth) : bind Sudloh sessions and stop OAuth redirect loops
This commit is contained in:
@@ -10,6 +10,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { authClient } from "@/lib/auth/client";
|
||||
import { sudlohStartErrorMessage } from "@/lib/auth/sudloh-redirect";
|
||||
import { ProfileImageInput } from "./profile-image-input";
|
||||
|
||||
declare global {
|
||||
@@ -187,7 +188,10 @@ export function AccountForm({
|
||||
const result = await authClient.signIn.social({
|
||||
provider: "sudloh", callbackURL: nextPath, errorCallbackURL,
|
||||
});
|
||||
if (result.error) throw new Error("เริ่มเข้าสู่ระบบด้วย Sudloh ไม่สำเร็จ กรุณาลองอีกครั้ง");
|
||||
if (result.error) {
|
||||
setError(sudlohStartErrorMessage(result.error.status));
|
||||
setBusy(false);
|
||||
}
|
||||
} catch {
|
||||
setError("เริ่มเข้าสู่ระบบด้วย Sudloh ไม่สำเร็จ กรุณาลองอีกครั้ง");
|
||||
setBusy(false);
|
||||
|
||||
@@ -7,6 +7,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem,
|
||||
DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
||||
import { authClient } from "@/lib/auth/client";
|
||||
import { markSudlohSignInAttempt } from "@/lib/auth/sudloh-redirect";
|
||||
import { unsubscribeCommissionPush } from "@/components/commission/push-client";
|
||||
|
||||
export function AccountMenu({ admin = false }: { admin?: boolean }) {
|
||||
@@ -17,6 +18,7 @@ export function AccountMenu({ admin = false }: { admin?: boolean }) {
|
||||
async function signOut() {
|
||||
await unsubscribeCommissionPush().catch(() => undefined);
|
||||
await authClient.signOut();
|
||||
markSudlohSignInAttempt();
|
||||
router.push(admin ? "/auth/login?next=%2Fadmin" : "/commission");
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
@@ -3,32 +3,51 @@
|
||||
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 [failed, setFailed] = useState(false);
|
||||
const [message, setMessage] = useState("กำลังไปที่ Sudloh Account…");
|
||||
const [showRetry, setShowRetry] = useState(false);
|
||||
|
||||
const start = useCallback(async () => {
|
||||
setFailed(false);
|
||||
const result = await authClient.signIn.social({
|
||||
provider: "sudloh",
|
||||
callbackURL: nextPath,
|
||||
errorCallbackURL: `/auth/login?next=${encodeURIComponent(nextPath)}&sudloh=1`,
|
||||
});
|
||||
if (result.error) setFailed(true);
|
||||
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(() => {
|
||||
if (started.current) return;
|
||||
started.current = true;
|
||||
void start().catch(() => setFailed(true));
|
||||
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>{failed ? "ไม่สามารถเริ่มเข้าสู่ระบบได้" : "กำลังไปที่ Sudloh Account…"}</p>
|
||||
<p>{message}</p>
|
||||
<p className="max-w-sm text-sm text-muted-foreground">เข้าสู่ระบบหรือสมัครสมาชิกที่ Sudloh Account แล้วระบบจะพาคุณกลับมายัง Buzz Guide</p>
|
||||
{failed && <Button onClick={() => void start().catch(() => setFailed(true))}>ลองอีกครั้ง</Button>}
|
||||
{showRetry && <Button onClick={() => void start()}>ลองอีกครั้ง</Button>}
|
||||
</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user