I am trying to use JwtAuthGuard to distinguish authenticated requests from the unauthenticated ones.
I did follow the official Nestjs documentation on authentification, yet, I can't make it work as I get a typescript error: TS2339: Property 'user' does not exist on type 'Request'.
bookmarks.controller.ts
import { Controller, Get, Req, UseGuards } from '@nestjs/common'
import { BookmarksService } from './bookmarks.service'
import { StandardSuccessResponse } from '../utils/standard-response.type'
import { ResponseMessage } from '../utils/response-messages.enum'
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
@Controller('v1/bookmarks')
export class BookmarksController {
constructor(private bookmarksService: BookmarksService) {}
@UseGuards(JwtAuthGuard)
@Get()
async getAllBookmarkedJobs(
@Req() request: Request
): Promise<StandardSuccessResponse<string[]>> {
return {
success: true,
data: await this.bookmarksService.getAllBookmarkedJobs(
request.user.candidateId
),
meta: null,
message: ResponseMessage.BOOKMARKS_RETRIEVED,
}
}
}
strategies/jwt.strategy.ts
import { ExtractJwt, Strategy } from 'passport-jwt'
import { PassportStrategy } from '@nestjs/passport'
import { Injectable } from '@nestjs/common'
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: process.env.JWT_SECRET,
})
}
async validate(payload: any) {
return { candidateId: payload.sub }
}
}
The
user
property is attached to theRequest
object via@types/passport
, or by using a custom interface thatextends Request
and adds the type to it yourself, like so: