Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

386
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda