✨ add skip-auth.decorator
This commit is contained in:
parent
205689e130
commit
93127d2bd6
src
@ -1,8 +1,10 @@
|
||||
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: [
|
||||
@ -22,6 +24,6 @@ import { AuthModule } from './auth/auth.module'
|
||||
AuthModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [],
|
||||
providers: [{ provide: APP_GUARD, useClass: JwtAuthGuard }],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
@ -5,17 +5,20 @@ 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'
|
||||
|
||||
@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)
|
||||
|
4
src/common/decorators/skip-auth.decorator.ts
Normal file
4
src/common/decorators/skip-auth.decorator.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { SetMetadata } from '@nestjs/common'
|
||||
|
||||
export const SKIP_AUTH_KEY = 'skip-auth'
|
||||
export const SkipAuth = () => SetMetadata(SKIP_AUTH_KEY, true)
|
@ -1,3 +1,4 @@
|
||||
import { Reflector } from '@nestjs/core'
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
@ -5,12 +6,22 @@ import {
|
||||
} from '@nestjs/common'
|
||||
import { AuthGuard } from '@nestjs/passport'
|
||||
import 'rxjs'
|
||||
import { SKIP_AUTH_KEY } from '../decorators/skip-auth.decorator'
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
constructor(private reflector: Reflector) {
|
||||
super()
|
||||
}
|
||||
|
||||
canActivate(context: ExecutionContext) {
|
||||
// Add your custom authentication logic here
|
||||
// for example, call super.logIn(request) to establish a session.
|
||||
const isPublic = this.reflector.getAllAndOverride<boolean>(SKIP_AUTH_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
])
|
||||
if (isPublic) {
|
||||
return true
|
||||
}
|
||||
return super.canActivate(context)
|
||||
}
|
||||
|
||||
|
@ -4,15 +4,12 @@ import { ConfigService } from '@nestjs/config'
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
|
||||
import { PrismaClientExceptionFilter, PrismaService } from 'nestjs-prisma'
|
||||
import { AppModule } from './app.module'
|
||||
import { JwtAuthGuard } from './common/guards/jwt-auth.guard'
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule)
|
||||
|
||||
// Validation
|
||||
app.useGlobalPipes(new ValidationPipe({ whitelist: true }))
|
||||
// Global Guard
|
||||
app.useGlobalGuards(new JwtAuthGuard())
|
||||
|
||||
// enable shutdown hook
|
||||
const prismaService = app.get(PrismaService)
|
||||
|
Loading…
Reference in New Issue
Block a user