update need-auth decorator

This commit is contained in:
秦秋旭 2023-02-17 15:04:35 +08:00
parent faee8db0f2
commit 8b20f7019e
5 changed files with 20 additions and 34 deletions

View File

@ -3,15 +3,11 @@ import { AuthService } from './auth.service'
import { CreateUserDto } from 'src/users/dto/create-user.dto' import { CreateUserDto } from 'src/users/dto/create-user.dto'
import { ApiTags } from '@nestjs/swagger' import { ApiTags } from '@nestjs/swagger'
import { LoginInputDto } from './dto/login-input.dto' import { LoginInputDto } from './dto/login-input.dto'
import { UsersService } from 'src/users/users.service'
@ApiTags('Auth') @ApiTags('Auth')
@Controller('api/auth') @Controller('api/auth')
export class AuthController { export class AuthController {
constructor( constructor(private readonly authService: AuthService) {}
private readonly authService: AuthService,
private readonly userService: UsersService,
) {}
@Post('register') @Post('register')
async register(@Body() userData: CreateUserDto) { async register(@Body() userData: CreateUserDto) {

View File

@ -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 { JwtAuthGuard } from '../guards/jwt-auth.guard'
import { ApiBearerAuth, ApiUnauthorizedResponse } from '@nestjs/swagger' import { ApiBearerAuth, ApiUnauthorizedResponse } from '@nestjs/swagger'
export function NeedAuth() { export const NEED_AUTH_KEY = 'need-auth'
return applyDecorators(
UseGuards(JwtAuthGuard), export function NeedAuth(needAuth = true) {
ApiBearerAuth(), if (needAuth) {
ApiUnauthorizedResponse({ description: 'Unauthorized' }), return applyDecorators(
) SetMetadata(NEED_AUTH_KEY, needAuth),
UseGuards(JwtAuthGuard),
ApiBearerAuth(),
ApiUnauthorizedResponse({ description: 'Unauthorized' }),
)
}
return new Function()
} }

View File

@ -1,4 +0,0 @@
import { SetMetadata } from '@nestjs/common'
export const SKIP_AUTH_KEY = 'skip-auth'
export const SkipAuth = () => SetMetadata(SKIP_AUTH_KEY, true)

View File

@ -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
},
)

View File

@ -6,7 +6,7 @@ import {
} from '@nestjs/common' } from '@nestjs/common'
import { AuthGuard } from '@nestjs/passport' import { AuthGuard } from '@nestjs/passport'
import 'rxjs' import 'rxjs'
import { SKIP_AUTH_KEY } from '../decorators/skip-auth.decorator' import { NEED_AUTH_KEY } from '../decorators/need-auth.decorator'
@Injectable() @Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') { export class JwtAuthGuard extends AuthGuard('jwt') {
@ -15,14 +15,14 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
} }
canActivate(context: ExecutionContext) { canActivate(context: ExecutionContext) {
const skipAuth = this.reflector.getAllAndOverride<boolean>(SKIP_AUTH_KEY, [ const needAuth = this.reflector.getAllAndOverride<boolean>(NEED_AUTH_KEY, [
context.getHandler(),
context.getClass(), context.getClass(),
context.getHandler(),
]) ])
if (skipAuth) { if (needAuth) {
return true return super.canActivate(context)
} }
return super.canActivate(context) return true
} }
handleRequest(err, user, info) { handleRequest(err, user, info) {