- 비밀번호 초기화 로직 1차 구현(테스트 필요)
This commit is contained in:
39
src/common/filters/all-exceptions.filter.ts
Normal file
39
src/common/filters/all-exceptions.filter.ts
Normal 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
|
||||
});
|
||||
}
|
||||
}
|
||||
12
src/main.ts
12
src/main.ts
@@ -1,8 +1,16 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import {
|
||||
FastifyAdapter,
|
||||
NestFastifyApplication
|
||||
} from '@nestjs/platform-fastify';
|
||||
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
new FastifyAdapter()
|
||||
);
|
||||
app.enableCors({
|
||||
origin: (origin, callback) => {
|
||||
// origin이 없는 경우(local file, curl 등) 허용
|
||||
@@ -21,7 +29,7 @@ async function bootstrap() {
|
||||
});
|
||||
|
||||
app.enableShutdownHooks();
|
||||
|
||||
app.useGlobalFilters(new AllExceptionsFilter());
|
||||
await app.listen(process.env.PORT ?? 3000, () => { process.env.NODE_ENV !== 'prod' && console.log(`servier is running on ${process.env.PORT}`) });
|
||||
|
||||
}
|
||||
|
||||
@@ -22,20 +22,40 @@ export class AccountController {
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post('send-verification-code')
|
||||
async sendVerificationCode(@Body() body: DTO.SendVerificationCodeRequest): Promise<DTO.SendVerificationCodeResponse> {
|
||||
@Post('send-email-verification-code')
|
||||
async sendEmailVerificationCode(@Body() body: DTO.SendEmailVerificationCodeRequest): Promise<DTO.SendEmailVerificationCodeResponse> {
|
||||
const result = await this.accountService.sendVerificationCode(body);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post('verify-code')
|
||||
async verifyCode(@Body() body: DTO.VerifyCodeRequest): Promise<DTO.VerifyCodeResponse> {
|
||||
console.log(body.email);
|
||||
@Post('verify-email-verification-code')
|
||||
async verifyCode(@Body() body: DTO.VerifyEmailVerificationCodeRequest): Promise<DTO.VerifyEmailVerificationCodeResponse> {
|
||||
const result = await this.accountService.verifyCode(body);
|
||||
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')
|
||||
async signup(@Body() body: DTO.SignupRequest): Promise<DTO.SignupResponse> {
|
||||
|
||||
@@ -7,7 +7,7 @@ import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
export class AccountRepo {
|
||||
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
|
||||
.db
|
||||
.select({ count: countDistinct(schema.account[type])})
|
||||
@@ -70,4 +70,19 @@ export class AccountRepo {
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
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)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,12 @@ export class AccountService {
|
||||
|
||||
async checkDuplication(data: DTO.CheckDuplicationRequest): Promise<DTO.CheckDuplicationResponse> {
|
||||
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 code = Generator.getVerificationCode();
|
||||
const html = `<p>Your verification code is: <strong style="font-size:16px;">${code}</strong></p>`;
|
||||
@@ -38,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 storedCode = await this.redis.get(`verify:${email}`);
|
||||
|
||||
if (!storedCode) {
|
||||
return { verified: false, error: '잘못된 이메일이거나 코드가 만료되었습니다.'};
|
||||
return { verified: false, success: true, error: '잘못된 이메일이거나 코드가 만료되었습니다.'};
|
||||
}
|
||||
if (storedCode !== code) {
|
||||
return { verified: false, error: "잘못된 코드입니다." };
|
||||
return { verified: false, success: true, error: "잘못된 코드입니다." };
|
||||
}
|
||||
|
||||
await this.redis.del(`verify:${email}`);
|
||||
return { verified: true, message: "이메일 인증이 완료되었습니다." };
|
||||
return { verified: true, success: true, message: "이메일 인증이 완료되었습니다." };
|
||||
}
|
||||
|
||||
async signup(data: DTO.SignupRequest): Promise<DTO.SignupResponse> {
|
||||
@@ -77,7 +77,7 @@ export class AccountService {
|
||||
const { type, id, password } = data;
|
||||
const queryResult = await this.accountRepo.login(type, id);
|
||||
const typeValue = type === 'email' ? '이메일' : '아이디';
|
||||
console.log(queryResult);
|
||||
|
||||
if (!queryResult || (queryResult.length < 1)) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -114,7 +114,87 @@ export class AccountService {
|
||||
const { accessToken, refreshToken } = this.authService.refreshTokens(id);
|
||||
return {
|
||||
accessToken: accessToken,
|
||||
refreshToken: refreshToken
|
||||
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,
|
||||
message: "잘못된 이메일이거나 코드가 만료되었습니다."
|
||||
};
|
||||
}
|
||||
|
||||
if (storedCode !== code) {
|
||||
return {
|
||||
success: false,
|
||||
message: "잘못된 코드입니다."
|
||||
};
|
||||
}
|
||||
|
||||
await this.redis.del(`resetPassword:${email}`);
|
||||
|
||||
return {
|
||||
success: 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.rows.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
error: "비밀번호 초기화 실패"
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "비밀번호 초기화 성공"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
export class SendVerificationCodeResponseDto {
|
||||
export class BaseResponseDto {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
error?: string;
|
||||
@@ -1,3 +1,5 @@
|
||||
export class CheckDuplicationResponseDto {
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class CheckDuplicationResponseDto extends BaseResponseDto {
|
||||
isDuplicated: boolean;
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import { ResetPasswordRequestDto } from './resetPassword/reset-password-request.dto';
|
||||
|
||||
export { CheckDuplicationRequestDto as CheckDuplicationRequest } from './checkDuplication/check-duplication-request.dto';
|
||||
export { CheckDuplicationResponseDto as CheckDuplicationResponse } from './checkDuplication/check-duplication-response.dto';
|
||||
|
||||
export { SendVerificationCodeRequestDto as SendVerificationCodeRequest } from './sendVerification/send-verification-code-request.dto';
|
||||
export { SendVerificationCodeResponseDto as SendVerificationCodeResponse } from './sendVerification/send-verification-code-response.dto';
|
||||
export { SendEmailVerificationCodeRequestDto as SendEmailVerificationCodeRequest } from './sendEmailVerificationCode/send-email-verification-code-request.dto';
|
||||
export { SendEmailVerificationCodeResponseDto as SendEmailVerificationCodeResponse } from './sendEmailVerificationCode/send-email-verification-code-response.dto';
|
||||
|
||||
export { VerifyCodeRequestDto as VerifyCodeRequest } from './verifyCode/verify-code-request.dto';
|
||||
export { VerifyCodeResponseDto as VerifyCodeResponse } from './verifyCode/verify-code-response.dto';
|
||||
export { VerifyEmailVerificationCodeRequestDto as VerifyEmailVerificationCodeRequest } from './verifyEmailVerificationCode/verify-email-verification-code-request.dto';
|
||||
export { VerifyEmailVerificationCodeResponseDto as VerifyEmailVerificationCodeResponse } from './verifyEmailVerificationCode/verify-email-verification-code-response.dto';
|
||||
|
||||
export { SignupRequestDto as SignupRequest } from './signup/signup-request.dto';
|
||||
export { SignupResponseDto as SignupResponse } from './signup/signup-response.dto';
|
||||
@@ -13,4 +15,13 @@ export { SignupResponseDto as SignupResponse } from './signup/signup-response.dt
|
||||
export { LoginRequestDto as LoginRequest } from './login/login-request.dto';
|
||||
export { LoginResponseDto as LoginResponse } from './login/login-response.dto'
|
||||
|
||||
export { RefreshAccessTokenResponseDto as RefreshAccessTokenResponse } from './refreshAccessToken/refresh-access-token-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';
|
||||
@@ -1,7 +1,6 @@
|
||||
export class LoginResponseDto {
|
||||
success: boolean;
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class LoginResponseDto extends BaseResponseDto {
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
export class RefreshAccessTokenResponseDto {
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class RefreshAccessTokenResponseDto extends BaseResponseDto{
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IsEmail, IsString } from "@nestjs/class-validator";
|
||||
|
||||
export class ResetPasswordRequestDto {
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class ResetPasswordResponseDto extends BaseResponseDto {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { IsEmail } from "@nestjs/class-validator";
|
||||
|
||||
export class SendEmailVerificationCodeRequestDto {
|
||||
@IsEmail()
|
||||
email: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class SendEmailVerificationCodeResponseDto extends BaseResponseDto{
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IsEmail } from "@nestjs/class-validator";
|
||||
|
||||
export class SendVerificationCodeRequestDto {
|
||||
export class SendResetPasswordCodeRequestDto {
|
||||
@IsEmail()
|
||||
email: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class SendResetPasswordCodeResponseDto extends BaseResponseDto {
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
export class SignupResponseDto {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
error?: string;
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class SignupResponseDto extends BaseResponseDto {
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
export class VerifyCodeResponseDto {
|
||||
verified: boolean;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IsEmail, IsString } from "@nestjs/class-validator";
|
||||
|
||||
export class VerifyCodeRequestDto {
|
||||
export class VerifyEmailVerificationCodeRequestDto {
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class VerifyEmailVerificationCodeResponseDto extends BaseResponseDto{
|
||||
verified: boolean;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IsEmail, IsString } from "@nestjs/class-validator"
|
||||
|
||||
export class VerifyResetPasswordCodeRequestDto {
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
@IsString()
|
||||
code: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { BaseResponseDto } from "../base-response.dto";
|
||||
|
||||
export class VerifyResetPasswordCodeResponseDto extends BaseResponseDto {
|
||||
}
|
||||
@@ -2,4 +2,36 @@ export class Generator {
|
||||
static getVerificationCode() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user