Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!