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

113
Vistas
Passport-Local and Express Router Issue with Imports

I have an Express.js app with a Passport-local Auth, I'm tying to implement express router to my app since it's getting really hard to debug

This is an example of my main file (app.js)

const express = require('express')
const app = express()
const passport = require('passport')
const testing = require('./routes/file')

app.use(passport.initialize())
app.use(passport.session())

app.use('/router', testing)

const initializePassport = require('./passport-config');
initializePassport.initialize(passport);

app.get('/', checkAuthenticated, (req, res) => {
    let user = data.key
    res.send(`Hi ${user}`)
})

app.post('/login', checkNotAuthenticated, passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/login',
    failureFlash: true
}))
  
function checkAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return next()
    }

    res.redirect('/login')
}
  
function checkNotAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return res.redirect('/')
    }
    next()
}

app.listen(3000)

This is what my main file is like, this works perfectly fine and just to make things clear, The function are to check if a user is authenticated or not, also my passport-config returns something called data.key which is basically the username of the user.

Now I've created a router here app.use('/router', testing) with the route as /router for testing purposes

Now this is my file.js that is being used by express

const express = require('express'), router = express.Router();
var bodyParser = require('body-parser');

// Handle POST requests
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());

router.get('/test', checkAuthenticated, (req, res) => {
    let user = data.key;
    res.send(`Hello ${user}, this is coming from the router`)
})

module.exports = router;

Now as expected when I open the site at localhost:3000/router/test it returns a undefined error for data.key since it's not imported and then when I comment that out the line causing the error then checkAuthenticated doesn't work, even when I'm not logged in I can get access to the page, now this is the problem, how do I fix it, I've gone through the entire express router docs I couldn't find luck there.

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your function is not in the global scope you need to set it to global to be able to see it like:

    GLOBAL.authFunction = function(){
    
    }

Suggestion:

1- make a module for auth name it authModule.

2- import the module auth check middleware where ever you want.

about 4 years ago · Juan Pablo Isaza 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