111 lines
3.9 KiB
TypeScript
111 lines
3.9 KiB
TypeScript
"use client"
|
|
import Header from "@/components/header";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { useTranslations } from "next-intl";
|
|
import { useEffect, useState } from "react";
|
|
import { useSearchParams } from "next/navigation";
|
|
import { getSession, Approve } from "@/actions/oauth";
|
|
import { Check } from "lucide-react";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import Link from "next/link";
|
|
|
|
interface Session {
|
|
client_name: string;
|
|
scopes: string[];
|
|
redirect_uri: string;
|
|
}
|
|
|
|
export default function ConsentPage() {
|
|
const searchParams = useSearchParams();
|
|
const sessionToken = searchParams.get("session") || "";
|
|
const t = useTranslations("ConsentPage");
|
|
|
|
const [session, setSession] = useState<Session | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
useEffect(() => {
|
|
getSession(sessionToken)
|
|
.then(data => {
|
|
if (!data) throw new Error();
|
|
setSession(data);
|
|
})
|
|
.catch(() => setError(t("invalidSession")));
|
|
}, []);
|
|
|
|
const handleDecision = async (allow: boolean) => {
|
|
setIsSubmitting(true);
|
|
if (allow) {
|
|
const result = await Approve(sessionToken, allow);
|
|
if (result) {
|
|
window.location.href = result;
|
|
} else {
|
|
setError(t("invalidSession"));
|
|
setIsSubmitting(false);
|
|
}
|
|
} else {
|
|
window.location.href = session?.redirect_uri || "/";
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Header />
|
|
<div className="bg-background flex min-h-svh flex-col items-center justify-center gap-6 p-6 md:p-10">
|
|
<div className="w-full max-w-sm flex flex-col gap-6">
|
|
{error ? (
|
|
<div className="flex flex-col gap-6">
|
|
<p className="text-destructive">{error}</p>
|
|
<div className="flex gap-3">
|
|
<Link href={session?.redirect_uri || "/"}><Button>{t("toApp")}</Button></Link>
|
|
<Link href="/"><Button variant="outline">{t("toHome")}</Button></Link>
|
|
</div>
|
|
</div>
|
|
) : !session ? (
|
|
<>
|
|
<div className="flex flex-col gap-2">
|
|
<div className="flex flex-col gap-2">
|
|
<Skeleton className="w-64 h-5" />
|
|
<Skeleton className="w-86 h-5" />
|
|
<Skeleton className="w-64 h-5" />
|
|
</div>
|
|
<div className="flex flex-col gap-2 my-4">
|
|
<Skeleton className="w-86 h-4" />
|
|
<Skeleton className="w-86 h-4" />
|
|
</div>
|
|
<div className="flex gap-3">
|
|
<Skeleton className="w-17 h-9" />
|
|
<Skeleton className="w-17 h-9" />
|
|
</div>
|
|
</div>
|
|
</>
|
|
) : (
|
|
<>
|
|
<div className="flex flex-col gap-2">
|
|
<h1 className="text-xl">{t("title", { service: session.client_name })}</h1>
|
|
<h2 className="text-lg">{t("subtitle", { service: session.client_name })}</h2>
|
|
</div>
|
|
<div className="flex flex-col gap-1">
|
|
{session.scopes.map(scope => (
|
|
<div key={scope} className="flex flex-row gap-3 items-center">
|
|
<Check size={18} />
|
|
<h3 className="text-base">{t(`scopes.${scope}`, { defaultValue: scope })}</h3>
|
|
</div>
|
|
))}
|
|
</div>
|
|
<div className="flex gap-3">
|
|
<Button onClick={() => handleDecision(true)} disabled={isSubmitting}>
|
|
{isSubmitting && <Spinner />}{t("allow")}
|
|
</Button>
|
|
<Button onClick={() => handleDecision(false)} disabled={isSubmitting} variant="outline">
|
|
{t("deny")}
|
|
</Button>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
} |