issue # 이메일 인증 로직 구현 중
This commit is contained in:
@@ -2,8 +2,8 @@ import { Body, Controller, Get, Post, Query } from "@nestjs/common";
|
|||||||
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto";
|
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto";
|
||||||
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto";
|
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto";
|
||||||
import { AccountService } from "./account.service";
|
import { AccountService } from "./account.service";
|
||||||
import { SendVerificationCodeRequestDto } from "./dto/checkDuplication/send-verification-code-request.dto";
|
import { SendVerificationCodeRequestDto } from "./dto/sendVerification/send-verification-code-request.dto";
|
||||||
import { SendVerificationCodeResponseDto } from "./dto/checkDuplication/send-verification-code-response.dto";
|
import { SendVerificationCodeResponseDto } from "./dto/sendVerification/send-verification-code-response.dto";
|
||||||
|
|
||||||
@Controller('account')
|
@Controller('account')
|
||||||
export class AccountController {
|
export class AccountController {
|
||||||
|
|||||||
@@ -18,4 +18,18 @@ export class AccountRepo {
|
|||||||
|
|
||||||
return result[0].count;
|
return result[0].count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async activeAccount(email: string) {
|
||||||
|
return this
|
||||||
|
.db
|
||||||
|
.update(schema.account)
|
||||||
|
.set({ status: 'active' })
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(schema.account.email, email),
|
||||||
|
eq(schema.account.status, 'wait'),
|
||||||
|
eq(schema.account.isDeleted, false)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,21 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Inject, Injectable } from "@nestjs/common";
|
||||||
import { AccountRepo } from "./account.repo";
|
import { AccountRepo } from "./account.repo";
|
||||||
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto";
|
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto";
|
||||||
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto";
|
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto";
|
||||||
import { SendVerificationCodeRequestDto } from "./dto/checkDuplication/send-verification-code-request.dto";
|
import { SendVerificationCodeRequestDto } from "./dto/sendVerification/send-verification-code-request.dto";
|
||||||
import { MailerService } from "src/util/mailer/mailer.service";
|
import { MailerService } from "src/util/mailer/mailer.service";
|
||||||
import { Generator } from "src/util/generator";
|
import { Generator } from "src/util/generator";
|
||||||
import { SendVerificationCodeResponseDto } from "./dto/checkDuplication/send-verification-code-response.dto";
|
import { SendVerificationCodeResponseDto } from "./dto/sendVerification/send-verification-code-response.dto";
|
||||||
|
import Redis from "ioredis";
|
||||||
|
import { VerifyCodeResponseDto } from "./dto/verifyCode/verify-code-response.dto";
|
||||||
|
import { VerifyCodeRequestDto } from "./dto/verifyCode/verify-code-request.dto";
|
||||||
|
|
||||||
@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
|
||||||
|
, @Inject("REDIS") private readonly redis: Redis
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async checkDuplication(data: CheckDuplicationRequestDto): Promise<CheckDuplicationResponseDto> {
|
async checkDuplication(data: CheckDuplicationRequestDto): Promise<CheckDuplicationResponseDto> {
|
||||||
@@ -28,7 +32,21 @@ export class AccountService {
|
|||||||
if (result.rejected.length > 0) {
|
if (result.rejected.length > 0) {
|
||||||
return { success: false, error: result.response }
|
return { success: false, error: result.response }
|
||||||
} else {
|
} else {
|
||||||
|
await this.redis.set(`verify:${data.email}`, code, 'EX', 600);
|
||||||
|
|
||||||
return { success: true, message: "이메일 발송 완료" };
|
return { success: true, message: "이메일 발송 완료" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async verifyCode(data: VerifyCodeRequestDto): Promise<VerifyCodeResponseDto>{
|
||||||
|
const { email, code } = data;
|
||||||
|
|
||||||
|
const storedCode = await this.redis.get(`verify:${email}`);
|
||||||
|
|
||||||
|
if (!storedCode) return { verified: false, error: '잘못된 이메일이거나 코드가 만료되었습니다.'};
|
||||||
|
if (storedCode !== code) return { verified: false, error: "잘못된 코드입니다." };
|
||||||
|
|
||||||
|
await this.redis.del(`verify:${email}`);
|
||||||
|
return { verified: true, message: "이메일 인증이 완료되었습니다." };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { IsEmail, IsString } from "@nestjs/class-validator";
|
||||||
|
|
||||||
|
export class VerifyCodeRequestDto {
|
||||||
|
@IsEmail()
|
||||||
|
email: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
code: string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
export class VerifyCodeResponseDto {
|
||||||
|
verified: boolean;
|
||||||
|
message?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import Redis from "ioredis";
|
|||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: "REDIS",
|
provide: "REDIS",
|
||||||
useFactory: () => {
|
useFactory: (): Redis => {
|
||||||
return new Redis({
|
return new Redis({
|
||||||
host: process.env.RD_HOST!,
|
host: process.env.RD_HOST!,
|
||||||
port: Number(process.env.RD_PORT || 6779)
|
port: Number(process.env.RD_PORT || 6779)
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ export class MailerService {
|
|||||||
|
|
||||||
async sendMail(to: string, subject: string, html: string) {
|
async sendMail(to: string, subject: string, html: string) {
|
||||||
const accessToken = await this.oauth2Client.getAccessToken();
|
const accessToken = await this.oauth2Client.getAccessToken();
|
||||||
console.log(accessToken);
|
|
||||||
const options: SMTPTransport.Options = {
|
const options: SMTPTransport.Options = {
|
||||||
host: "smtp.gmail.com",
|
host: "smtp.gmail.com",
|
||||||
port: 465,
|
port: 465,
|
||||||
|
|||||||
Reference in New Issue
Block a user