“类型错误:OAuth2Strategy 需要验证回调。”但我发送了(nestjs)

问题描述 投票:0回答:2

我在app.controller.ts中调用OAuth2Strategy的构造函数,我向它发送了它需要的2个参数:带有clientID、callbackURL等的选项,以及一个验证回调函数。但我有这个错误,看起来我没有向构造函数发送验证回调函数,但我做到了。错误发生在node_modules/passport-oauth2/lib/strategy.js中。

app.controller.ts:

import { Body, Controller, Get, Post, Query, Res, Req, Next, UnauthorizedException, UseGuards } from '@nestjs/common';
import { PartieService, JoueurService, CanalService } from './app.service';
import { Joueur, Partie, Canal } from './app.entity';
import { AuthGuard } from '@nestjs/passport';
import passport = require("passport");
import OAuth2Strategy = require("passport-oauth2");
//var OAuth2Strategy = require('passport-oauth2').OAuth2Strategy;
//import OAuth2Strategy from 'passport-oauth2';

@Controller()
export class AppController {
  constructor(private readonly partieService: PartieService, private readonly joueurService: JoueurService,
    private readonly canalService: CanalService) {}

  @Get('/oauth2')
  async oauth2(@Req() req, @Res() res, @Next() next) {
    passport.use('oauth2', new OAuth2Strategy( 
      {
                clientID: 'my_clientID',
                clientSecret: 'my_clientSecret',
                authorizationURL: 'https://api.abc.com/oauth/authorize',
                tokenURL: 'https://api.abc.fr/oauth/token',
                callbackURL: 'http://localhost:3000/Connection'
            }, 
      async (accessToken: string, refreshToken: string, profile: any, done: Function) => {
                    //console.log(profile);
                    try {
                        if (!accessToken || !refreshToken || !profile) {
                            return done(new UnauthorizedException(), false);
                        }
                        const user: Joueur = await this.joueurService.findOrCreate(profile);
                        return done(null, user);
                    } catch (err) {
                        return done(new UnauthorizedException(), false);
                    }
                } ));
    passport.authenticate('oauth2');
    return res.sendFile("/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/public/connection.html");
  }

  @Get('/Connection')
  //@UseGuards(AuthGuard('oauth2')) 
  async Callback(@Req() req, @Res() res, @Next() next) {
    passport.authenticate('oauth2', (err, user, info) => {
      if (err || !user) {
        req.session.destroy();
        return res.status(401).json({
          message: 'Unauthorized',
        });
      }
      req.user = user;
      return next();
    })(req, res, next);

    const utilisateur: Joueur = req.user;
    console.log(utilisateur);
  }
[...]
}

node_modules/passport-oauth2/lib/strategy.js:

function OAuth2Strategy(options, verify) {
  if (typeof options == 'function') {
    verify = options;
    options = undefined;
  }
  options = options || {};

  if (!verify) { throw new TypeError('OAuth2Strategy requires a verify callback'); }
[...]
}

错误:

[Nest] 6550  - 05/02/2023, 18:34:54   ERROR [ExceptionHandler] OAuth2Strategy requires a verify callback
TypeError: OAuth2Strategy requires a verify callback
    at new OAuth2Strategy (/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/passport-oauth2/lib/strategy.js:84:24)
    at Injector.instantiateClass (/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/@nestjs/core/injector/injector.js:340:19)
    at callback (/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/@nestjs/core/injector/injector.js:53:45)
    at Injector.resolveConstructorParams (/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/@nestjs/core/injector/injector.js:132:24)
    at Injector.loadInstance (/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/@nestjs/core/injector/injector.js:57:13)
    at Injector.loadProvider (/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/@nestjs/core/injector/injector.js:84:9)
    at async Promise.all (index 3)
    at InstanceLoader.createInstancesOfProviders (/home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/@nestjs/core/injector/instance-loader.js:47:9)
    at /home/user42/Documents/Projets/ft_transcendence/services/pong/pong/node_modules/@nestjs/core/injector/instance-loader.js:32:13
    at async Promise.all (index 1)

`

我多次尝试更改导入 OAuth2Strategy (如注释行)。我尝试了很多事情,但我无法记住所有事情。我在互联网上没有找到答案,也没有找到更多的不太好的chatgpt。 我是 Nestjs 的新手,这个错误对我来说很奇怪,它如何询问我发送的参数?有人可以帮我解决这个问题吗? :)

抱歉,如果答案很明显:/,这是我使用 Nestjs 的第一个 API

oauth-2.0 callback nestjs passport.js nestjs-passport
2个回答
0
投票

请问您是如何解决这里的问题的?


-1
投票

错误来自另一个文件,我的错,它已修复。

© www.soinside.com 2019 - 2024. All rights reserved.