-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
490 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
prisma/migrations/20250105105313_create_admin/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- CreateTable | ||
CREATE TABLE "admin_user" ( | ||
"id" SERIAL NOT NULL, | ||
"email" VARCHAR(50) NOT NULL, | ||
"password" VARCHAR(255) NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "admin_user_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "admin_user_email_key" ON "admin_user"("email"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AdminController } from './controllers/admin.controller'; | ||
import { AdminService } from './services/admin.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
providers: [AdminService], | ||
controllers: [AdminController], | ||
}) | ||
export class AdminModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { AdminService } from '../services/admin.service'; | ||
import { LoginAdminDto } from '../dtos/login-admin.dto'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
|
||
@Controller('admin') | ||
export class AdminController { | ||
constructor(private readonly adminService: AdminService) {} | ||
|
||
// admin 로그인 | ||
@ApiTags('admins') | ||
@Post('login') | ||
async loginAdmin(@Body() loginAdminInfo: LoginAdminDto) { | ||
return await this.adminService.loginAdmin(loginAdminInfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsEmail, IsString } from 'class-validator'; | ||
|
||
export class LoginAdminDto { | ||
@ApiProperty({ | ||
description: 'admin 이메일', | ||
example: 'gwgw123@gmail.com', | ||
}) | ||
@IsEmail() | ||
readonly email: string; | ||
|
||
@ApiProperty({ | ||
description: 'admin 비밀번호', | ||
example: 'gwpassword123', | ||
}) | ||
@IsString() | ||
readonly password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
Injectable, | ||
NotFoundException, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { LoginAdminDto } from '../dtos/login-admin.dto'; | ||
|
||
@Injectable() | ||
export class AdminService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
/** | ||
* admin 로그인 메서드 | ||
* @param loginAdminInfo | ||
* email과 password | ||
* @returns | ||
*/ | ||
async loginAdmin(loginAdminInfo: LoginAdminDto) { | ||
const { email, password } = loginAdminInfo; | ||
|
||
const userInfo = await this.prisma.adminUser.findUnique({ | ||
where: { email }, | ||
}); | ||
|
||
if (!userInfo) { | ||
throw new NotFoundException('Please check your email.'); | ||
} | ||
|
||
// 비밀번호 복호화 | ||
const isMatch = await this.comparePasswords(password, userInfo.password); | ||
|
||
if (!isMatch) { | ||
throw new UnauthorizedException('Password does not match.'); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* password 복호화 함수 | ||
* @param plainPassword 사용자가 입력한 password 값 | ||
* @param hashedPassword DB에 있는 password 값 | ||
* @returns | ||
*/ | ||
private async comparePasswords( | ||
plainPassword: string, | ||
hashedPassword: string, | ||
): Promise<boolean> { | ||
return await bcrypt.compare(plainPassword, hashedPassword); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters