UNPKG

@auth/core

Version:

Authentication for the Web.

56 lines (55 loc) 2.29 kB
/** Set default env variables on the config object */ export function setEnvDefaults(envObject, config) { try { const url = envObject.AUTH_URL; if (url && !config.basePath) config.basePath = new URL(url).pathname; } catch { } finally { config.basePath ?? (config.basePath = `/auth`); } if (!config.secret?.length) { config.secret = []; const secret = envObject.AUTH_SECRET; if (secret) config.secret.push(secret); for (const i of [1, 2, 3]) { const secret = envObject[`AUTH_SECRET_${i}`]; if (secret) config.secret.unshift(secret); } } config.redirectProxyUrl ?? (config.redirectProxyUrl = envObject.AUTH_REDIRECT_PROXY_URL); config.trustHost ?? (config.trustHost = !!(envObject.AUTH_URL ?? envObject.AUTH_TRUST_HOST ?? envObject.VERCEL ?? envObject.NODE_ENV !== "production")); config.providers = config.providers.map((p) => { const finalProvider = typeof p === "function" ? p({}) : p; const ID = finalProvider.id.toUpperCase(); if (finalProvider.type === "oauth" || finalProvider.type === "oidc") { finalProvider.clientId ?? (finalProvider.clientId = envObject[`AUTH_${ID}_ID`]); finalProvider.clientSecret ?? (finalProvider.clientSecret = envObject[`AUTH_${ID}_SECRET`]); if (finalProvider.type === "oidc") { finalProvider.issuer ?? (finalProvider.issuer = envObject[`AUTH_${ID}_ISSUER`]); } } else if (finalProvider.type === "email") { finalProvider.apiKey ?? (finalProvider.apiKey = envObject[`AUTH_${ID}_KEY`]); } return finalProvider; }); } export function createActionURL(action, protocol, headers, envObject, basePath) { let url = envObject.AUTH_URL ?? envObject.NEXTAUTH_URL; if (!url) { const host = headers.get("x-forwarded-host") ?? headers.get("host"); if (!host) throw new TypeError("Missing host"); const proto = headers.get("x-forwarded-proto") ?? protocol; url = `${proto === "http" ? "http" : "https"}://${host}${basePath}`; } return new URL(`${url.replace(/\/$/, "")}/${action}`); }