update need-auth decorator
This commit is contained in:
parent
faee8db0f2
commit
8b20f7019e
@ -3,15 +3,11 @@ import { AuthService } from './auth.service'
|
||||
import { CreateUserDto } from 'src/users/dto/create-user.dto'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { LoginInputDto } from './dto/login-input.dto'
|
||||
import { UsersService } from 'src/users/users.service'
|
||||
|
||||
@ApiTags('Auth')
|
||||
@Controller('api/auth')
|
||||
export class AuthController {
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
private readonly userService: UsersService,
|
||||
) {}
|
||||
constructor(private readonly authService: AuthService) {}
|
||||
|
||||
@Post('register')
|
||||
async register(@Body() userData: CreateUserDto) {
|
||||
|
@ -1,11 +1,17 @@
|
||||
import { applyDecorators, UseGuards } from '@nestjs/common'
|
||||
import { applyDecorators, UseGuards, SetMetadata } from '@nestjs/common'
|
||||
import { JwtAuthGuard } from '../guards/jwt-auth.guard'
|
||||
import { ApiBearerAuth, ApiUnauthorizedResponse } from '@nestjs/swagger'
|
||||
|
||||
export function NeedAuth() {
|
||||
return applyDecorators(
|
||||
UseGuards(JwtAuthGuard),
|
||||
ApiBearerAuth(),
|
||||
ApiUnauthorizedResponse({ description: 'Unauthorized' }),
|
||||
)
|
||||
export const NEED_AUTH_KEY = 'need-auth'
|
||||
|
||||
export function NeedAuth(needAuth = true) {
|
||||
if (needAuth) {
|
||||
return applyDecorators(
|
||||
SetMetadata(NEED_AUTH_KEY, needAuth),
|
||||
UseGuards(JwtAuthGuard),
|
||||
ApiBearerAuth(),
|
||||
ApiUnauthorizedResponse({ description: 'Unauthorized' }),
|
||||
)
|
||||
}
|
||||
return new Function()
|
||||
}
|
||||
|
@ -1,4 +0,0 @@
|
||||
import { SetMetadata } from '@nestjs/common'
|
||||
|
||||
export const SKIP_AUTH_KEY = 'skip-auth'
|
||||
export const SkipAuth = () => SetMetadata(SKIP_AUTH_KEY, true)
|
@ -1,12 +0,0 @@
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'
|
||||
import { type Request } from 'express'
|
||||
import { TokenPayload } from 'src/auth/dto/token.dto'
|
||||
|
||||
export const Token = createParamDecorator(
|
||||
(key: keyof TokenPayload, ctx: ExecutionContext) => {
|
||||
const request = ctx.switchToHttp().getRequest<Request>()
|
||||
const token = request.user as TokenPayload
|
||||
|
||||
return key ? token?.[key] : token
|
||||
},
|
||||
)
|
@ -6,7 +6,7 @@ import {
|
||||
} from '@nestjs/common'
|
||||
import { AuthGuard } from '@nestjs/passport'
|
||||
import 'rxjs'
|
||||
import { SKIP_AUTH_KEY } from '../decorators/skip-auth.decorator'
|
||||
import { NEED_AUTH_KEY } from '../decorators/need-auth.decorator'
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
@ -15,14 +15,14 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
}
|
||||
|
||||
canActivate(context: ExecutionContext) {
|
||||
const skipAuth = this.reflector.getAllAndOverride<boolean>(SKIP_AUTH_KEY, [
|
||||
context.getHandler(),
|
||||
const needAuth = this.reflector.getAllAndOverride<boolean>(NEED_AUTH_KEY, [
|
||||
context.getClass(),
|
||||
context.getHandler(),
|
||||
])
|
||||
if (skipAuth) {
|
||||
return true
|
||||
if (needAuth) {
|
||||
return super.canActivate(context)
|
||||
}
|
||||
return super.canActivate(context)
|
||||
return true
|
||||
}
|
||||
|
||||
handleRequest(err, user, info) {
|
||||
|
Loading…
Reference in New Issue
Block a user