gpt4 book ai didi

typescript - TS2339 : Property 'user' does not exist on type 'Request'

转载 作者:行者123 更新时间:2023-12-05 09:37:21 28 4
gpt4 key购买 nike

我正在尝试使用 JwtAuthGuard 来区分经过身份验证的请求和未经身份验证的请求。

我确实关注了 official Nestjs documentation on authentification ,但是,我无法让它工作,因为我收到 typescript 错误: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 }
}
}

最佳答案

因为你没有从 express 导入 Request,只需添加:

import { Request } from 'express';

但是你需要安装 @types/passport@types/express,它会起作用。

关于typescript - TS2339 : Property 'user' does not exist on type 'Request' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64234974/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com