javascriptnode.jsecmascript-6passport.jspassport-saml

passport.authenticate not working properly with nodejs


I am basically trying to separate out the login methods to a separate file (out from app.js).

This works fine this way:

== app.js ==

app.get("/login", passport.authenticate(passportConfig.config.passport.strategy, 
          {
            successRedirect: "/",
            failureRedirect: "/login"
            })
        );

But when I separate the method to a different file then redirection (or may be authentication doesnot seems to be working)

== New app.js

app.get("/login", authController.loginCtrl)

== AuthenticationController

import PassportConfig from './passportConfig';
import passport from 'passport';
export default class AuthenticationController{
  constructor(){

  }
  loginCtrl(){
        let passportConfig = new PassportConfig();
        var config = passportConfig.config;

        passport.authenticate(config.passport.strategy, {
            successRedirect: "/",
            failureRedirect: "/login"
        });
    }
}

Can any one please help me what I am doing wrong.


Solution

  • passport.authenticate builds a connect middleware that must be passed to app.get. It means loginCtrl must return it.

    app.get("/login", authController.loginCtrl())
    
    loginCtrl(){
        let passportConfig = new PassportConfig();
        var config = passportConfig.config;
    
        return passport.authenticate(config.passport.strategy, {
            successRedirect: "/",
            failureRedirect: "/login"
        });
    }