You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.6 KiB

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAuthenticatedRouter = void 0;
const tslib_1 = require("tslib");
const logout_handler_1 = require("./authentication/logout.handler");
const buildRouter_1 = require("./buildRouter");
const login_handler_1 = require("./authentication/login.handler");
const protected_routes_handler_1 = require("./authentication/protected-routes.handler");
const cookie_1 = tslib_1.__importDefault(require("@fastify/cookie"));
const formbody_1 = tslib_1.__importDefault(require("@fastify/formbody"));
const session_1 = tslib_1.__importDefault(require("@fastify/session"));
/**
* @typedef {Function} Authenticate
* @memberof module:@adminjs/fastify
* @description
* function taking 2 arguments email and password
* @param {string} [email] email given in the form
* @param {string} [password] password given in the form
* @return {CurrentAdmin | null} returns current admin or null
*/
/**
* Builds the Express Router which is protected by a session auth
*
* Normally fastify-session holds session in memory, which is
* not optimized for production usage and, in development, it causes
* logging out after every page refresh (if you use nodemon).
* @static
* @memberof module:@adminjs/fastify
* @example
* const ADMIN = {
* email: 'test@example.com',
* password: 'password',
* }
*
* AdminJSFastify.buildAuthenticatedRouter(adminJs, {
* authenticate: async (email, password) => {
* if (ADMIN.password === password && ADMIN.email === email) {
* return ADMIN
* }
* return null
* },
* cookieName: 'adminjs',
* cookiePassword: 'somePassword',
* }, [router])
*/
const buildAuthenticatedRouter = async (admin, auth, fastifyApp, sessionOptions) => {
var _a, _b;
await fastifyApp.register(cookie_1.default, {
secret: auth.cookiePassword,
});
await fastifyApp.register(session_1.default, Object.assign({ secret: auth.cookiePassword, cookieName: (_a = auth.cookieName) !== null && _a !== void 0 ? _a : 'adminjs', cookie: (_b = sessionOptions === null || sessionOptions === void 0 ? void 0 : sessionOptions.cookie) !== null && _b !== void 0 ? _b : { secure: false } }, (sessionOptions !== null && sessionOptions !== void 0 ? sessionOptions : {})));
await fastifyApp.register(formbody_1.default);
await (0, buildRouter_1.buildRouter)(admin, fastifyApp);
(0, protected_routes_handler_1.withProtectedRoutesHandler)(fastifyApp, admin);
(0, login_handler_1.withLogin)(fastifyApp, admin, auth);
(0, logout_handler_1.withLogout)(fastifyApp, admin);
};
exports.buildAuthenticatedRouter = buildAuthenticatedRouter;