Nest
@adminjs/nestjs
$ yarn add adminjs @adminjs/nestjs
As of version
5.x.x
of @adminjs/nestjs
, the plugin only supports Nest servers that use Express. Fastify Nest servers are not currently supported.If you are starting a new project, install
nest-cli
and bootstrap the project:$ npm i -g nest-cli
$ nest new
@adminjs/nestjs
uses @adminjs/express
to set up AdminJS, because of this you have to additionally install it's dependencies:$ yarn add @adminjs/express express-session express-formidable
Next, add Nest plugin code into
imports
of your AppModule
:app.module.ts
import { Module } from '@nestjs/common'
import { AdminModule } from '@adminjs/nestjs'
import { AppController } from './app.controller'
import { AppService } from './app.service'
const DEFAULT_ADMIN = {
email: '[email protected]',
password: 'password',
}
const authenticate = async (email: string, password: string) => {
if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
return Promise.resolve(DEFAULT_ADMIN)
}
return null
}
@Module({
imports: [
AdminModule.createAdminAsync({
useFactory: () => ({
adminJsOptions: {
rootPath: '/admin',
resources: [],
},
auth: {
authenticate,
cookieName: 'adminjs',
cookiePassword: 'secret'
},
sessionOptions: {
resave: true,
saveUninitialized: true,
secret: 'secret'
},
}),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Now you should be able to start your AdminJS application:
$ nest start
AdminJS panel will be available under
http://localhost:3000/admin
if you have followed this example thoroughly.If you don't want your admin panel to require authentication, simply follow the example above, but remove
auth
and sessionOptions
configuration, for example: AdminModule.createAdminAsync({
useFactory: () => ({
adminJsOptions: {
rootPath: '/admin',
resources: [],
},
}),
}),
Last modified 6mo ago