Files
kotosoft-id/kotosoft-id-frontend/app/api/auth/initiate/route.ts
T
2026-03-09 18:09:02 +03:00

26 lines
986 B
TypeScript

import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { pkceConfig } from "@/lib/cookie-config";
import crypto from "crypto";
export async function GET() {
const verifier = crypto.randomBytes(32).toString("base64url");
const challenge = crypto.createHash("sha256").update(verifier).digest("base64url");
const state = crypto.randomBytes(16).toString("hex");
const store = await cookies();
store.set("pkce_verifier", verifier, pkceConfig);
store.set("oauth_state", state, { ...pkceConfig, path: "/api/callback" });
const params = new URLSearchParams({
client_id: process.env.CLIENT_ID!,
redirect_uri: `${process.env.PUBLIC_URL}/api/callback`,
scope: "profile:read profile:edit profile:consent:read profile:consent:delete",
response_type: "code",
code_challenge: challenge,
code_challenge_method: "S256",
state,
});
redirect(`${process.env.KOTOSOFT_ID_API_URL}/oauth/authorize?${params.toString()}`);
}