Compare commits

...

17 Commits

Author SHA1 Message Date
91e4f987ea issue #
All checks were successful
Test CI / build (push) Successful in 1m17s
- 서버 호스트 0.0.0.0 설정
2025-12-06 00:20:05 +09:00
1611026688 issue #39
All checks were successful
Test CI / build (push) Successful in 1m17s
- 로그인 기능 오류 보완
2025-12-02 22:31:36 +09:00
e4048843e9 issue #41
- 비밀번호 초기화 로직 구현 및 테스트 완료
2025-12-02 22:31:10 +09:00
geonhee-min
0f0717fc79 issue #41
All checks were successful
Test CI / build (push) Successful in 1m21s
- 비밀번호 초기화 로직 1차 구현(테스트 필요)
2025-12-02 12:35:45 +09:00
58d092536e issue #39
All checks were successful
Test CI / build (push) Successful in 1m29s
- Access 토큰 만료 시 Refresh 토큰으로 Access 토큰 갱신 로직 구현 중
2025-12-01 22:36:01 +09:00
geonhee-min
56cee12c81 issue #39
All checks were successful
Test CI / build (push) Successful in 1m28s
- Access/Refresh 토큰 검증 및 허가 구현 중
2025-12-01 16:35:22 +09:00
43868489e0 issue #
All checks were successful
Test CI / build (push) Successful in 1m25s
- gitea-ci 파일 node_modules 캐싱 수정
2025-11-30 19:03:32 +09:00
be65742caa issue #
Some checks failed
Test CI / build (push) Failing after 23s
- gitea-ci 파일 node_modules 캐싱 수정
2025-11-30 18:58:27 +09:00
ab99d23de3 issue #39
Some checks failed
Test CI / build (push) Failing after 26s
- 로그인 이후 access/refresh token 생성 및 반환 로직 구현
- gitea 웹훅 테스트
2025-11-30 18:30:16 +09:00
5c79aa18f4 issue #39
Some checks failed
Test CI / build (push) Failing after 28s
- 로그인 이후 access/refresh token 생성 및 반환 로직 구현
2025-11-30 18:19:39 +09:00
geonhee-min
810b4c1fb0 issue # server pc change
All checks were successful
Test CI / build (push) Successful in 1m29s
2025-11-28 13:10:59 +09:00
geonhee-min
3bea9bca11 issue # server pc change
Some checks failed
Test CI / build (push) Failing after 3s
2025-11-28 13:08:11 +09:00
geonhee-min
115c5e61f0 issue # server pc change
Some checks failed
Test CI / build (push) Failing after 1m50s
2025-11-28 12:23:16 +09:00
geonhee-min
4365f29e27 issue # server pc change
Some checks failed
Test CI / build (push) Has been cancelled
2025-11-28 12:22:54 +09:00
geonhee-min
f71415d7c0 issue # caching and reload test
All checks were successful
Test CI / build (push) Successful in 3m9s
2025-11-26 16:56:54 +09:00
geonhee-min
ca1e6071cf issue # caching and reload test
All checks were successful
Test CI / build (push) Successful in 3m14s
2025-11-26 16:49:09 +09:00
geonhee-min
4d77d2689b issue # gitea ci cache test
All checks were successful
Test CI / build (push) Successful in 4m26s
2025-11-26 16:43:52 +09:00
44 changed files with 1384 additions and 102 deletions

View File

@@ -1,6 +1,3 @@
# Nestjs 서버 포트
PORT=3000
# Gmail SMTP 설정 # Gmail SMTP 설정
GMAIL_USER=bkd.scheduler@gmail.com GMAIL_USER=bkd.scheduler@gmail.com
GMAIL_PASS= # 앱 비밀번호 또는 OAuth2 토큰 GMAIL_PASS= # 앱 비밀번호 또는 OAuth2 토큰
@@ -13,3 +10,5 @@ SMTP_AUTH=true
SMTP_STARTTLS_ENABLE=true SMTP_STARTTLS_ENABLE=true
SMTP_STARTTLS_REQUIRED=true SMTP_STARTTLS_REQUIRED=true
SMTP_AUTH_MECHANISMS=XOAUTH2 SMTP_AUTH_MECHANISMS=XOAUTH2
JWT_SECRET=96612b08364bbd9f275f29f86d39c18225e3cb3f31551434d5a84a88f5b01e627b5aafac902e0769bda4f1574b2f84ffb26e659b1a672182015a180c086cb911

View File

@@ -1,3 +1,5 @@
PORT=8088
# PostgreSQL 설정 # PostgreSQL 설정
PGHOST=bkdhome.p-e.kr PGHOST=bkdhome.p-e.kr
PGPORT=15454 PGPORT=15454
@@ -8,5 +10,5 @@ PG_DATABASE_URL=postgres://baekyangdan:qwas745478!@bkdhome.p-e.kr:15454/schedule
# Redis 설정 # Redis 설정
RD_HOST=bkdhome.p-e.kr RD_HOST=bkdhome.p-e.kr
RD_PORT=6779 RD_PORT=16779
RD_URL=redis://bkdhome.p-e.kr:16779 RD_URL=redis://bkdhome.p-e.kr:16779

View File

