"use client"; import { useSearchParams, useRouter } from "next/navigation"; import { Suspense, useEffect, useState } from "react"; import { LoginForm } from "@/components/auth/login-form"; import { useLanguage } from "@/components/language-provider"; interface Application { client_id: string; name: string; redirect_uris: string[]; } interface ConsentData { application: Application; scopes: string[]; login_url: string; } function ConsentContent() { const searchParams = useSearchParams(); const router = useRouter(); const { lang, dir } = useLanguage(); const client_id = searchParams.get("client_id") || ""; const redirect_uri = searchParams.get("redirect_uri") || ""; const scope = searchParams.get("scope") || "openid"; const state = searchParams.get("state") || ""; const nonce = searchParams.get("nonce") || ""; const code_challenge = searchParams.get("code_challenge") || ""; const code_challenge_method = searchParams.get("code_challenge_method") || "plain"; const [consentData, setConsentData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [token, setToken] = useState(null); const t = (fa: string, en: string) => (lang === "fa" ? fa : en); const fetchConsent = async () => { try { const storedToken = localStorage.getItem("access_token"); if (storedToken) { setToken(storedToken); } const params = new URLSearchParams({ client_id, redirect_uri, response_type: "code", scope, state, nonce, code_challenge, code_challenge_method, }); const headers: Record = {}; if (storedToken) { headers["Authorization"] = `Bearer ${storedToken}`; } const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/oauth/authorize?${params.toString()}`, { headers } ); if (res.status === 401) { const data = await res.json(); setConsentData({ application: data.application || { client_id, name: "", redirect_uris: [redirect_uri] }, scopes: data.scopes || scope.split(" "), login_url: data.login_url, }); setToken(null); } else if (res.ok) { const data = await res.json(); setConsentData({ application: data.application || { client_id, name: "", redirect_uris: [redirect_uri] }, scopes: data.scopes || scope.split(" "), login_url: "", }); } } catch (e) { setError("خطا در بارگذاری اطلاعات برنامه"); } finally { setLoading(false); } }; useEffect(() => { fetchConsent(); }, []); const handleConsent = async () => { if (!token) return; const params = new URLSearchParams({ client_id, redirect_uri, response_type: "code", scope, state, nonce, code_challenge, code_challenge_method, }); const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/oauth/authorize?${params.toString()}`, { headers: { Authorization: `Bearer ${token}` }, } ); if (res.ok) { const data = await res.json(); const code = data.code; const redirectParams = new URLSearchParams(); redirectParams.set("code", code); if (state) redirectParams.set("state", state); window.location.href = `${redirect_uri}?${redirectParams.toString()}`; } }; const handleLogin = (access: string) => { setToken(access); handleConsent(); }; if (loading) { return (

{t("در حال بارگذاری...", "Loading...")}

); } return (

{t("تایید دسترسی", "Authorize Application")}

{error && (
{error}
)} {consentData && (

{t("برنامه درخواست دسترسی به:", "This application requests access to:")}

{consentData.application?.name?.charAt(0) || "App"}
{consentData.application?.name || client_id}
{client_id}

{t("دسترسی‌های درخواستی:", "Requested permissions:")}

    {consentData.scopes.map((s: string) => (
  • {s}
  • ))}
{!token ? ( ) : (
)}
)}
); } export default function ConsentPage() { return (
Loading...
}>
); }