✨ 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 { ConfigModule } from '@nestjs/config'
|
||||
import { PrismaModule, loggingMiddleware } from 'nestjs-prisma'
|
||||
import { UsersModule } from './users/users.module'
|
||||
import { AuthModule } from './auth/auth.module'
|
||||
import { JwtAuthGuard } from './common/guards/jwt-auth.guard'
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -24,6 +22,5 @@ import { JwtAuthGuard } from './common/guards/jwt-auth.guard'
|
||||
AuthModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [{ provide: APP_GUARD, useClass: JwtAuthGuard }],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
@ -1,30 +1,28 @@
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common'
|
||||
import { AuthService } from './auth.service'
|
||||
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 { UserEntity } from 'src/users/entities/user.entity'
|
||||
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')
|
||||
@Controller()
|
||||
export class AuthController {
|
||||
constructor(private readonly authService: AuthService) {}
|
||||
|
||||
@SkipAuth()
|
||||
@Post('api/register')
|
||||
async register(@Body() userData: CreateUserDto) {
|
||||
return this.authService.register(userData)
|
||||
}
|
||||
|
||||
@SkipAuth()
|
||||
@Post('api/login')
|
||||
async login(@Body() user: LoginInputDto) {
|
||||
return this.authService.login(user.email, user.password)
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@NeedAuth()
|
||||
@Get('api/profile')
|
||||
async getUserInfo(@User() user: UserEntity) {
|
||||
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