Skip to content

Commit

Permalink
feat: 회원가입 시 이메일 형식 확인 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
minjungw00 committed Nov 20, 2024
1 parent 996a250 commit d28cbdf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion server/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Response,
UnauthorizedException,
ConflictException,
BadRequestException,
} from "@nestjs/common";
import { Response as ExpressResponse, Request as ExpressRequest } from "express";
import { AuthService } from "./auth.service";
Expand Down Expand Up @@ -44,11 +45,15 @@ export class AuthController {
status: 201,
description: "The user has been successfully created.",
})
@ApiResponse({ status: 400, description: "Bad Request" })
@ApiResponse({ status: 400, description: "Bad Request: Invalid email format" })
@ApiResponse({ status: 409, description: "Conflict: Email already exists" })
async register(@Body() body: { email: string; password: string; name: string }): Promise<void> {
const { email, password, name } = body;

if (!this.authService.isValidEmail(email)) {
throw new BadRequestException("Invalid email format");
}

const existingUser = await this.authService.findByEmail(email);
if (existingUser) {
throw new ConflictException("Email already exists");
Expand Down
5 changes: 5 additions & 0 deletions server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export class AuthService {
private jwtService: JwtService,
) {}

isValidEmail(email: string): boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}

async register(email: string, password: string, name: string): Promise<User> {
const hashedPassword = await bcrypt.hash(password, 10);
return this.userModel.create({
Expand Down
12 changes: 11 additions & 1 deletion server/src/auth/test/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AuthController } from "../auth.controller";
import { AuthService } from "../auth.service";
import { JwtAuthGuard } from "../guards/jwt-auth.guard";
import { JwtRefreshTokenAuthGuard } from "../guards/jwt-refresh-token-auth.guard";
import { UnauthorizedException, ConflictException } from "@nestjs/common";
import { UnauthorizedException, ConflictException, BadRequestException } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { Response as ExpressResponse, Request as ExpressRequest } from "express";

Expand All @@ -26,6 +26,7 @@ describe("AuthController", () => {
getProfile: jest.fn(),
refresh: jest.fn(),
increaseTokenVersion: jest.fn(),
isValidEmail: jest.fn(),
};

const mockJwtService = {
Expand Down Expand Up @@ -53,6 +54,7 @@ describe("AuthController", () => {
describe("register", () => {
it("should call authService.register and return the result", async () => {
const dto = { email: "[email protected]", password: "password", name: "Test User" };
mockAuthService.isValidEmail.mockReturnValue(true);
mockAuthService.findByEmail.mockResolvedValue(null);
mockAuthService.register.mockResolvedValue(undefined);

Expand All @@ -62,8 +64,16 @@ describe("AuthController", () => {
expect(mockAuthService.register).toHaveBeenCalledWith(dto.email, dto.password, dto.name);
});

it("should throw BadRequestException if email format is invalid", async () => {
const dto = { email: "invalid-email", password: "password", name: "Test User" };
mockAuthService.isValidEmail.mockReturnValue(false);

await expect(authController.register(dto)).rejects.toThrow(BadRequestException);
});

it("should throw ConflictException if email already exists", async () => {
const dto = { email: "[email protected]", password: "password", name: "Test User" };
mockAuthService.isValidEmail.mockReturnValue(true);
mockAuthService.findByEmail.mockResolvedValue({ id: "mockNanoId123", email: dto.email });

await expect(authController.register(dto)).rejects.toThrow(ConflictException);
Expand Down

0 comments on commit d28cbdf

Please sign in to comment.