import { fetchSession } from "@/actions/oauth"; import { isRedirectError } from "next/dist/client/components/redirect-error"; import { cookies } from "next/headers"; import { redirect } from "next/navigation"; export const dynamic = "force-dynamic"; export default async function OAuthPage({ searchParams, }: { searchParams: Promise<{ [key: string]: string | string[] | undefined }>; }) { try { const session = (await searchParams).session as string | undefined; if (!session) redirect("/oauth/error"); const data = await fetchSession(session); if (!data) redirect("/oauth/error"); const store = await cookies(); const accessToken = store.get("access_token")?.value; if (accessToken) { const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/oauth/session/attach`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}` }, body: JSON.stringify({ session }), cache: "no-store", }); if (res.ok) { const body = await res.json(); if (body.redirect_url) redirect(body.redirect_url); redirect(`/consent?session=${session}`); } } redirect(`/login?session=${session}`); } catch (error) { if (isRedirectError(error)) throw error; console.error("OAuth page error:", error); redirect("/oauth/error"); } }