✨ use global jwt-auth.guard
This commit is contained in:
parent
2d2365fe7a
commit
205689e130
@ -1,9 +1,8 @@
|
||||
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common'
|
||||
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 { JwtAuthGuard } from './jwt-auth.guard'
|
||||
import { UserEntity } from 'src/users/entities/user.entity'
|
||||
import { User } from 'src/common/decorators/user.decorator'
|
||||
|
||||
@ -22,7 +21,6 @@ export class AuthController {
|
||||
return this.authService.login(user.email, user.password)
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('api/profile')
|
||||
async getUserInfo(@User() user: UserEntity) {
|
||||
return user
|
||||
|
@ -3,7 +3,7 @@ import { AuthService } from './auth.service'
|
||||
import { AuthController } from './auth.controller'
|
||||
import { UsersModule } from 'src/users/users.module'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { JwtStrategy } from './jwt.strategy'
|
||||
import { JwtStrategy } from './strategies/jwt.strategy'
|
||||
|
||||
@Module({
|
||||
controllers: [AuthController],
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Injectable, BadRequestException } from '@nestjs/common'
|
||||
import { PasswordService } from 'src/users/password.service'
|
||||
import { Token } from './models/token.model'
|
||||
import { Token } from './dto/token.dto'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { ConfigService } from '@nestjs/config'
|
||||
import { UsersService } from 'src/users/users.service'
|
||||
|
@ -7,11 +7,3 @@ export class Token {
|
||||
@ApiProperty()
|
||||
refreshToken: string
|
||||
}
|
||||
|
||||
export class TokenPayload {
|
||||
userId: string
|
||||
/** Issued at */
|
||||
iat: number
|
||||
/** Expiration time */
|
||||
exp: number
|
||||
}
|
@ -3,7 +3,6 @@ import { PassportStrategy } from '@nestjs/passport'
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common'
|
||||
import { ConfigService } from '@nestjs/config'
|
||||
import { User } from '@prisma/client'
|
||||
import { TokenPayload } from './models/token.model'
|
||||
import { PrismaService } from 'nestjs-prisma'
|
||||
|
||||
@Injectable()
|
||||
@ -18,7 +17,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
})
|
||||
}
|
||||
|
||||
async validate(payload: TokenPayload): Promise<User> {
|
||||
async validate(payload: { userId: string }): Promise<User> {
|
||||
const user = await this.prismaService.user.findUnique({
|
||||
where: { id: payload.userId },
|
||||
})
|
@ -4,12 +4,15 @@ 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