SoftwareBrothers / adminjs-nestjs

NestJS module to import admin the Nest way
MIT License
162 stars 37 forks source link

Unable to use nestjs and admin-bro in the same app due to body-parser and formidable. #5

Open blombergniklas opened 4 years ago

blombergniklas commented 4 years ago

Backend api built with nestjs serves call at /, endpoints looks like /users/me or /auth/login etc. Before adding AdminBro, call to /auth/login with post body of {"email": "<some-email>", "password": "<some-password>" } works fine. But when adding a module named Backoffice to my app.module.ts post body is not found when calling /auth/login and throws an exception in the controller. All calls to AdminBro works fine under /admin

@Module({
    imports: [
      AdminModule.createAdminAsync({
        imports: [
          TypeOrmModule.forRootAsync({
            imports: [SharedModule],
            inject: [ConfigService],
            useFactory: (configService: ConfigService) => configService.typeOrmConfig,
          }),
          UsersModule,
        ],
        inject: [UsersService, ConfigService],
        useFactory: (usersService: UsersService, configService: ConfigService) => {
          return {
            adminBroOptions: {
              rootPath: '/admin',
              resources: AdminBroResources,
            },
            auth: {
              authenticate: async (email, password) => {
                const user = await usersService.findOne({
                  email: email,
                  password: ConfigService.getPasswordsHash(password),
                });
                if (user) {
                  return { email: `${user.firstName}`, title: user.email.split('@')[0] };
                }
                return false;
              },
              cookiePassword: configService.get('COOKIE_SECRET'),
              cookieName: 'backoffice',
            },
          } as AdminModuleOptions;
        },
      }),
    ],
    // providers: [UsersService],
  })
  export class BackofficeModule {}

package.json dependencies used:

"@admin-bro/express": "^3.0.1", 
"@admin-bro/nestjs": "^1.0.0", 
"@admin-bro/typeorm": "^1.3.0", 
"express-formidable": "^1.2.0",

I've narrowed it down to ExpressLoader in this repo which moves jsonParser and urlencodeparser in layer stack below AdminBro router. Commenting this out breaks AdminBro but nestjs api works again.

hra4h03 commented 3 years ago

Same here. How about adding admin panel as microservice?

maxpeterson commented 3 years ago

Is this related to #7? I was able to overcome similar auth issues by using the approach outlined in https://github.com/SoftwareBrothers/admin-bro-nestjs/issues/7#issuecomment-743963341

blombergniklas commented 3 years ago

@maxpeterson its working for me also with your setup. Thanks!

Saidmurodjon commented 1 year ago

you must add app.use(express.json()); to index.js

Panchami-Hebbar commented 1 month ago

node:internal/modules/esm/resolve:257 admin-dashboard | throw new ERR_MODULE_NOT_FOUND( admin-dashboard | ^ admin-dashboard | admin-dashboard | Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/dist/app.module' imported from /app/dist/main.js admin-dashboard | at finalizeResolution (node:internal/modules/esm/resolve:257:11) admin-dashboard | at moduleResolve (node:internal/modules/esm/resolve:914:10) admin-dashboard | at defaultResolve (node:internal/modules/esm/resolve:1038:11) admin-dashboard | at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12) admin-dashboard | at ModuleLoader.resolve (node:internal/modules/esm/loader:525:25) admin-dashboard | at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38) admin-dashboard | at ModuleJob._link (node:internal/modules/esm/module_job:126:49) { admin-dashboard | code: 'ERR_MODULE_NOT_FOUND', admin-dashboard | url: 'file:///app/dist/app.module' admin-dashboard | }