nestjs_auth init..

This commit is contained in:
최준흠 2022-09-09 11:54:59 +09:00
parent 8871266630
commit 0135d94b55
5 changed files with 335 additions and 544 deletions

832
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
/* eslint-disable prettier/prettier */ /* eslint-disable prettier/prettier */
import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common' import { Body, Controller, Get, Post, Request, UseGuards } from '@nestjs/common'
import { User } from '@prisma/client'
import { UserDTO } from 'src/user/dtos/user.dto'
import { AuthService } from './auth.service' import { AuthService } from './auth.service'
import { JwtAuthGuard } from './guards/jwt.authguard' import { JwtAuthGuard } from './guards/jwt.authguard'
import { LocalAuthGuard } from './guards/local-auth.guard' import { LocalAuthGuard } from './guards/local-auth.guard'
@ -27,11 +29,17 @@ export class AuthController {
return response return response
} }
//Login여부 확인용 //Profile 여부 확인용
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('profile') @Get('profile')
getProfile(@Request() req) { getProfile(@Request() req) {
//console.log(req) //console.log(req)
return req.user return req.user
} }
//사용자 등록
@Post('register')
async add(@Body() data: UserDTO): Promise<User> {
return await this.authService.register(data)
}
} }

View File

@ -1,6 +1,8 @@
/* eslint-disable prettier/prettier */ /* eslint-disable prettier/prettier */
import { Injectable } from '@nestjs/common' import { Injectable } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt' import { JwtService } from '@nestjs/jwt'
import { User } from '@prisma/client'
import { UserDTO } from 'src/user/dtos/user.dto'
import { UserService } from '../user/user.service' import { UserService } from '../user/user.service'
@Injectable() @Injectable()
@ -26,12 +28,14 @@ export class AuthService {
async login(user: any) { async login(user: any) {
//console.log(user) //console.log(user)
const payload = { const payload = {
id: user.id,
email: user.email, email: user.email,
name: user.name, name: user.name
roles: [user.role]
} }
// console.log(payload) // console.log(payload)
return { access_token: this.jwtService.sign(payload) } return { access_token: this.jwtService.sign(payload) }
} }
async register(data: UserDTO): Promise<User> {
return this.userService.add(data)
}
} }

View File

@ -129,22 +129,21 @@ export class UserController {
return result return result
} }
@Roles(Role.USER) @Roles(Role.ADMIN)
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@Get(':id') @Get(':id')
async fetchOne(@Param('id') id: string): Promise<User | undefined> { async fetchOne(@Param('id') id: string): Promise<User | undefined> {
return await this.userService.fetchOne({ id: Number(id) }) return await this.userService.fetchOne({ id: Number(id) })
} }
// @Roles(Role.USER) @Roles(Role.ADMIN)
// @UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
//@UseGuards(JwtAuthGuard) @Post('add')
@Post('register')
async add(@Body() data: UserDTO): Promise<User> { async add(@Body() data: UserDTO): Promise<User> {
return await this.userService.add(data) return await this.userService.add(data)
} }
@Roles(Role.USER) @Roles(Role.ADMIN)
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@Put(':id') @Put(':id')
async update(@Param('id') id: string, @Body() data: UserDTO): Promise<User> { async update(@Param('id') id: string, @Body() data: UserDTO): Promise<User> {
@ -155,7 +154,7 @@ export class UserController {
}) })
} }
@Roles(Role.USER) @Roles(Role.ADMIN)
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@Delete(':id') @Delete(':id')
async delete(@Param('id') id: string): Promise<User | undefined> { async delete(@Param('id') id: string): Promise<User | undefined> {

View File

@ -23,7 +23,7 @@ export class UserService {
where?: Prisma.UserWhereInput where?: Prisma.UserWhereInput
}): Promise<number> { }): Promise<number> {
const { cursor, where } = params const { cursor, where } = params
return this.prisma.todo.count({ return this.prisma.user.count({
cursor, cursor,
where where
}) })
@ -38,7 +38,7 @@ export class UserService {
orderBy?: Prisma.UserOrderByWithRelationInput orderBy?: Prisma.UserOrderByWithRelationInput
}): Promise<User[]> { }): Promise<User[]> {
const { skip, take, cursor, where, orderBy } = params const { skip, take, cursor, where, orderBy } = params
return this.prisma.todo.findMany({ return this.prisma.user.findMany({
skip, skip,
take, take,
cursor, cursor,
@ -49,12 +49,12 @@ export class UserService {
//단일 조회 //단일 조회
async fetchOne(where: Prisma.UserWhereUniqueInput): Promise<User | null> { async fetchOne(where: Prisma.UserWhereUniqueInput): Promise<User | null> {
return this.prisma.todo.findUnique({ where }) return this.prisma.user.findUnique({ where })
} }
//단일 추가 //단일 추가
async add(data: Prisma.UserCreateInput): Promise<User> { async add(data: Prisma.UserCreateInput): Promise<User> {
return this.prisma.todo.create({ data }) return this.prisma.user.create({ data })
} }
//단일 수정 //단일 수정
@ -63,7 +63,7 @@ export class UserService {
data: Prisma.UserUpdateInput data: Prisma.UserUpdateInput
}): Promise<User | null> { }): Promise<User | null> {
const { where, data } = params const { where, data } = params
return this.prisma.todo.update({ return this.prisma.user.update({
data, data,
where where
}) })
@ -71,6 +71,6 @@ export class UserService {
//단일 삭제 //단일 삭제
async remove(where: Prisma.UserWhereUniqueInput): Promise<User | null> { async remove(where: Prisma.UserWhereUniqueInput): Promise<User | null> {
return this.prisma.todo.delete({ where }) return this.prisma.user.delete({ where })
} }
} }