Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
What is the ensureLoggedIn equivilent when using the JWT password Strategy?

I have a project that I want to be able to use OpenIDConnect for session management and also be able to call services using an Authorization header. My current code looks like this...

import expressSession from "express-session";
import oidc from "passport-auth0-openidconnect";
import {ensureLoggedIn} from "connect-ensure-login";
getOpenIdMiddleware(){
  const verify = (issuer, audience, profile, cb)=> cb(null, profile._json);
  const oidcAuth0Config = {
    domain: process.env.AUTH0_DOMAIN,
    clientID: process.env.CLIENT_ID,
    clientSecret: process.env.SECRET,
    callbackURL: process.env.AUTH0_CALLBACK_URL,
  };
  console.log(oidcAuth0Config);
  passport.use(new oidc.Strategy(
    oidcAuth0Config,
    verify,
  ));
  this.type = "auth0-oidc";
}
this.app.get("/user",
  ensureLoggedIn(),
  function(req, res) {
    console.log(`User is  ${JSON.stringify(req.user)}`);
    res.json(req.user || {});
  });

This works great but now I would like something that will work for both. I tried just using passport.authenticate like...

this.app.get("/user",
  passport.authenticate(this.type, {
    scope: "openid email profile",
  }),
  function(req, res) {
    console.log(`User is  ${JSON.stringify(req.user)}`);
    res.json(req.user || {});
  });

But that causes an indefinite loop between /user and /login.

I also tried...

const checkAuthentication = (req, res, next)=>{
  if(req.isAuthenticated || req.isAuthenticated()){
    next();
  } else {
    res.redirect("/login");
  }
}
this.app.get("/user",
  checkAuthentication,
  function(req, res) {
    console.log(`User is  ${JSON.stringify(req.user)}`);
    res.json(req.user || {});
  });

but when I do this req.user is undefined.

What would be an equivalent that would work for both the passport-auth0-openidconnect and the passport-jwt strategy?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda