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.
		
		
		
		
		
			
		
			
				
					
					
						
							69 lines
						
					
					
						
							2.3 KiB
						
					
					
				
			
		
		
	
	
							69 lines
						
					
					
						
							2.3 KiB
						
					
					
				| import AdminJS from 'adminjs';
 | |
| import { withLogout } from './authentication/logout.handler';
 | |
| import { buildRouter } from './buildRouter';
 | |
| import { AuthenticationOptions } from './types';
 | |
| import { withLogin } from './authentication/login.handler';
 | |
| import { withProtectedRoutesHandler } from './authentication/protected-routes.handler';
 | |
| import { FastifyInstance } from 'fastify';
 | |
| import fastifyCookie from '@fastify/cookie';
 | |
| import fastifyFormBody from '@fastify/formbody';
 | |
| import FastifySessionPlugin from '@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])
 | |
|  */
 | |
| export const buildAuthenticatedRouter = async (
 | |
|   admin: AdminJS,
 | |
|   auth: AuthenticationOptions,
 | |
|   fastifyApp: FastifyInstance,
 | |
|   sessionOptions?: FastifySessionPlugin.Options
 | |
| ): Promise<void> => {
 | |
|   await fastifyApp.register(fastifyCookie, {
 | |
|     secret: auth.cookiePassword,
 | |
|   });
 | |
|   await fastifyApp.register(FastifySessionPlugin, {
 | |
|     secret: auth.cookiePassword,
 | |
|     cookieName: auth.cookieName ?? 'adminjs',
 | |
|     cookie: sessionOptions?.cookie ?? { secure: false },
 | |
|     ...(sessionOptions ?? {}),
 | |
|   });
 | |
|   await fastifyApp.register(fastifyFormBody);
 | |
| 
 | |
|   await buildRouter(admin, fastifyApp);
 | |
|   withProtectedRoutesHandler(fastifyApp, admin);
 | |
|   withLogin(fastifyApp, admin, auth);
 | |
|   withLogout(fastifyApp, admin);
 | |
| };
 |