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

384
Visualizações
Passportjs multiple authentication strategies external file with expressjs

First off if I am asking a obviously stupid question I apologies in advance.

I have a passport authentication strategy setup currently and it is working ok. The implementation is as follows.

Authentication strategy (authentication.js) :

const passport = require("passport");
const passportJWT = require("passport-jwt");
const params = {
    //Params here
};


module.exports = function LocalStrategy() {
let strategy = new Strategy(params, function (payload, done) {
    //Logic here
});
passport.use(strategy);
return {
    initialize: function () {
        return passport.initialize();
    },
    authenticate: function () {
        return passport.authenticate("jwt", {
            session: false
        });
    }
  };
};

Using in a route :

const localAuth = require('./authentication/LocalStrategy')();

app.get('/test', localAuth.authenticate(), (req, res) => {
    res.json(req.isAuthenticated());
});

In the server.js file

const localAuth = require('./authentication/LocalStrategy')();
app.use(localAuth.initialize());

I am planning to use multiple authentication strategies in a single route and I found this implementation. But rather than having the authentication strategy written in the same server.js I want to have the strategy written in an external file (in my case authentication.js) and refer the strategy in the route as

passport.authenticate(['SOME_OTHER_STRATEGY', 'jwt']

How can I implement this?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Ok apparently I was't trying hard enough, I didn't have to do any change to my current logic other than serializeUser and deserializeUser. and just use:

passport.authenticate(['SOME_OTHER_STRATEGY', 'jwt']) 

that't it.

about 4 years ago · Santiago Trujillo Relatório
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