Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: server-admin 리드미, jsdoc 작성 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/server/admin/README.md
Original file line number Diff line number Diff line change
@@ -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<CustomEntity> {
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).
3 changes: 3 additions & 0 deletions packages/server/admin/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export interface AdminConfigOption {
jwtSecretKey: string;
}

/**
* AdminConfigOption의 key를 이용해 값을 가져옵니다.
*/
@Injectable()
export class AdminConfig {
public constructor(private readonly option: AdminConfigOption) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity extends BaseIdEntity> {
constructor(protected readonly service: BaseService<Entity>) {}

Expand Down
6 changes: 6 additions & 0 deletions packages/server/admin/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () =>
Expand Down
7 changes: 7 additions & 0 deletions packages/server/admin/src/providers/admin-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') ||
Expand Down
3 changes: 3 additions & 0 deletions packages/server/admin/src/providers/admin.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down