From fe1da3c5a340348ecddfc5d285837a603f50cee5 Mon Sep 17 00:00:00 2001 From: mertmit Date: Tue, 21 May 2024 23:03:19 +0300 Subject: [PATCH] refactor: separate auth module --- packages/nocodb/src/app.module.ts | 12 ++----- .../auth/auth.controller.ts | 13 +++++--- .../nocodb/src/modules/auth/auth.module.ts | 31 +++++++++++++++++++ .../auth}/auth.service.spec.ts | 0 .../auth}/auth.service.ts | 0 .../auth/ui/auth/emailVerify.ts | 0 .../auth/ui/auth/resetPassword.ts | 0 .../auth/ui/emailTemplates/forgotPassword.ts | 0 .../auth/ui/emailTemplates/invite.ts | 0 .../auth/ui/emailTemplates/verify.ts | 0 packages/nocodb/src/modules/noco.module.ts | 16 ++-------- .../src/services/users/users.service.ts | 7 ++--- .../nocodb/src/strategies/local.strategy.ts | 2 +- 13 files changed, 48 insertions(+), 33 deletions(-) rename packages/nocodb/src/{controllers => modules}/auth/auth.controller.ts (96%) create mode 100644 packages/nocodb/src/modules/auth/auth.module.ts rename packages/nocodb/src/{services => modules/auth}/auth.service.spec.ts (100%) rename packages/nocodb/src/{services => modules/auth}/auth.service.ts (100%) rename packages/nocodb/src/{controllers => modules}/auth/ui/auth/emailVerify.ts (100%) rename packages/nocodb/src/{controllers => modules}/auth/ui/auth/resetPassword.ts (100%) rename packages/nocodb/src/{controllers => modules}/auth/ui/emailTemplates/forgotPassword.ts (100%) rename packages/nocodb/src/{controllers => modules}/auth/ui/emailTemplates/invite.ts (100%) rename packages/nocodb/src/{controllers => modules}/auth/ui/emailTemplates/verify.ts (100%) diff --git a/packages/nocodb/src/app.module.ts b/packages/nocodb/src/app.module.ts index 1e287ace11..8728a231f5 100644 --- a/packages/nocodb/src/app.module.ts +++ b/packages/nocodb/src/app.module.ts @@ -6,24 +6,21 @@ import { EventEmitterModule as NestJsEventEmitter } from '@nestjs/event-emitter' import { SentryModule } from '@ntegral/nestjs-sentry'; import type { MiddlewareConsumer } from '@nestjs/common'; import { NocoModule } from '~/modules/noco.module'; +import { AuthModule } from '~/modules/auth/auth.module'; import { GlobalExceptionFilter } from '~/filters/global-exception/global-exception.filter'; import { GlobalMiddleware } from '~/middlewares/global/global.middleware'; import { GuiMiddleware } from '~/middlewares/gui/gui.middleware'; import { EventEmitterModule } from '~/modules/event-emitter/event-emitter.module'; -import { AuthService } from '~/services/auth.service'; -import { LocalStrategy } from '~/strategies/local.strategy'; -import { AuthTokenStrategy } from '~/strategies/authtoken.strategy/authtoken.strategy'; -import { BaseViewStrategy } from '~/strategies/base-view.strategy/base-view.strategy'; import { JobsModule } from '~/modules/jobs/jobs.module'; import appConfig from '~/app.config'; import { ExtractIdsMiddleware } from '~/middlewares/extract-ids/extract-ids.middleware'; -import { BasicStrategy } from '~/strategies/basic.strategy/basic.strategy'; import { packageInfo } from '~/utils/packageVersion'; export const ceModuleConfig = { imports: [ + AuthModule, NocoModule, EventEmitterModule, JobsModule, @@ -45,7 +42,6 @@ export const ceModuleConfig = { : []), ], providers: [ - AuthService, { provide: APP_FILTER, useClass: GlobalExceptionFilter, @@ -54,10 +50,6 @@ export const ceModuleConfig = { provide: APP_GUARD, useClass: ExtractIdsMiddleware, }, - LocalStrategy, - AuthTokenStrategy, - BaseViewStrategy, - BasicStrategy, ], }; diff --git a/packages/nocodb/src/controllers/auth/auth.controller.ts b/packages/nocodb/src/modules/auth/auth.controller.ts similarity index 96% rename from packages/nocodb/src/controllers/auth/auth.controller.ts rename to packages/nocodb/src/modules/auth/auth.controller.ts index 3ab670fad1..7fa29d475a 100644 --- a/packages/nocodb/src/controllers/auth/auth.controller.ts +++ b/packages/nocodb/src/modules/auth/auth.controller.ts @@ -232,11 +232,14 @@ export class AuthController { ): Promise { try { res.send( - ejs.render((await import('./ui/auth/resetPassword')).default, { - ncPublicUrl: process.env.NC_PUBLIC_URL || '', - token: JSON.stringify(tokenId), - baseUrl: `/`, - }), + ejs.render( + (await import('~/modules/auth/ui/auth/resetPassword')).default, + { + ncPublicUrl: process.env.NC_PUBLIC_URL || '', + token: JSON.stringify(tokenId), + baseUrl: `/`, + }, + ), ); } catch (e) { return res.status(400).json({ msg: e.message }); diff --git a/packages/nocodb/src/modules/auth/auth.module.ts b/packages/nocodb/src/modules/auth/auth.module.ts new file mode 100644 index 0000000000..11f71f9d8d --- /dev/null +++ b/packages/nocodb/src/modules/auth/auth.module.ts @@ -0,0 +1,31 @@ +import { Module } from '@nestjs/common'; +import { PassportModule } from '@nestjs/passport'; + +import { NocoModule } from '~/modules/noco.module'; + +import { BasicStrategy } from '~/strategies/basic.strategy/basic.strategy'; +import { LocalStrategy } from '~/strategies/local.strategy'; +import { AuthTokenStrategy } from '~/strategies/authtoken.strategy/authtoken.strategy'; +import { BaseViewStrategy } from '~/strategies/base-view.strategy/base-view.strategy'; +import { GoogleStrategyProvider } from '~/strategies/google.strategy/google.strategy'; +import { AuthService } from '~/modules/auth/auth.service'; +import { AuthController } from '~/modules/auth/auth.controller'; + +export const authModuleMetadata = { + imports: [PassportModule, NocoModule], + controllers: [ + ...(process.env.NC_WORKER_CONTAINER !== 'true' ? [AuthController] : []), + ], + providers: [ + AuthService, + LocalStrategy, + AuthTokenStrategy, + BaseViewStrategy, + BasicStrategy, + GoogleStrategyProvider, + ], + exports: [], +}; + +@Module(authModuleMetadata) +export class AuthModule {} diff --git a/packages/nocodb/src/services/auth.service.spec.ts b/packages/nocodb/src/modules/auth/auth.service.spec.ts similarity index 100% rename from packages/nocodb/src/services/auth.service.spec.ts rename to packages/nocodb/src/modules/auth/auth.service.spec.ts diff --git a/packages/nocodb/src/services/auth.service.ts b/packages/nocodb/src/modules/auth/auth.service.ts similarity index 100% rename from packages/nocodb/src/services/auth.service.ts rename to packages/nocodb/src/modules/auth/auth.service.ts diff --git a/packages/nocodb/src/controllers/auth/ui/auth/emailVerify.ts b/packages/nocodb/src/modules/auth/ui/auth/emailVerify.ts similarity index 100% rename from packages/nocodb/src/controllers/auth/ui/auth/emailVerify.ts rename to packages/nocodb/src/modules/auth/ui/auth/emailVerify.ts diff --git a/packages/nocodb/src/controllers/auth/ui/auth/resetPassword.ts b/packages/nocodb/src/modules/auth/ui/auth/resetPassword.ts similarity index 100% rename from packages/nocodb/src/controllers/auth/ui/auth/resetPassword.ts rename to packages/nocodb/src/modules/auth/ui/auth/resetPassword.ts diff --git a/packages/nocodb/src/controllers/auth/ui/emailTemplates/forgotPassword.ts b/packages/nocodb/src/modules/auth/ui/emailTemplates/forgotPassword.ts similarity index 100% rename from packages/nocodb/src/controllers/auth/ui/emailTemplates/forgotPassword.ts rename to packages/nocodb/src/modules/auth/ui/emailTemplates/forgotPassword.ts diff --git a/packages/nocodb/src/controllers/auth/ui/emailTemplates/invite.ts b/packages/nocodb/src/modules/auth/ui/emailTemplates/invite.ts similarity index 100% rename from packages/nocodb/src/controllers/auth/ui/emailTemplates/invite.ts rename to packages/nocodb/src/modules/auth/ui/emailTemplates/invite.ts diff --git a/packages/nocodb/src/controllers/auth/ui/emailTemplates/verify.ts b/packages/nocodb/src/modules/auth/ui/emailTemplates/verify.ts similarity index 100% rename from packages/nocodb/src/controllers/auth/ui/emailTemplates/verify.ts rename to packages/nocodb/src/modules/auth/ui/emailTemplates/verify.ts diff --git a/packages/nocodb/src/modules/noco.module.ts b/packages/nocodb/src/modules/noco.module.ts index 59b0ef2578..080e88e49d 100644 --- a/packages/nocodb/src/modules/noco.module.ts +++ b/packages/nocodb/src/modules/noco.module.ts @@ -1,6 +1,5 @@ import multer from 'multer'; import { Module } from '@nestjs/common'; -import { PassportModule } from '@nestjs/passport'; import { MulterModule } from '@nestjs/platform-express'; /* Modules */ @@ -24,6 +23,7 @@ import { UsersService } from '~/services/users/users.service'; /* Metas */ import { NC_ATTACHMENT_FIELD_SIZE } from '~/constants'; +import { MetaService } from '~/meta/meta.service'; import { ApiDocsController } from '~/controllers/api-docs/api-docs.controller'; import { ApiTokensController } from '~/controllers/api-tokens.controller'; import { AttachmentsController } from '~/controllers/attachments.controller'; @@ -120,15 +120,10 @@ import { PublicDatasService } from '~/services/public-datas.service'; import { CalendarDatasController } from '~/controllers/calendars-datas.controller'; import { CalendarDatasService } from '~/services/calendar-datas.service'; -/* Auth */ -import { GoogleStrategyProvider } from '~/strategies/google.strategy/google.strategy'; -import { AuthController } from '~/controllers/auth/auth.controller'; - export const nocoModuleMetadata = { imports: [ EventEmitterModule, JobsModule, - PassportModule, MulterModule.register({ storage: multer.diskStorage({}), limits: { @@ -193,16 +188,12 @@ export const nocoModuleMetadata = { OldDatasController, PublicDatasController, PublicDatasExportController, - - /* Auth */ - AuthController, ] : []), ], providers: [ /* Generic */ InitMetaServiceProvider, - AppHooksService, JwtStrategyProvider, GlobalGuard, SocketGateway, @@ -263,12 +254,10 @@ export const nocoModuleMetadata = { OldDatasService, PublicDatasService, PublicDatasExportService, - - /* Auth */ - GoogleStrategyProvider, ], exports: [ /* Generic */ + AppHooksService, TelemetryService, HookHandlerService, JwtStrategy, @@ -277,6 +266,7 @@ export const nocoModuleMetadata = { UsersService, /* Metas */ + MetaService, TablesService, ColumnsService, FiltersService, diff --git a/packages/nocodb/src/services/users/users.service.ts b/packages/nocodb/src/services/users/users.service.ts index 9010192250..e2afbd33bd 100644 --- a/packages/nocodb/src/services/users/users.service.ts +++ b/packages/nocodb/src/services/users/users.service.ts @@ -229,7 +229,7 @@ export class UsersService { }); try { const template = ( - await import('~/controllers/auth/ui/emailTemplates/forgotPassword') + await import('~/modules/auth/ui/emailTemplates/forgotPassword') ).default; await NcPluginMgrv2.emailAdapter().then((adapter) => adapter.mailSend({ @@ -475,9 +475,8 @@ export class UsersService { user = await User.getByEmail(email); try { - const template = ( - await import('~/controllers/auth/ui/emailTemplates/verify') - ).default; + const template = (await import('~/modules/auth/ui/emailTemplates/verify')) + .default; await ( await NcPluginMgrv2.emailAdapter() ).mailSend({ diff --git a/packages/nocodb/src/strategies/local.strategy.ts b/packages/nocodb/src/strategies/local.strategy.ts index 0ac466c1b8..3446a4acde 100644 --- a/packages/nocodb/src/strategies/local.strategy.ts +++ b/packages/nocodb/src/strategies/local.strategy.ts @@ -4,7 +4,7 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { extractRolesObj } from 'nocodb-sdk'; import type { AppConfig } from '~/interface/config'; -import { AuthService } from '~/services/auth.service'; +import { AuthService } from '~/modules/auth/auth.service'; import { NcError } from '~/helpers/catchError'; @Injectable()