diff --git a/packages/server/admin/README.md b/packages/server/admin/README.md index 0b4cd6b..b17f1a8 100644 --- a/packages/server/admin/README.md +++ b/packages/server/admin/README.md @@ -1 +1,41 @@ # admin + +Create AdminAuth, AdminController + +## Installation + +```bash +$ npm i @awesome-dev/server-admin +``` + +## Dependencies + +@awesome-dev/server-env +@awesome-dev/utils +@awesome-dev/server-aws-s3 +jsonwebtoken + +## Usage + +```bash +@AdminController(CustomEntity) +export class AdminCustomController extends BaseAdminController { + constructor(readonly service: EntityService) { + super(service); + } +} +``` +env +```bash +ADMIN_USER_ID="userId" +ADMIN_USER_PASSWORD="userPassword" +ADMIN_JWT_SECRET_KEY='JWTSecretKey' +``` + +## Support + +Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). + +## License + +Nest is [MIT licensed](LICENSE). diff --git a/packages/server/admin/src/config.ts b/packages/server/admin/src/config.ts index 7d3bcc4..f65d841 100644 --- a/packages/server/admin/src/config.ts +++ b/packages/server/admin/src/config.ts @@ -6,6 +6,9 @@ export interface AdminConfigOption { jwtSecretKey: string; } +/** + * AdminConfigOption의 key를 이용해 값을 가져옵니다. + */ @Injectable() export class AdminConfig { public constructor(private readonly option: AdminConfigOption) {} diff --git a/packages/server/admin/src/controllers/admin-auth.controller.ts b/packages/server/admin/src/controllers/admin-auth.controller.ts index 4e8ab36..8fddff8 100644 --- a/packages/server/admin/src/controllers/admin-auth.controller.ts +++ b/packages/server/admin/src/controllers/admin-auth.controller.ts @@ -3,6 +3,10 @@ import { Request } from 'express'; import { AdminAuthService } from '../providers'; +/** + * path는 '/admin/auth'로 설정된다. + * 어드민 로그인 API를 제공한다. + */ @Controller('/admin/auth') export class AdminAuthController { constructor(private readonly authService: AdminAuthService) {} diff --git a/packages/server/admin/src/controllers/admin-common.controller.ts b/packages/server/admin/src/controllers/admin-common.controller.ts index 6d90010..47bbc15 100644 --- a/packages/server/admin/src/controllers/admin-common.controller.ts +++ b/packages/server/admin/src/controllers/admin-common.controller.ts @@ -4,6 +4,10 @@ import { FilesInterceptor } from '@nestjs/platform-express'; import { AdminGuard } from '../providers'; +/** + * path는 '/admin/common'으로 설정된다. + * 파일 업로드 API를 제공한다. + */ @UseGuards(AdminGuard) @Controller('/admin/common') export class AdminCommonController { diff --git a/packages/server/admin/src/controllers/base-admin.controller.ts b/packages/server/admin/src/controllers/base-admin.controller.ts index 4122a1e..1751c07 100644 --- a/packages/server/admin/src/controllers/base-admin.controller.ts +++ b/packages/server/admin/src/controllers/base-admin.controller.ts @@ -8,6 +8,11 @@ import { import { PartialDeep } from '@awesome-dev/typings'; import { Body, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from '@nestjs/common'; +/** + * path는 '/admin/${toKebabCase(entityName)}s'으로 설정된다. + * AdminController 데코레이터를 사용한 컨트롤러는 이 클래스를 상속받아야 한다. + * AdminController 데코레이터를 사용한 컨트롤러는 CRUD와 entity의 개수를 가져오는 API를 자동으로 생성한다. + */ export abstract class BaseAdminController { constructor(protected readonly service: BaseService) {} diff --git a/packages/server/admin/src/module.ts b/packages/server/admin/src/module.ts index dccd76d..4dcbae9 100644 --- a/packages/server/admin/src/module.ts +++ b/packages/server/admin/src/module.ts @@ -9,12 +9,18 @@ import { AdminConfig } from './config'; @Module({}) export class AwesomeAdminModule { static forRoot(): DynamicModule { + /** + * process.env의 환경변수를 가져옵니다. + */ const config = loadEnv([ 'ADMIN_USER_ID', 'ADMIN_USER_PASSWORD', 'ADMIN_JWT_SECRET_KEY', ] as const); + /** + * AdminConfig 인스턴스를 생성합니다. + */ const ConfigProvider: Provider = { provide: AdminConfig, useFactory: () => diff --git a/packages/server/admin/src/providers/admin-auth.service.ts b/packages/server/admin/src/providers/admin-auth.service.ts index fb8b7fd..b82a7d6 100644 --- a/packages/server/admin/src/providers/admin-auth.service.ts +++ b/packages/server/admin/src/providers/admin-auth.service.ts @@ -3,10 +3,17 @@ import * as jwt from 'jsonwebtoken'; import { AdminConfig } from '../config'; +/** + * 어드민 로그인 서비스 + */ @Injectable() export class AdminAuthService { constructor(private readonly config: AdminConfig) {} + /** + * 로그인을 수행합니다. + * env에 설정된 userId와 userPassword를 비교하여 일치하면 토큰을 발급합니다. + */ signin(code: string, password: string) { if ( code !== this.config.getOption('userId') || diff --git a/packages/server/admin/src/providers/admin.guard.ts b/packages/server/admin/src/providers/admin.guard.ts index 1b5950a..47e9bb2 100644 --- a/packages/server/admin/src/providers/admin.guard.ts +++ b/packages/server/admin/src/providers/admin.guard.ts @@ -4,6 +4,9 @@ import { Observable } from 'rxjs'; import { AdminAuthService } from './admin-auth.service'; +/** + * 어드민 권한이 있는지 확인합니다. + */ @Injectable() export class AdminGuard implements CanActivate { constructor(private readonly authService: AdminAuthService) {}