1

我有一个 Koa.JS 网络应用程序,它使用 Passport.JS 进行 oAuth2 身份验证。这个网络应用程序是用打字稿编写的。这是验证码:

import { deserializeUser, serializeUser, use } from 'koa-passport';
import { Strategy, VerifyCallback } from 'passport-oauth2';
import config from 'config';
import { Authenticator } from 'passport';
const OAuth2Strategy = Strategy;
use(
  new OAuth2Strategy(
    config.get('oauth2'),
    (accessToken: string, refreshToken: string, profile: any, done: VerifyCallback): void => 
    {
      if (accessToken && refreshToken) {
        const tokens = { access_token: accessToken, refresh_token: refreshToken };
        done(null, tokens);
      } else {
        done(undefined, undefined, { message: 'An error occurred!' });
      }
    }
  )
);

serializeUser<Authenticator, Authenticator>(
  (user: Authenticator, done: (err: any, id?: Authenticator | undefined) => void): void => {
    done(null, user);
  }
);

Web 应用程序可以使用 @types/passport 1.0.4 正常编译,但是当我将 @types/passport 的版本升级到 1.0.5 时会生成新错误。

Type 'Authenticator<Handler, any, any, AuthenticateOptions>' does not satisfy the constraint 'IncomingMessage'.
Type 'Authenticator<Handler, any, any, AuthenticateOptions>' is missing the following properties from type 'IncomingMessage': aborted, httpVersion, httpVersionMajor, httpVersionMinor, and 45 more.

serializeUser<Authenticator, Authenticator>

有没有人经历过这个?

4

1 回答 1

1

也许是 Authenticator 界面的变化?你说的话!?是的 - 界面更改,版本号仅略有更改!!!

我希望 koa-passport 的 serializeUser 界面可能与对护照的更新不同步......在我的世界中 - 对界面的重大更改通常意味着主要版本更改。

如下 Passport V1.0.4 允许使用通用 TUser

serializeUser<TUser, TID>(fn: (user: TUser, done: (err: any, id?: TID) => void) => void): void;

现在使用 V1.0.5 的护照,界面更改为限制为 Express.User。serializeUser(fn: (user: Express.User, done: (err: any, id?: TID) => void) => void): void;

于 2021-03-15T23:19:17.450 回答