0

ネストjsで認証パスポートjwtのシステムを作成しましたが、リクエストから現在のユーザーを抽出できない場合を除き、すべてうまく機能します

これが私の戦略です

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(
     private userService:UserService
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey:"MedAKRAOU",
    });
  }

  async validate(payload:PayLoadInterface) {
      
      if(payload){
          console.log(payload);
          return payload;
      }

    else throw new UnauthorizedException()
  }
}

それは機能し、ペイロードに入れたすべての情報を出力します

これが AuthGuards です。

import { Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
    
}

ガードを使用する別のコントローラーで

@Post()
    @UseGuards(JwtAuthGuard)
   async addCv(@Body() cv:CvDto,@Request() req):Promise<CvEntity>{
     console.log("user extra from req"+req.user)
    return await this.cvServise.addCv(cv)
    }

それは出力します: req[object Object] からの user extra 解決策を見つけるのを手伝ってください

4

0 に答える 0