@@ -1,12 +1,12 @@
HOST=0.0.0.0
PORT=3000
# PostgreSQL 설정 # PostgreSQL 설정
PGHOST=bkdhome.p-e.kr
PGPORT=15454
PGDATABASE=scheduler
PGUSER=baekyangdan PGUSER=baekyangdan
PGPASSWORD=qwas745478! PGPASSWORD=qwas745478!
PG_DATABASE_URL=postgres://192.168.219.107:5454/scheduler PG_DATABASE_URL=postgres://192.168.219.103:5454/scheduler
# Redis 설정 # Redis 설정
RD_HOST=bkdhome.p-e.kr RD_HOST=192.168.219.103
RD_PORT=6779 RD_PORT=6779
RD_URL=redis://192.168.219.107:6779 RD_URL=redis://192.168.219.103:6779

View File

@@ -1,3 +1,5 @@
PORT=3000
# PostgreSQL 설정 # PostgreSQL 설정
PGHOST=db PGHOST=db
PGPORT=5454 PGPORT=5454

View File

@@ -6,7 +6,7 @@ on:
jobs: jobs:
build: build:
runs-on: runner runs-on: rpi5
env: env:
DOCKER_VOLUME: ${{ vars.DOCKER_VOLUME }} DOCKER_VOLUME: ${{ vars.DOCKER_VOLUME }}
@@ -17,21 +17,37 @@ jobs:
- name: Check PWD - name: Check PWD
run: | run: |
echo $DOCKER_VOLUME echo "Docker volume: $DOCKER_VOLUME"
echo $PWD echo "PWD: $PWD"
ls . ls -lRa ./.yarn
- name: Restore node_modules - name: Validate Node and Yarn Environment
id: cache-node run: |
if ! command -v node &> /dev/null
then
echo "Error: Node.js not found"
exit 1
fi
echo "Node.js version: $(node -v)"
if ! command -v yarn &> /dev/null
then
echo "Error: Yarn.js not found"
exit 1
fi
echo "yarn version: $(yarn -v)"
- name: Restore Yarn cache
uses: actions/cache@v4 uses: actions/cache@v4
with: with:
path: node_modules path: |
key: ${{ runner.os }}-yarn-v1-${{ hashFiles('**/yarn.lock') }} .yarn/cache
restore-keys: | .yarn/unplugged
${{ runner.os }}-yarn-v1- .yarn/install-state.gz
.pnp.cjs
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock','package.json') }}
- name: Install Dependencies with yarn - name: Install Dependencies with yarn
if: steps.cache-node.outputs.cache-hit != 'true'
run: | run: |
yarn install --immutable yarn install --immutable
ls . ls .
@@ -46,4 +62,4 @@ jobs:
cp -r dist $DOCKER_VOLUME/scheduler/back/ cp -r dist $DOCKER_VOLUME/scheduler/back/
cp -r node_modules $DOCKER_VOLUME/scheduler/back/ cp -r node_modules $DOCKER_VOLUME/scheduler/back/
ls $DOCKER_VOLUME/scheduler/back ls $DOCKER_VOLUME/scheduler/back
docker exec -it scheduler_back pm2 reload scheduler_back docker exec -it scheduler_back pm2 reload scheduler-back

1
.gitignore vendored
View File

@@ -12,7 +12,6 @@ package-lock.json
!.yarn/releases !.yarn/releases
!.yarn/sdks !.yarn/sdks
!.yarn/versions !.yarn/versions
.pnp.* .pnp.*
# .pnp.loader.mjs # .pnp.loader.mjs
# .yarn/install-state.gz # .yarn/install-state.gz

View File

@@ -1,8 +1,5 @@
{ {
"$schema": "https://json.schemastore.org/nest-cli", "$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics", "collection": "@nestjs/schematics",
"sourceRoot": "src", "sourceRoot": "src"
"compilerOptions": {
"deleteOutDir": true
}
} }

View File

