How to get the X-Forwarded-For (IP) header from Passport

Asked

Viewed 60 times

1

I’m using express to create route for my application. To login I have a route to authenticate using Passport and Passport-local. I need to compare the user access IP with the IP of the user that was registered.

router.post('/login', passport.authenticate('local', {
    successRedirect: '/index',
    failureRedirect: '/login?fail=true'
}))

In my file auth.js I have it ...

module.exports = passport => {
passport.use(
    new LocalStrategy({
        usernameField: 'username',
        passwordField: 'password',
        passReqToCallback: true
    },
    (req, username, password, done) => {
        console.log(req.headers)
        findUser(username, (err, user) => {
            if (err) return done(err)
            if (!user) return done(null, false)

            bcrypt.compare(password, user.password, (err, isValid) => {
                if (err) return done(err)
                if (!isValid) return done(null, false)
                return done(null, user)
            })
        })
    })
)}

In the express it is possible to obtain the header of the request made on the route using the first callback parameter request.headers['x-forwarded-for'] but in Passport, on the return of request.headers['x-forwarded-for'] is undefined, then I checked that it does not have the header x-forwarded-for in the Passport request. How can I get it?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.