Skip to content

Commit

Permalink
feat/#55/admin api 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
gwgw123 committed Jan 5, 2025
1 parent af210cc commit 03d4393
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 22 deletions.
388 changes: 366 additions & 22 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@nestjs/swagger": "^7.4.2",
"@prisma/client": "^6.0.1",
"@types/cache-manager-redis-store": "^2.0.4",
"bcrypt": "^5.1.1",
"cache-manager": "^5.7.6",
"cache-manager-redis-store": "^2.0.0",
"class-transformer": "^0.5.1",
Expand All @@ -49,6 +50,7 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.4.4",
"@types/bcrypt": "^5.0.2",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
Expand Down
13 changes: 13 additions & 0 deletions prisma/migrations/20250105105313_create_admin/migration.sql
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");
10 changes: 10 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,13 @@ model PartProgress {
@@id([userId, partId])
@@map("part_progress")
}

model AdminUser {
id Int @id @default(autoincrement())
email String @unique @db.VarChar(50)
password String @db.VarChar(255)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@map("admin_user")
}
10 changes: 10 additions & 0 deletions src/admin/admin.module.ts
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 {}
16 changes: 16 additions & 0 deletions src/admin/controllers/admin.controller.ts
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);
}
}
18 changes: 18 additions & 0 deletions src/admin/dtos/login-admin.dto.ts
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;
}
53 changes: 53 additions & 0 deletions src/admin/services/admin.service.ts
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);
}
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SectionsModule } from './sections/sections.module';
import { ConfigModule } from '@nestjs/config';
import { AuthModule } from './auth/auth.module';
import { AppController } from './app.controller';
import { AdminModule } from './admin/admin.module';

@Module({
imports: [
Expand All @@ -22,6 +23,7 @@ import { AppController } from './app.controller';
isGlobal: true,
}),
AuthModule,
AdminModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down

0 comments on commit 03d4393

Please sign in to comment.