@@ -6,13 +6,15 @@
"private": true, "private": true,
"license": "UNLICENSED", "license": "UNLICENSED",
"scripts": { "scripts": {
"build": "NODE_ENV=prod nest build", "build": "cross-env NODE_ENV=prod nest build",
"build:local": "cross-env NODE_ENV=local nest build",
"build:dev": "cross-env NODE_ENV=dev nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start", "start": "nest start",
"start:local": "NODE_ENV=local nest start --watch", "start:local": "cross-env NODE_ENV=local nest start --watch",
"start:dev": "NODE_ENV=dev nest start --watch", "start:dev": "cross-env NODE_ENV=dev nest start --watch",
"start:debug": "nest start --debug --watch", "start:debug": "nest start --debug --watch",
"start:prod": "NODE_ENV=prod node dist/main", "start:prod": "cross-env NODE_ENV=prod node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest", "test": "jest",
"test:watch": "jest --watch", "test:watch": "jest --watch",
@@ -26,14 +28,21 @@
"@nestjs/common": "^11.0.1", "@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.2", "@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.0.1", "@nestjs/core": "^11.0.1",
"@nestjs/jwt": "^11.0.1",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^11.0.1", "@nestjs/platform-express": "^11.0.1",
"@nestjs/platform-fastify": "^11.1.9",
"bcrypt": "^6.0.0", "bcrypt": "^6.0.0",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"drizzle-kit": "^0.31.7", "drizzle-kit": "^0.31.7",
"drizzle-orm": "^0.44.7", "drizzle-orm": "^0.44.7",
"fastify": "^5.6.2",
"fastify-cors": "^6.1.0",
"googleapis": "^166.0.0", "googleapis": "^166.0.0",
"ioredis": "^5.8.2", "ioredis": "^5.8.2",
"nodemailer": "^7.0.10", "nodemailer": "^7.0.10",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"pg": "^8.16.3", "pg": "^8.16.3",
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1" "rxjs": "^7.8.1"
@@ -50,8 +59,11 @@
"@types/jest": "^30.0.0", "@types/jest": "^30.0.0",
"@types/node": "^22.10.7", "@types/node": "^22.10.7",
"@types/nodemailer": "^7.0.4", "@types/nodemailer": "^7.0.4",
"@types/passport": "^0",
"@types/passport-jwt": "^4.0.1",
"@types/pg": "^8.15.6", "@types/pg": "^8.15.6",
"@types/supertest": "^6.0.2", "@types/supertest": "^6.0.2",
"cross-env": "^10.1.0",
"eslint": "^9.18.0", "eslint": "^9.18.0",
"eslint-config-prettier": "^10.0.1", "eslint-config-prettier": "^10.0.1",
"eslint-plugin-prettier": "^5.2.2", "eslint-plugin-prettier": "^5.2.2",

View File

@@ -0,0 +1,5 @@
import { SetMetadata } from "@nestjs/common";
export const IS_PUBLIC_KEY = 'isPublic345827';
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);

View File

@@ -0,0 +1,39 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus
} from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<FastifyReply>();
const request = ctx.getRequest<FastifyRequest>();
let status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
let message =
exception instanceof HttpException
? exception.getResponse()
: 'Internal server error';
if (typeof message === 'object' && (message as any).message) {
message = (message as any).message;
}
response.status(status).send({
success: false,
timestamp: new Date().toISOString(),
path: request.url,
statusCode: status,
error: message
});
}
}

View File

@@ -1,13 +1,14 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import dotenv from 'dotenv';
@Module({ @Module({
imports: [ imports: [
ConfigModule.forRoot({ ConfigModule.forRoot({
isGlobal: true, isGlobal: true,
envFilePath: [ envFilePath: [
'.env.common', `.env.${process.env.NODE_ENV}`,
`.env.${process.env.NODE_ENV}` '.env.common'
] ]
}) })
] ]

42
src/const/HttpResponse.ts Normal file
View File

@@ -0,0 +1,42 @@
export const HttpResponse: Record<string, {code: number, title: string, message: string}> = {
"ACCESS_TOKEN_EXPIRED": {
code: 401,
title: "ACCESS_TOKEN_EXPIRED",
message: "ACCESS TOKEN EXPIRED"
},
"INVALID_TOKEN": {
code: 401,
title: "INVALID_TOKEN",
message: "INVALID TOKEN"
},
"REFRESH_TOKEN_EXPIRED": {
code: 401,
title: "REFRESH_TOKEN_EXPIRED",
message: "REFRESH TOKEN EXPIRED"
},
"UNAUTHORIZED": {
code: 401,
title: "UNAUTHORIZED",
message: "UNAUTHORIZED"
},
"OK": {
code: 200,
title: "OK",
message: "OK"
},
"CREATED": {
code: 201,
title: "CREATED",
message: "CREATED"
},
"BAD_REQUEST": {
code: 400,
title: "BAD_REQUEST",
message: "BAD REQUEST"
},
"INTERNAL_SERVER_ERROR": {
code: 500,
title: "INTERNAL_SERVER_ERROR",
message: "INTERNAL SERVER ERROR"
}
} as const;

View File

@@ -1,18 +1,23 @@
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import dotenv from 'dotenv'; import {
FastifyAdapter,
dotenv.config(); NestFastifyApplication
} from '@nestjs/platform-fastify';
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
app.enableCors({ app.enableCors({
origin: (origin, callback) => { origin: (origin, callback) => {
// origin이 없는 경우(local file, curl 등) 허용 // origin이 없는 경우(local file, curl 등) 허용
if (!origin) return callback(null, true); if (!origin) return callback(null, true);
// 특정 도메인만 막고 싶은 경우 whitelist 가능 // 특정 도메인만 막고 싶은 경우 whitelist 가능
const whitelist = ["http://localhost:5173", "https://scheduler.bkdhome.p-e.kr"]; const whitelist = ["http://localhost:5173", "http://192.168.219.105:5185", "https://scheduler.bkdhome.p-e.kr"];
if (whitelist.includes(origin)) { if (whitelist.includes(origin)) {
return callback(null, true); return callback(null, true);
} }
@@ -24,8 +29,8 @@ async function bootstrap() {
}); });
app.enableShutdownHooks(); app.enableShutdownHooks();
app.useGlobalFilters(new AllExceptionsFilter());
await app.listen(process.env.PORT ?? 3000); await app.listen(process.env.PORT ?? 3000, '0.0.0.0', () => { process.env.NODE_ENV !== 'prod' && console.log(`servier is running on ${process.env.PORT}`) });
} }
bootstrap(); bootstrap();

View File

@@ -0,0 +1,24 @@
import { forwardRef, Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AccountModule } from 'src/modules/account/account.module';
import { JwtAccessStrategy } from './strategy/access-token.strategy';
import { JwtRefreshStrategy } from './strategy/refresh-token.strategy';
@Module({
imports: [
ConfigModule,
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
secret: config.get<string>('JWT_SECRET')!,
signOptions: { expiresIn: '1h' }
})
}),
forwardRef(() => AccountModule)
],
providers: [AuthService, JwtAccessStrategy, JwtRefreshStrategy],
exports: [AuthService]
})
export class AuthModule{}

View File

@@ -0,0 +1,23 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
@Injectable()
export class AuthService {
constructor(private readonly jwtService: JwtService) {}
generateTokens(payload: any) {
const accessToken = this.jwtService.sign(payload, { expiresIn: '5s' });
const refreshToken = this.jwtService.sign({id: payload.id}, { expiresIn: '7d' });
return { accessToken, refreshToken };
}
refreshTokens(refreshToken: string) {
try {
const payload = this.jwtService.verify(refreshToken);
return this.generateTokens(payload);
} catch (e) {
throw new UnauthorizedException('Invalid Refresh Token');
}
}
}

View File

@@ -0,0 +1,45 @@
import { ExecutionContext, Injectable, UnauthorizedException } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { TokenExpiredError } from "@nestjs/jwt";
import { AuthGuard } from "@nestjs/passport";
import { IS_PUBLIC_KEY } from "src/common/decorators/public.decorator";
@Injectable()
export class JwtAccessAuthGuard extends AuthGuard('access-token') {
constructor(private reflector: Reflector) {
super();
}
canActivate(context: ExecutionContext) {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass()
]);
if (isPublic) {
return true;
}
return super.canActivate(context);
}
handleRequest(err: any, user:any, info:any) {
if (info instanceof TokenExpiredError) {
throw new UnauthorizedException({
statusCode: 401,
message: 'Access Token Expired',
code: 'AccessTokenExpired'
});
}
if (err || !user) {
throw new UnauthorizedException({
statusCode: 401,
message: 'Invalid Token',
code: 'InvalidToken'
});
}
return user;
}
}

View File

@@ -0,0 +1,45 @@
import { ExecutionContext, Injectable, UnauthorizedException } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { TokenExpiredError } from "@nestjs/jwt";
import { AuthGuard } from "@nestjs/passport";
import { IS_PUBLIC_KEY } from "src/common/decorators/public.decorator";
@Injectable()
export class JwtRefreshAuthGuard extends AuthGuard('refresh-token') {
constructor(private reflector: Reflector) {
super();
}
canActivate(context: ExecutionContext) {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass()
]);
if (isPublic) {
return true;
}
return super.canActivate(context);
}
handleRequest(err: any, user:any, info:any) {
if (info instanceof TokenExpiredError) {
throw new UnauthorizedException({
statusCode: 401,
message: 'Refresh Token Expired',
code: 'RefreshTokenExpired'
});
}
if (err || !user) {
throw new UnauthorizedException({
statusCode: 401,
message: 'Invalid Token',
code: 'InvalidToken'
});
}
return user;
}
}

View File

@@ -0,0 +1,22 @@
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
@Injectable()
export class JwtAccessStrategy extends PassportStrategy(Strategy, "access-token") {
constructor(configService: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.get<string>('JWT_SECRET')!
});
}
async validate(payload: any) {
const token = ExtractJwt.fromAuthHeaderAsBearerToken();
if (!token) {
throw new UnauthorizedException();
}
return { id: payload.id };
}
}

View File

@@ -0,0 +1,26 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
@Injectable()
export class JwtRefreshStrategy extends PassportStrategy(Strategy, 'refresh-token') {
constructor(configService: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.get<string>('JWT_SECRET')!,
passReqToCallback: true
});
}
async validate(payload: any) {
const token = ExtractJwt.fromAuthHeaderAsBearerToken();
if (!token) throw new UnauthorizedException('Invalid Refresh Token');
return {
id: payload.id,
token
};
}
}

View File

@@ -1,13 +1,11 @@
import { Body, Controller, Get, Post, Query } from "@nestjs/common"; import { Body, Controller, Get, Headers, Post, Query, Req, UseGuards } from "@nestjs/common";
import { AccountService } from "./account.service"; import { AccountService } from "./account.service";
import { import * as DTO from "./dto";
CheckDuplicationRequest, CheckDuplicationResponse, import { JwtAccessAuthGuard } from "src/middleware/auth/guard/access-token.guard";
SendVerificationCodeRequest, SendVerificationCodeResponse, import { Public } from "src/common/decorators/public.decorator";
VerifyCodeRequest, VerifyCodeResponse, import { JwtRefreshAuthGuard } from "src/middleware/auth/guard/refresh-token.guard";
LoginRequest, LoginResponse,
SignupRequest, SignupResponse
} from "./dto";
@UseGuards(JwtAccessAuthGuard)
@Controller('account') @Controller('account')
export class AccountController { export class AccountController {
constructor(private readonly accountService: AccountService) {} constructor(private readonly accountService: AccountService) {}
@@ -17,27 +15,68 @@ export class AccountController {
return "Test" return "Test"
} }
@Public()
@Get('check-duplication') @Get('check-duplication')
async checkDuplication(@Query() query: CheckDuplicationRequest): Promise<CheckDuplicationResponse> { async checkDuplication(@Query() query: DTO.CheckDuplicationRequest): Promise<DTO.CheckDuplicationResponse> {
return await this.accountService.checkDuplication(query); return await this.accountService.checkDuplication(query);
} }
@Post('send-verification-code') @Public()
async sendVerificationCode(@Body() body: SendVerificationCodeRequest): Promise<SendVerificationCodeResponse> { @Post('send-email-verification-code')
async sendEmailVerificationCode(@Body() body: DTO.SendEmailVerificationCodeRequest): Promise<DTO.SendEmailVerificationCodeResponse> {
const result = await this.accountService.sendVerificationCode(body); const result = await this.accountService.sendVerificationCode(body);
return result; return result;
} }
@Post('verify-code') @Public()
async verifyCode(@Body() body: VerifyCodeRequest): Promise<VerifyCodeResponse> { @Post('verify-email-verification-code')
console.log(body.email); async verifyCode(@Body() body: DTO.VerifyEmailVerificationCodeRequest): Promise<DTO.VerifyEmailVerificationCodeResponse> {
const result = await this.accountService.verifyCode(body); const result = await this.accountService.verifyCode(body);
return result; return result;
} }
@Public()
@Post('send-reset-password-code')
async sendResetPasswordCode(@Body() body: DTO.SendResetPasswordCodeRequest): Promise<DTO.SendResetPasswordCodeResponse> {
const result = await this.accountService.sendResetPasswordCode(body);
return result;
}
@Public()
@Post('verify-reset-password-code')
async verifyResetPasswordCode(@Body() body: DTO.VerifyResetPasswordCodeRequest): Promise<DTO.VerifyResetPasswordCodeResponse> {
const result = await this.accountService.verifyResetPasswordCode(body);
return result;
}
@Public()
@Post('reset-password')
async resetPassword(@Body() body: DTO.ResetPasswordRequest): Promise<DTO.ResetPasswordResponse> {
const result = await this.accountService.resetPassword(body);
return result;
}
@Public()
@Post('signup') @Post('signup')
async signup(@Body() body: SignupRequest): Promise<LoginResponse> { async signup(@Body() body: DTO.SignupRequest): Promise<DTO.SignupResponse> {
const result = await this.accountService.signup(body); const result = await this.accountService.signup(body);
return result; return result;
} }
@Public()
@Post('login')
async login(@Body() body: DTO.LoginRequest): Promise<DTO.LoginResponse> {
console.log('a');
const result = await this.accountService.login(body);
return result;
}
@Public()
@UseGuards(JwtRefreshAuthGuard)
@Get('refresh-access-token')
async refreshAccessToken(@Req() req): Promise<DTO.RefreshAccessTokenResponse> {
const id = req.user.id;
const newAccessToken = this.accountService.refreshAccessToken(id);
return newAccessToken;
}
} }

View File

@@ -1,11 +1,12 @@
import { Module } from "@nestjs/common"; import { forwardRef, Module } from "@nestjs/common";
import { AccountController } from "./account.controller"; import { AccountController } from "./account.controller";
import { AccountRepo } from "./account.repo"; import { AccountRepo } from "./account.repo";
import { AccountService } from "./account.service"; import { AccountService } from "./account.service";
import { AuthModule } from 'src/middleware/auth/auth.module';
@Module({ @Module({
imports: [forwardRef(() => AuthModule)],
controllers: [AccountController], controllers: [AccountController],
providers: [AccountService, AccountRepo], providers: [AccountService, AccountRepo],
exports: [AccountService, AccountRepo] exports: [AccountService, AccountRepo]
}) })
export class AccountModule {} export class AccountModule {}

View File

@@ -1,13 +1,13 @@
import { Inject, Injectable } from "@nestjs/common"; import { Inject, Injectable } from "@nestjs/common";
import * as schema from "drizzle/schema"; import * as schema from "drizzle/schema";
import { countDistinct, and, eq } from 'drizzle-orm'; import { countDistinct, and, eq, not } from 'drizzle-orm';
import { NodePgDatabase } from "drizzle-orm/node-postgres"; import { NodePgDatabase } from "drizzle-orm/node-postgres";
@Injectable() @Injectable()
export class AccountRepo { export class AccountRepo {
constructor(@Inject('DRIZZLE') private readonly db: NodePgDatabase<typeof schema>) {} constructor(@Inject('DRIZZLE') private readonly db: NodePgDatabase<typeof schema>) {}
async checkDuplication(type: 'email' | 'accountId', value: string) { async checkIdExists(type: 'email' | 'accountId', value: string) {
const result = await this const result = await this
.db .db
.select({ count: countDistinct(schema.account[type])}) .select({ count: countDistinct(schema.account[type])})
@@ -38,5 +38,48 @@ export class AccountRepo {
}); });
} }
async async login(
type: 'email' | 'accountId'
, id: string
) {
return this
.db
.select()
.from(schema.account)
.where(
and(
eq(schema.account[type], id),
eq(schema.account.isDeleted, false),
eq(schema.account.status, 'active')
)
);
}
async findById(id: string) {
return await this
.db
.select()
.from(schema.account)
.where(
and(
eq(schema.account.id, id),
eq(schema.account.isDeleted, false)
)
)
}
async updatePassword(type: 'email' | 'accountId', id: string, value: string) {
return await this
.db
.update(schema.account)
.set({
password: value
})
.where(
and(
eq(schema.account[type], id),
eq(schema.account.isDeleted, false)
)
);
}
} }

View File

@@ -5,23 +5,25 @@ import { MailerService } from "src/util/mailer/mailer.service";
import { Generator } from "src/util/generator"; import { Generator } from "src/util/generator";
import Redis from "ioredis"; import Redis from "ioredis";
import { Converter } from "src/util/converter"; import { Converter } from "src/util/converter";
import { AuthService } from "src/middleware/auth/auth.service";
@Injectable() @Injectable()
export class AccountService { export class AccountService {
constructor( constructor(
private readonly accountRepo: AccountRepo private readonly accountRepo: AccountRepo
, private readonly mailerService: MailerService , private readonly mailerService: MailerService
, private readonly authService: AuthService
, @Inject("REDIS") private readonly redis: Redis , @Inject("REDIS") private readonly redis: Redis
) {} ) {}
async checkDuplication(data: DTO.CheckDuplicationRequest): Promise<DTO.CheckDuplicationResponse> { async checkDuplication(data: DTO.CheckDuplicationRequest): Promise<DTO.CheckDuplicationResponse> {
const { type, value } = data; const { type, value } = data;
const count = await this.accountRepo.checkDuplication(type, value); const count = await this.accountRepo.checkIdExists(type, value);
return { isDuplicated: count > 0 }; return { isDuplicated: count > 0, success: true };
} }
async sendVerificationCode(data: DTO.SendVerificationCodeRequest): Promise<DTO.SendVerificationCodeResponse> { async sendVerificationCode(data: DTO.SendEmailVerificationCodeRequest): Promise<DTO.SendEmailVerificationCodeResponse> {
const { email } = data; const { email } = data;
const code = Generator.getVerificationCode(); const code = Generator.getVerificationCode();
const html = `<p>Your verification code is: <strong style="font-size:16px;">${code}</strong></p>`; const html = `<p>Your verification code is: <strong style="font-size:16px;">${code}</strong></p>`;
@@ -36,20 +38,20 @@ export class AccountService {
} }
} }
async verifyCode(data: DTO.VerifyCodeRequest): Promise<DTO.VerifyCodeResponse> { async verifyCode(data: DTO.VerifyEmailVerificationCodeRequest): Promise<DTO.VerifyEmailVerificationCodeResponse> {
const { email, code } = data; const { email, code } = data;
const storedCode = await this.redis.get(`verify:${email}`); const storedCode = await this.redis.get(`verify:${email}`);
if (!storedCode) { if (!storedCode) {
return { verified: false, error: '잘못된 이메일이거나 코드가 만료되었습니다.'}; return { verified: false, success: true, error: '잘못된 이메일이거나 코드가 만료되었습니다.'};
} }
if (storedCode !== code) { if (storedCode !== code) {
return { verified: false, error: "잘못된 코드입니다." }; return { verified: false, success: true, error: "잘못된 코드입니다." };
} }
await this.redis.del(`verify:${email}`); await this.redis.del(`verify:${email}`);
return { verified: true, message: "이메일 인증이 완료되었습니다." }; return { verified: true, success: true, message: "이메일 인증이 완료되었습니다." };
} }
async signup(data: DTO.SignupRequest): Promise<DTO.SignupResponse> { async signup(data: DTO.SignupRequest): Promise<DTO.SignupResponse> {
@@ -70,4 +72,133 @@ export class AccountService {
} }
} }
async login(data: DTO.LoginRequest): Promise<DTO.LoginResponse> {
const { type, id, password } = data;
const queryResult = await this.accountRepo.login(type, id);
const typeValue = type === 'email' ? '이메일' : '아이디';
if (!queryResult || (queryResult.length < 1)) {
return {
success: false,
message: `존재하지 않는 ${typeValue} 입니다.`
};
}
const hashedPassword = queryResult[0].password;
const isPasswordMatch = Converter.comparePassword(password, hashedPassword);
if (!isPasswordMatch) {
return {
success: false,
message: `비밀번호가 맞지 않습니다.`
};
}
{
const { id, accountId, status, isDeleted, birthday } = queryResult[0];
const payload = {
id, accountId, status, isDeleted, birthday
};
const { accessToken, refreshToken } = this.authService.generateTokens(payload);
return {
success: true,
accessToken: accessToken,
refreshToken: refreshToken
};
}
}
async refreshAccessToken(id: string): Promise<DTO.RefreshAccessTokenResponse> {
const { accessToken, refreshToken } = this.authService.refreshTokens(id);
return {
accessToken: accessToken,
refreshToken: refreshToken,
success: true
};
}
async sendResetPasswordCode(data: DTO.SendResetPasswordCodeRequest): Promise<DTO.SendResetPasswordCodeResponse> {
const { email } = data;
const count = await this.accountRepo.checkIdExists('email', email);
if (count === 0) {
return {
success: false,
error: "찾을 수 없는 사용자"
};
}
const code = Generator.getResetPasswordCode();
const html =
`<p>Your Password Reset Code is: <strong>${code}</strong></p>`
+ `<p>Please Enter this code in 5 minutes.</p>`;
const result = await this.mailerService.sendMail(email, "<Scheduler> 비밀번호 초기화 코드", html);
if (result.rejected.length > 0) {
return {
success: false,
error: result.response
};
}
await this.redis.set(`resetPassword:${email}`, code, 'EX', 300);
return {
success: true,
message: "비밀번호 초기화 코드 발송 완료"
};
}
async verifyResetPasswordCode(data: DTO.VerifyResetPasswordCodeRequest): Promise<DTO.VerifyResetPasswordCodeResponse> {
const { email, code } = data;
const storedCode = await this.redis.get(`resetPassword:${email}`);
if (!storedCode) {
return {
success: false,
verified: false,
error: "잘못된 이메일이거나 코드가 만료되었습니다."
};
}
if (storedCode !== code) {
return {
success: false,
verified: false,
error: "잘못된 코드입니다."
};
}
await this.redis.del(`resetPassword:${email}`);
return {
success: true,
verified: true,
message: "비밀번호 초기화 코드 인증 완료"
};
}
async resetPassword(data: DTO.ResetPasswordRequest): Promise<DTO.ResetPasswordResponse> {
const { email, password } = data;
const hashedPassword = Converter.getHashedPassword(password);
const result = await this.accountRepo.updatePassword('email', email, hashedPassword);
if (!result.rowCount || result.rowCount === 0) {
return {
success: false,
error: "비밀번호 초기화 실패"
};
}
return {
success: true,
message: "비밀번호 초기화 성공"
};
}
} }

