nestjs_auth init..
This commit is contained in:
parent
0135d94b55
commit
0d260d6849
10
.env
10
.env
@ -8,9 +8,15 @@ DATABASE_URL="mysql://root:@localhost:3306/test"
|
|||||||
CORS_ALLOW_ORIGINS = ['http://localhost:8080']
|
CORS_ALLOW_ORIGINS = ['http://localhost:8080']
|
||||||
CORS_ALLOW_METHOD = "GET,PUT,POST,DELETE,PATCH,OPTIONS"
|
CORS_ALLOW_METHOD = "GET,PUT,POST,DELETE,PATCH,OPTIONS"
|
||||||
|
|
||||||
JWT_SECURITY_KEY = "security_key"
|
|
||||||
JWT_EXPIRE_MAX = "600s"
|
|
||||||
AUTH_USERNAME_FIELD="email"
|
AUTH_USERNAME_FIELD="email"
|
||||||
|
|
||||||
|
JWT_REFRESH_TOKEN_SECRET = "security_key"
|
||||||
|
JWT_REFRESH_TOKEN_EXPIREIN = "14d"
|
||||||
|
JWT_REFRESH_TOKEN_ISSUER = "idcjp"
|
||||||
|
|
||||||
|
JWT_ACCESS_TOKEN_SECRET = "security_key"
|
||||||
|
JWT_ACCESS_TOKEN_EXPIREIN = "60s"
|
||||||
|
JWT_ACCESS_TOKEN_ISSUER = "idcjp"
|
||||||
|
|
||||||
DEFAULT_TABLE_PERPAGE = 10
|
DEFAULT_TABLE_PERPAGE = 10
|
||||||
DEFAULT_TABLE_PAGE = 1
|
DEFAULT_TABLE_PAGE = 1
|
||||||
@ -3,8 +3,8 @@ import { Body, Controller, Get, Post, Request, UseGuards } from '@nestjs/common'
|
|||||||
import { User } from '@prisma/client'
|
import { User } from '@prisma/client'
|
||||||
import { UserDTO } from 'src/user/dtos/user.dto'
|
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.auth.guard'
|
||||||
import { LocalAuthGuard } from './guards/local-auth.guard'
|
import { LocalAuthGuard } from './guards/local.auth.guard'
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
@ -13,17 +13,16 @@ export class AuthController {
|
|||||||
//local.strategy.ts 사용
|
//local.strategy.ts 사용
|
||||||
// @UseGuards(AuthGuard('local'))
|
// @UseGuards(AuthGuard('local'))
|
||||||
// @UseGuards(LocalAuthGuard)
|
// @UseGuards(LocalAuthGuard)
|
||||||
// @Post('login')
|
// @Post('/local/login')
|
||||||
// async login(@Request() req) {
|
// async login(@Request() req) {
|
||||||
// return req.user
|
// return req.user
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//Login용
|
//Login용
|
||||||
//local-auth.guard.ts 사용
|
|
||||||
@UseGuards(LocalAuthGuard)
|
@UseGuards(LocalAuthGuard)
|
||||||
@Post('login')
|
@Post('login')
|
||||||
async login(@Request() req) {
|
async login(@Request() req) {
|
||||||
console.log(req.user)
|
//console.log(req.user)
|
||||||
const response = this.authService.login(req.user)
|
const response = this.authService.login(req.user)
|
||||||
console.log(response)
|
console.log(response)
|
||||||
return response
|
return response
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import { Module } from '@nestjs/common'
|
|||||||
import { PassportModule } from '@nestjs/passport'
|
import { PassportModule } from '@nestjs/passport'
|
||||||
import { AuthService } from './auth.service'
|
import { AuthService } from './auth.service'
|
||||||
import { JwtModule } from '@nestjs/jwt'
|
import { JwtModule } from '@nestjs/jwt'
|
||||||
import { jwtConstants } from './guards/constants'
|
import { jwtAcceesTokenTypes } from './guards/jwt.constants'
|
||||||
import { AuthController } from './auth.controller'
|
import { AuthController } from './auth.controller'
|
||||||
import { LocalStrategy } from './guards/local.strategy'
|
import { LocalStrategy } from './guards/local.strategy'
|
||||||
import { JwtStrategy } from './guards/jwt.strategy'
|
import { JwtStrategy } from './guards/jwt.strategy'
|
||||||
@ -17,8 +17,8 @@ import { UsersModule } from '../user/user.module'
|
|||||||
UsersModule,
|
UsersModule,
|
||||||
PassportModule,
|
PassportModule,
|
||||||
JwtModule.register({
|
JwtModule.register({
|
||||||
secret: jwtConstants.secret,
|
secret: jwtAcceesTokenTypes.secret,
|
||||||
signOptions: { expiresIn: jwtConstants.expiresIn }
|
signOptions: { expiresIn: jwtAcceesTokenTypes.expiresIn }
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
controllers: [AuthController],
|
controllers: [AuthController],
|
||||||
|
|||||||
@ -29,10 +29,12 @@ export class AuthService {
|
|||||||
//console.log(user)
|
//console.log(user)
|
||||||
const payload = {
|
const payload = {
|
||||||
email: user.email,
|
email: user.email,
|
||||||
name: user.name
|
name: user.name,
|
||||||
|
role: user.role,
|
||||||
|
access_token: this.jwtService.sign(payload)
|
||||||
}
|
}
|
||||||
// console.log(payload)
|
// console.log(payload)
|
||||||
return { access_token: this.jwtService.sign(payload) }
|
return { }
|
||||||
}
|
}
|
||||||
|
|
||||||
async register(data: UserDTO): Promise<User> {
|
async register(data: UserDTO): Promise<User> {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { SetMetadata } from '@nestjs/common'
|
import { SetMetadata } from '@nestjs/common'
|
||||||
import { Role } from '../guards/role.enum'
|
import { Role } from './role.enum'
|
||||||
|
|
||||||
export const ROLES_KEY = 'roles'
|
export const ROLES_KEY = 'roles'
|
||||||
export const Roles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles)
|
export const Roles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles)
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
import { env } from 'process'
|
|
||||||
|
|
||||||
export const jwtConstants = {
|
|
||||||
secret: env.JWT_SECURITY_KEY,
|
|
||||||
expiresIn: env.JWT_EXPIRE_MAX
|
|
||||||
}
|
|
||||||
11
src/auth/guards/jwt.constants.ts
Normal file
11
src/auth/guards/jwt.constants.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export const jwtAcceesTokenTypes = {
|
||||||
|
secret: process.env.JWT_ACCESS_TOKEN_SECRET,
|
||||||
|
expiresIn: process.env.JWT_ACCESS_TOKEN_EXPIREIN,
|
||||||
|
issuer: process.env.JWT_ACCESS_TOKEN_ISSUER
|
||||||
|
}
|
||||||
|
|
||||||
|
export const jwtRefreshTokenTypes = {
|
||||||
|
secret: process.env.JWT_REFRESH_TOKEN_SECRET,
|
||||||
|
expiresIn: process.env.JWT_REFRESH_TOKEN_EXPIREIN,
|
||||||
|
issuer: process.env.JWT_REFRESH_TOKEN_ISSUER
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import { ExtractJwt, Strategy } from 'passport-jwt'
|
import { ExtractJwt, Strategy } from 'passport-jwt'
|
||||||
import { PassportStrategy } from '@nestjs/passport'
|
import { PassportStrategy } from '@nestjs/passport'
|
||||||
import { Injectable } from '@nestjs/common'
|
import { Injectable } from '@nestjs/common'
|
||||||
import { jwtConstants } from './constants'
|
import { jwtAcceesTokenTypes } from './jwt.constants'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||||
@ -9,16 +9,14 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|||||||
super({
|
super({
|
||||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||||
ignoreExpiration: false,
|
ignoreExpiration: false,
|
||||||
secretOrKey: jwtConstants.secret
|
secretOrKey: jwtAcceesTokenTypes.secret
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async validate(payload: any) {
|
async validate(payload: any) {
|
||||||
return {
|
return {
|
||||||
id: payload.id,
|
|
||||||
email: payload.email,
|
email: payload.email,
|
||||||
name: payload.name,
|
name: payload.name
|
||||||
roles: payload.roles
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,14 +3,13 @@ import { Strategy } from 'passport-local'
|
|||||||
import { PassportStrategy } from '@nestjs/passport'
|
import { PassportStrategy } from '@nestjs/passport'
|
||||||
import { Injectable, UnauthorizedException } from '@nestjs/common'
|
import { Injectable, UnauthorizedException } from '@nestjs/common'
|
||||||
import { AuthService } from '../auth.service'
|
import { AuthService } from '../auth.service'
|
||||||
import { env } from 'process'
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LocalStrategy extends PassportStrategy(Strategy) {
|
export class LocalStrategy extends PassportStrategy(Strategy) {
|
||||||
constructor(private authService: AuthService) {
|
constructor(private authService: AuthService) {
|
||||||
//super()
|
//super()
|
||||||
//If you want to check user authenticate with custom column like 'email', try pass it.
|
//If you want to check user authenticate with custom column like 'email', try pass it.
|
||||||
super({ usernameField: env.AUTH_USERNAME_FIELD })
|
super({ usernameField: process.env.AUTH_USERNAME_FIELD })
|
||||||
}
|
}
|
||||||
|
|
||||||
async validate(email: string, password: string): Promise<any> {
|
async validate(email: string, password: string): Promise<any> {
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
|
|||||||
import { Reflector } from '@nestjs/core'
|
import { Reflector } from '@nestjs/core'
|
||||||
import { Observable } from 'rxjs'
|
import { Observable } from 'rxjs'
|
||||||
import { ROLES_KEY } from '../decorators/roles.decorator'
|
import { ROLES_KEY } from '../decorators/roles.decorator'
|
||||||
import { Role } from './role.enum'
|
import { Role } from '../decorators/role.enum'
|
||||||
|
|
||||||
//참고: https://shpota.com/2022/07/16/role-based-authorization-with-jwt-using-nestjs.html
|
//참고: https://shpota.com/2022/07/16/role-based-authorization-with-jwt-using-nestjs.html
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
|||||||
@ -14,8 +14,8 @@ import { User } from '@prisma/client'
|
|||||||
import { UserDTO } from './dtos/user.dto'
|
import { UserDTO } from './dtos/user.dto'
|
||||||
import { UserService } from './user.service'
|
import { UserService } from './user.service'
|
||||||
import { Roles } from '../auth/decorators/roles.decorator'
|
import { Roles } from '../auth/decorators/roles.decorator'
|
||||||
import { JwtAuthGuard } from '../auth/guards/jwt.authguard'
|
import { JwtAuthGuard } from '../auth/guards/jwt.auth.guard'
|
||||||
import { Role } from '../auth/guards/role.enum'
|
import { Role } from '../auth/decorators/role.enum'
|
||||||
import { RolesGuard } from '../auth/guards/roles.guard'
|
import { RolesGuard } from '../auth/guards/roles.guard'
|
||||||
|
|
||||||
@Controller('user')
|
@Controller('user')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user