diff --git a/src/services/idnot/index.ts b/src/services/idnot/index.ts index 8b82265..e3d2a88 100644 --- a/src/services/idnot/index.ts +++ b/src/services/idnot/index.ts @@ -5,20 +5,28 @@ dotenv.config(); export class IdNotService { static async exchangeCodeForTokens(code: string) { + + const tokenUrl = process.env.IDNOT_TOKEN_URL; + const clientId = process.env.IDNOT_CLIENT_ID; + const clientSecret = process.env.IDNOT_CLIENT_SECRET; + const redirectUri = process.env.IDNOT_REDIRECT_URI; + + if (!tokenUrl || !clientId || !clientSecret || !redirectUri) { + throw new Error("Missing required IDNOT environment variables"); + } + const params = { - client_id: process.env.IDNOT_CLIENT_ID, - client_secret: process.env.IDNOT_CLIENT_SECRET, - redirect_uri: process.env.IDNOT_REDIRECT_URI, - grant_type: 'authorization_code', + client_id: clientId, + client_secret: clientSecret, + redirect_uri: redirectUri, + grant_type: "authorization_code", code: code }; const tokens = await ( - await fetch(process.env.IDNOT_TOKEN_URL, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, + await fetch(tokenUrl, { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(params).toString() }) ).json();