View File

@@ -1,4 +1,4 @@
export class SendVerificationCodeResponseDto { export class BaseResponseDto {
success: boolean; success: boolean;
message?: string; message?: string;
error?: string; error?: string;

View File

@@ -1,3 +1,5 @@
export class CheckDuplicationResponseDto { import { BaseResponseDto } from "../base-response.dto";
export class CheckDuplicationResponseDto extends BaseResponseDto {
isDuplicated: boolean; isDuplicated: boolean;
} }

View File

@@ -1,14 +1,27 @@
import { ResetPasswordRequestDto } from './resetPassword/reset-password-request.dto';
export { CheckDuplicationRequestDto as CheckDuplicationRequest } from './checkDuplication/check-duplication-request.dto'; export { CheckDuplicationRequestDto as CheckDuplicationRequest } from './checkDuplication/check-duplication-request.dto';
export { CheckDuplicationResponseDto as CheckDuplicationResponse } from './checkDuplication/check-duplication-response.dto'; export { CheckDuplicationResponseDto as CheckDuplicationResponse } from './checkDuplication/check-duplication-response.dto';
export { SendVerificationCodeRequestDto as SendVerificationCodeRequest } from './sendVerification/send-verification-code-request.dto'; export { SendEmailVerificationCodeRequestDto as SendEmailVerificationCodeRequest } from './sendEmailVerificationCode/send-email-verification-code-request.dto';
export { SendVerificationCodeResponseDto as SendVerificationCodeResponse } from './sendVerification/send-verification-code-response.dto'; export { SendEmailVerificationCodeResponseDto as SendEmailVerificationCodeResponse } from './sendEmailVerificationCode/send-email-verification-code-response.dto';
export { VerifyCodeRequestDto as VerifyCodeRequest } from './verifyCode/verify-code-request.dto'; export { VerifyEmailVerificationCodeRequestDto as VerifyEmailVerificationCodeRequest } from './verifyEmailVerificationCode/verify-email-verification-code-request.dto';
export { VerifyCodeResponseDto as VerifyCodeResponse } from './verifyCode/verify-code-response.dto'; export { VerifyEmailVerificationCodeResponseDto as VerifyEmailVerificationCodeResponse } from './verifyEmailVerificationCode/verify-email-verification-code-response.dto';
export { SignupRequestDto as SignupRequest } from './signup/signup-request.dto'; export { SignupRequestDto as SignupRequest } from './signup/signup-request.dto';
export { SignupResponseDto as SignupResponse } from './signup/signup-response.dto'; export { SignupResponseDto as SignupResponse } from './signup/signup-response.dto';
export { LoginRequestDto as LoginRequest } from './login/login-request.dto'; export { LoginRequestDto as LoginRequest } from './login/login-request.dto';
export { LoginResponseDto as LoginResponse } from './login/login-response.dto' export { LoginResponseDto as LoginResponse } from './login/login-response.dto'
export { RefreshAccessTokenResponseDto as RefreshAccessTokenResponse } from './refreshAccessToken/refresh-access-token-response.dto';
export { SendResetPasswordCodeRequestDto as SendResetPasswordCodeRequest } from './sendResetPasswordCode/send-reset-password-code-request.dto';
export { SendResetPasswordCodeResponseDto as SendResetPasswordCodeResponse } from './sendResetPasswordCode/send-reset-password-code-response.dto';
export { VerifyResetPasswordCodeRequestDto as VerifyResetPasswordCodeRequest } from './verifyResetPasswordCode/verify-reset-password-code-request.dto';
export { VerifyResetPasswordCodeResponseDto as VerifyResetPasswordCodeResponse } from './verifyResetPasswordCode/verify-reset-password-code-response.dto'
export { ResetPasswordRequestDto as ResetPasswordRequest } from './resetPassword/reset-password-request.dto';
export { ResetPasswordResponseDto as ResetPasswordResponse } from './resetPassword/reset-password-response.dto';

View File

@@ -1,5 +1,6 @@
export class LoginResponseDto { import { BaseResponseDto } from "../base-response.dto";
success: boolean;
message?: string; export class LoginResponseDto extends BaseResponseDto {
error?: string; accessToken?: string;
refreshToken?: string;
} }

View File

@@ -0,0 +1,6 @@
import { BaseResponseDto } from "../base-response.dto";
export class RefreshAccessTokenResponseDto extends BaseResponseDto{
accessToken: string;
refreshToken: string;
}

View File

@@ -0,0 +1,9 @@
import { IsEmail, IsString } from "@nestjs/class-validator";
export class ResetPasswordRequestDto {
@IsEmail()
email: string;
@IsString()
password: string;
}

View File

@@ -0,0 +1,5 @@
import { BaseResponseDto } from "../base-response.dto";
export class ResetPasswordResponseDto extends BaseResponseDto {
}

View File

@@ -0,0 +1,6 @@
import { IsEmail } from "@nestjs/class-validator";
export class SendEmailVerificationCodeRequestDto {
@IsEmail()
email: string;
}

View File

@@ -0,0 +1,4 @@
import { BaseResponseDto } from "../base-response.dto";
export class SendEmailVerificationCodeResponseDto extends BaseResponseDto{
}

View File

@@ -1,6 +1,6 @@
import { IsEmail } from "@nestjs/class-validator"; import { IsEmail } from "@nestjs/class-validator";
export class SendVerificationCodeRequestDto { export class SendResetPasswordCodeRequestDto {
@IsEmail() @IsEmail()
email: string; email: string;
} }

View File

@@ -0,0 +1,4 @@
import { BaseResponseDto } from "../base-response.dto";
export class SendResetPasswordCodeResponseDto extends BaseResponseDto {
}

View File

@@ -1,5 +1,4 @@
export class SignupResponseDto { import { BaseResponseDto } from "../base-response.dto";
success: boolean;
message?: string; export class SignupResponseDto extends BaseResponseDto {
error?: string;
} }

View File

@@ -1,5 +0,0 @@
export class VerifyCodeResponseDto {
verified: boolean;
message?: string;
error?: string;
}

View File

@@ -1,6 +1,6 @@
import { IsEmail, IsString } from "@nestjs/class-validator"; import { IsEmail, IsString } from "@nestjs/class-validator";
export class VerifyCodeRequestDto { export class VerifyEmailVerificationCodeRequestDto {
@IsEmail() @IsEmail()
email: string; email: string;

View File

@@ -0,0 +1,5 @@
import { BaseResponseDto } from "../base-response.dto";
export class VerifyEmailVerificationCodeResponseDto extends BaseResponseDto{
verified: boolean;
}

View File

@@ -0,0 +1,9 @@
import { IsEmail, IsString } from "@nestjs/class-validator"
export class VerifyResetPasswordCodeRequestDto {
@IsEmail()
email: string;
@IsString()
code: string;
}

View File

@@ -0,0 +1,5 @@
import { BaseResponseDto } from "../base-response.dto";
export class VerifyResetPasswordCodeResponseDto extends BaseResponseDto {
verified: boolean;
}

View File

@@ -2,4 +2,36 @@ export class Generator {
static getVerificationCode() { static getVerificationCode() {
return Math.random().toString().slice(2, 8); return Math.random().toString().slice(2, 8);
} }
private static getRandomCharacter(string: string) {
return string[Math.floor(Math.random() * string.length)];
}
private static getShuffledString(string: string) {
let arr = string.split('');
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr.join('');
}
static getResetPasswordCode() {
const alphabets = 'abcdefghijklmnopqrstuvwxyz';
const numbers = '0123456789';
const specials = '!@#$%^';
const all = alphabets + numbers + specials;
let resetPasswordCode = Generator.getRandomCharacter(alphabets);
let requiredNumber = Generator.getRandomCharacter(numbers);
let requiredSpecial = Generator.getRandomCharacter(specials);
let shuffledRestPart = Generator.getShuffledString(all).slice(0, 5);
let shuffledRestCode = Generator.getShuffledString(requiredNumber + requiredSpecial + shuffledRestPart);
return resetPasswordCode + shuffledRestCode;
}
} }

View File

@@ -2,8 +2,10 @@
"compilerOptions": { "compilerOptions": {
"noEmitOnError": true, "noEmitOnError": true,
"sourceMap": false, "sourceMap": false,
"incremental": true, "incremental": false,
"tsBuildInfoFile": ".tsbuildinfo" "noEmit": false,
"tsBuildInfoFile": ".tsbuildinfo",
"outDir": "./dist"
}, },
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"] "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]

View File

@@ -14,12 +14,13 @@
"sourceMap": true, "sourceMap": true,
"outDir": "./dist", "outDir": "./dist",
"baseUrl": "./", "baseUrl": "./",
"incremental": true, "incremental": false,
"skipLibCheck": true, "skipLibCheck": true,
"strictNullChecks": true, "strictNullChecks": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noImplicitAny": false, "noImplicitAny": false,
"strictBindCallApply": false, "strictBindCallApply": false,
"noFallthroughCasesInSwitch": false "noFallthroughCasesInSwitch": false,
"noEmit": false
} }
} }

682
yarn.lock

File diff suppressed because it is too large Load Diff