const API_URL = process.env.KOTOSOFT_ID_API_URL!; const CLIENT_ID = process.env.KOTOSOFT_CLIENT_ID!; const CLIENT_SECRET = process.env.KOTOSOFT_CLIENT_SECRET!; export async function exchangeCode(code: string, codeVerifier: string) { const body = new URLSearchParams({ grant_type: "authorization_code", code, redirect_uri: `${process.env.PUBLIC_URL}/api/callback`, code_verifier: codeVerifier, }); const response = await fetch(`${API_URL}/oauth/token`, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: `Basic ${Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString("base64")}`, }, body, }); if (!response.ok) throw new Error("Failed to exchange code"); return response.json() as Promise<{ access_token: string; refresh_token: string; expires_in: number }>; } export async function fetchKotosoftUser(accessToken: string) { const response = await fetch(`${API_URL}/api/v1/me`, { headers: { Authorization: `Bearer ${accessToken}` }, }); if (!response.ok) throw new Error("Failed to fetch user"); return response.json() as Promise<{ id: string; email: string; first_name: string; second_name: string; sex: string; birth_date: string; }>; }