【问题标题】:How to extend passport.js module AuthenticateOptions interface如何扩展 passport.js 模块 AuthenticateOptions 接口
【发布时间】:2019-11-05 04:23:07
【问题描述】:

Passport.js 策略可以支持身份验证调用中的其他选项:

    passport.authenticate('azuread-openidconnect', {
      // Default passport options
      failWithError: true,
      successReturnToOrRedirect: '/',
      // Custom option supported by the azure-ad plugin
      // Type error - 'tenantIdOrName' does not exist in type 'AuthenticateOptions'
      tenantIdOrName: 'common',
    });

使用自定义策略支持的选项,例如上面的tenantIdOrName,会导致打字稿错误,因为它不是护照的AuthenticateOptions 接口found here 的一部分并用于authenticate 签名here

我尝试了一些没有成功的事情

  • 模块扩充,即declare module 'passport' {...} 似乎覆盖了模块的类型而不是扩展它们(不在我的扩展中的任何东西都被视为无类型)
  • 合并接口即declare namespace passport { interface AuthenticateOptions { ...new properties }},这似乎对authenticate方法签名没有影响。

如何在不进行类型转换的情况下支持 authenticate 调用中的其他属性?

【问题讨论】:

    标签: typescript passport.js passport-azure-ad


    【解决方案1】:

    原来我需要为我的模块扩充导入现有模块以扩展模块的类型。

    .d.ts 文件中的以下内容*成功扩展了AuthenticateOptions 接口:

    import { AuthenticateOptions } from 'passport';
    
    declare module 'passport' {
      // Extend acceptable authenticate options for Passport Azure AD
      // https://github.com/AzureAD/passport-azure-ad#513-options-available-for-passportauthenticate
      interface AuthenticateOptions {
        customState?: string;
        resourceURL?: string;
        tenantIdOrName?: string;
        domain_hint?: string;
        login_hint?: string;
        prompt?: string;
      }
    }
    

    *我发现文件必须命名为passport.d.ts,任何其他名称都可以正常工作

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 2012-09-01
      • 2017-09-05
      • 1970-01-01
      • 2022-01-14
      • 2018-08-22
      • 2015-12-20
      • 1970-01-01
      相关资源
      最近更新 更多