passport / discuss

A forum for discussing topics related to the usage of Passport.js.
1 stars 0 forks source link

req.isAuthenticated() is always returning false after authenticating with AWS cognito using 'passport' and 'passport-cognito' for strategy #64

Open Anubhav-Nigam opened 2 years ago

Anubhav-Nigam commented 2 years ago

req.isAuthenticated() is returning false even after authenticating with AWS cognito.

The function which I am using for adding login routes is:

addAuthenticationRoutesCognito: function (app) {

app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
  res.setHeader('Cache-control', 'no-store');
  res.setHeader('Pragma', 'no-cache');
  next();
});
passport.serializeUser((user, cb) => {
  cb(null, user);
});
passport.deserializeUser((user, cb) => {
  cb(null, user);
});

let config = {};
let cognitoRedirectUri;
if (process.env.hasOwnProperty("NODE_ENV") && process.env.NODE_ENV === "development") {
  /*in use for development purpose only*/
  cognitoRedirectUri = "http://localhost:3000/";
} else {
  cognitoRedirectUri = process.env.APP_URL;
}

let loginUrl = "https://" + cognitoDomain + ".auth.ap-south-1.amazoncognito.com/login?response_type=token&client_id=" + cognitoAppClientId + "&redirect_uri=" + cognitoRedirectUri;
console.log('COGNITO LOGIN URL = ', loginUrl);

config.userPoolId = cognitoUserPoolId;
config.clientId = cognitoAppClientId;
config.region = cognitoRegion;

let strategy = new CognitoStrategy(config, function(accessToken, idToken, refreshToken, user, cb) {
  // verify callback
  process.nextTick(function() {
    // ...
    cb(null, user);
  });
});

passport.use(strategy);

app.get(loginUrl, passport.authenticate('cognito', {
  successRedirect: landingPageUrl,
  forceLogin: true
}));

app.get("/home", passport.authenticate('cognito'), foundationAuth.getUserDetailsAndJWT.bind(foundationAuth));

}

And for checking if the request is authenticated I am using:

function ensureAuthenticatedCognito(req, res, next) { console.log("is request authenticated ? " + req.isAuthenticated()); console.log("original ULR = " + req.originalUrl); console.log('req====',req);

if (!req.isAuthenticated()) {
let cognitoObj = authManager.getCognitoInstance(); // console.log('cognitoObj.loginUrl==', cognitoObj.loginUrl); authManager.validateUrlAndRedirectCognito(cognitoObj.loginUrl, req, res); // return next(); } else { return next(); } }

The _passport object printed inside res is:

_passport: { instance: Authenticator { _key: 'passport', _strategies: [Object], _serializers: [Array], _deserializers: [Array], _infoTransformers: [], _framework: [Object], _userProperty: 'user', Authenticator: [Function: Authenticator], Passport: [Function: Authenticator], Strategy: [Function], strategies: [Object] } }

getCognitoInstance() returns the login page url of hosted UI of cognito. validateUrlAndRedirectCognito() redirects to that url.

Since the value of '_userProperty' is 'user', req.isAuthenticated() should return true after authentication. Please help me with what is wrong in this code.