next-auth
Version:
Authentication for Next.js
131 lines (130 loc) • 4.64 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.init = init;
var _crypto = require("crypto");
var _logger = _interopRequireDefault(require("../utils/logger"));
var _errors = require("./errors");
var _providers = _interopRequireDefault(require("./lib/providers"));
var _utils = require("./lib/utils");
var cookie = _interopRequireWildcard(require("./lib/cookie"));
var jwt = _interopRequireWildcard(require("../jwt"));
var _defaultCallbacks = require("./lib/default-callbacks");
var _csrfToken = require("./lib/csrf-token");
var _callbackUrl = require("./lib/callback-url");
var _parseUrl = _interopRequireDefault(require("../utils/parse-url"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
async function init({
authOptions,
providerId,
action,
origin,
cookies: reqCookies,
callbackUrl: reqCallbackUrl,
csrfToken: reqCsrfToken,
isPost
}) {
var _authOptions$useSecur, _authOptions$events;
const url = (0, _parseUrl.default)(origin);
const secret = (0, _utils.createSecret)({
authOptions,
url
});
const {
providers,
provider
} = (0, _providers.default)({
providers: authOptions.providers,
url,
providerId
});
const maxAge = 30 * 24 * 60 * 60;
const options = {
debug: false,
pages: {},
theme: {
colorScheme: "auto",
logo: "",
brandColor: "",
buttonText: ""
},
...authOptions,
url,
action,
provider,
cookies: {
...cookie.defaultCookies((_authOptions$useSecur = authOptions.useSecureCookies) !== null && _authOptions$useSecur !== void 0 ? _authOptions$useSecur : url.base.startsWith("https://")),
...authOptions.cookies
},
secret,
providers,
session: {
strategy: authOptions.adapter ? "database" : "jwt",
maxAge,
updateAge: 24 * 60 * 60,
generateSessionToken: () => {
var _randomUUID;
return (_randomUUID = _crypto.randomUUID === null || _crypto.randomUUID === void 0 ? void 0 : (0, _crypto.randomUUID)()) !== null && _randomUUID !== void 0 ? _randomUUID : (0, _crypto.randomBytes)(32).toString("hex");
},
...authOptions.session
},
jwt: {
secret,
maxAge,
encode: jwt.encode,
decode: jwt.decode,
...authOptions.jwt
},
events: (0, _errors.eventsErrorHandler)((_authOptions$events = authOptions.events) !== null && _authOptions$events !== void 0 ? _authOptions$events : {}, _logger.default),
adapter: (0, _errors.adapterErrorHandler)(authOptions.adapter, _logger.default),
callbacks: {
..._defaultCallbacks.defaultCallbacks,
...authOptions.callbacks
},
logger: _logger.default,
callbackUrl: url.origin
};
const cookies = [];
const {
csrfToken,
cookie: csrfCookie,
csrfTokenVerified
} = (0, _csrfToken.createCSRFToken)({
options,
cookieValue: reqCookies === null || reqCookies === void 0 ? void 0 : reqCookies[options.cookies.csrfToken.name],
isPost,
bodyValue: reqCsrfToken
});
options.csrfToken = csrfToken;
options.csrfTokenVerified = csrfTokenVerified;
if (csrfCookie) {
cookies.push({
name: options.cookies.csrfToken.name,
value: csrfCookie,
options: options.cookies.csrfToken.options
});
}
const {
callbackUrl,
callbackUrlCookie
} = await (0, _callbackUrl.createCallbackUrl)({
options,
cookieValue: reqCookies === null || reqCookies === void 0 ? void 0 : reqCookies[options.cookies.callbackUrl.name],
paramValue: reqCallbackUrl
});
options.callbackUrl = callbackUrl;
if (callbackUrlCookie) {
cookies.push({
name: options.cookies.callbackUrl.name,
value: callbackUrlCookie,
options: options.cookies.callbackUrl.options
});
}
return {
options,
cookies
};
}