代码之家  ›  专栏  ›  技术社区  ›  wero026

路由控制器框架中的passport.authenticate()

  •  0
  • wero026  · 技术社区  · 5 年前

    我是javascript/typescript开发的新手,目前正在使用单点登录扩展一个express应用程序。express应用程序使用路由控制器框架处理请求,并应使用passport saml进行身份验证。 我已经设法使身份验证与标准快速路由一起工作:

    export class SsoRoutes {
        public router: Router;
    
        constructor() {
            this.router = Router();
        }
    
    
        this.router.get('/login-sso', passport.authenticate('saml'));
    
        this.router.post('/login-sso/consume', passport.authenticate('saml', {
            failureRedirect: '/',
            failureFlash: true,
            session: false
        }), function (req, res) {
            // handle callback
    
        });
    }
    

    但我不知道如何使用 passport.authenticate(...) 方法中的。 有人能给我解释一下吗?

    0 回复  |  直到 5 年前
        1
  •  0
  •   Batuzz    5 年前

    我选择的解决方案是创建自己的中间件,它将处理 passport.authenticate() (看看怎么做 here )然后您可以使用自己的中间件 @UseBefore() 装饰者。

    @Get("/login-sso")
    @UseBefore(yourFirstMiddleware)
    loginSso() {
        // ... something you want to do after passport.authenticate()
    }
    

    第二个端点类似:

    @Post("/login-sso/consume")
    @UseBefore(yourSecondMiddleware)
    loginSso() {
        // ... some other action you want to do after
    }
    

    其他解决方案检查 documentation 你正在使用的框架。