Skip to content

Commit

Permalink
[Fix] Add exception to expired JWT token
Browse files Browse the repository at this point in the history
  • Loading branch information
Cytrogen committed May 11, 2024
1 parent 9c97149 commit 3bda6fe
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions server/src/auth/guards/access-token.guard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CanActivate, ExecutionContext, Inject, Injectable, UnauthorizedException } from '@nestjs/common'
import { ConfigType } from '@nestjs/config'
import { JwtService } from '@nestjs/jwt'
import { JwtService, TokenExpiredError } from '@nestjs/jwt'
import { Reflector } from '@nestjs/core'
import { Request } from 'express'
import { REQUEST_USER_KEY } from '../../common'
Expand Down Expand Up @@ -34,11 +34,17 @@ export class AccessTokenGuard implements CanActivate {
// Extract the JWT token from the request header.
const request = context.switchToHttp().getRequest()
const token = this.extractTokenFromHeader(request)
if (!token) throw new UnauthorizedException()

if (!token) return false
console.log('token', token)
try {
request[REQUEST_USER_KEY] = await this.jwtService.verifyAsync(token, this.jwtConfiguration)
} catch (error) {
console.log(error)

if (error instanceof TokenExpiredError) {
throw new UnauthorizedException('Your session has expired. Please log in again.')
}

throw new UnauthorizedException()
}

Expand Down

0 comments on commit 3bda6fe

Please sign in to comment.