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 */
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 { JwtAuthGuard } from './guards/jwt.authguard'
import { LocalAuthGuard } from './guards/local-auth.guard'
@ -27,11 +29,17 @@ export class AuthController {
return response
}
//Login여부 확인용
//Profile 여부 확인용
@UseGuards(JwtAuthGuard)
@Get('profile')
getProfile(@Request() req) {
//console.log(req)
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 */
import { Injectable } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { User } from '@prisma/client'
import { UserDTO } from 'src/user/dtos/user.dto'
import { UserService } from '../user/user.service'
@Injectable()
@ -26,12 +28,14 @@ export class AuthService {
async login(user: any) {
//console.log(user)
const payload = {
id: user.id,
email: user.email,
name: user.name,
roles: [user.role]
name: user.name
}
// console.log(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
}
@Roles(Role.USER)
@Roles(Role.ADMIN)
@UseGuards(JwtAuthGuard, RolesGuard)
@Get(':id')
async fetchOne(@Param('id') id: string): Promise<User | undefined> {
return await this.userService.fetchOne({ id: Number(id) })
}
// @Roles(Role.USER)
// @UseGuards(JwtAuthGuard, RolesGuard)
//@UseGuards(JwtAuthGuard)
@Post('register')
@Roles(Role.ADMIN)
@UseGuards(JwtAuthGuard, RolesGuard)
@Post('add')
async add(@Body() data: UserDTO): Promise<User> {
return await this.userService.add(data)
}
@Roles(Role.USER)
@Roles(Role.ADMIN)
@UseGuards(JwtAuthGuard, RolesGuard)
@Put(':id')
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)
@Delete(':id')
async delete(@Param('id') id: string): Promise<User | undefined> {

View File

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