✨ add @NeedAuth Decorator
This commit is contained in:
parent
d32239e53a
commit
0cb2f6797b
@ -1,10 +1,8 @@
|
|||||||
import { APP_GUARD } from '@nestjs/core'
|
|
||||||
import { Logger, Module } from '@nestjs/common'
|
import { Logger, Module } from '@nestjs/common'
|
||||||
import { ConfigModule } from '@nestjs/config'
|
import { ConfigModule } from '@nestjs/config'
|
||||||
import { PrismaModule, loggingMiddleware } from 'nestjs-prisma'
|
import { PrismaModule, loggingMiddleware } from 'nestjs-prisma'
|
||||||
import { UsersModule } from './users/users.module'
|
import { UsersModule } from './users/users.module'
|
||||||
import { AuthModule } from './auth/auth.module'
|
import { AuthModule } from './auth/auth.module'
|
||||||
import { JwtAuthGuard } from './common/guards/jwt-auth.guard'
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -24,6 +22,5 @@ import { JwtAuthGuard } from './common/guards/jwt-auth.guard'
|
|||||||
AuthModule,
|
AuthModule,
|
||||||
],
|
],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [{ provide: APP_GUARD, useClass: JwtAuthGuard }],
|
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
@ -1,30 +1,28 @@
|
|||||||
import { Body, Controller, Get, Post } from '@nestjs/common'
|
import { Body, Controller, Get, Post } from '@nestjs/common'
|
||||||
import { AuthService } from './auth.service'
|
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, ApiBearerAuth } from '@nestjs/swagger'
|
import { ApiTags } from '@nestjs/swagger'
|
||||||
import { LoginInputDto } from './dto/login-input.dto'
|
import { LoginInputDto } from './dto/login-input.dto'
|
||||||
import { UserEntity } from 'src/users/entities/user.entity'
|
import { UserEntity } from 'src/users/entities/user.entity'
|
||||||
import { User } from 'src/common/decorators/user.decorator'
|
import { User } from 'src/common/decorators/user.decorator'
|
||||||
import { SkipAuth } from 'src/common/decorators/skip-auth.decorator'
|
import { NeedAuth } from 'src/common/decorators/auth.decorator'
|
||||||
|
|
||||||
@ApiTags('auth')
|
@ApiTags('auth')
|
||||||
@Controller()
|
@Controller()
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private readonly authService: AuthService) {}
|
constructor(private readonly authService: AuthService) {}
|
||||||
|
|
||||||
@SkipAuth()
|
|
||||||
@Post('api/register')
|
@Post('api/register')
|
||||||
async register(@Body() userData: CreateUserDto) {
|
async register(@Body() userData: CreateUserDto) {
|
||||||
return this.authService.register(userData)
|
return this.authService.register(userData)
|
||||||
}
|
}
|
||||||
|
|
||||||
@SkipAuth()
|
|
||||||
@Post('api/login')
|
@Post('api/login')
|
||||||
async login(@Body() user: LoginInputDto) {
|
async login(@Body() user: LoginInputDto) {
|
||||||
return this.authService.login(user.email, user.password)
|
return this.authService.login(user.email, user.password)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@NeedAuth()
|
||||||
@Get('api/profile')
|
@Get('api/profile')
|
||||||
async getUserInfo(@User() user: UserEntity) {
|
async getUserInfo(@User() user: UserEntity) {
|
||||||
return user
|
return user
|
||||||
|
11
src/common/decorators/auth.decorator.ts
Normal file
11
src/common/decorators/auth.decorator.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { applyDecorators, UseGuards } 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' }),
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user