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

143
Views
Getting 'undefined' when try to login in a server I created

This is the authenticate.js which will verify the user.

const passport=require('passport');
const LocalStrategy=require('passport-local').Strategy;
const User=require('./models/user');
const JwtStrategy=require('passport-jwt').Strategy;
const ExtractJwt=require('passport-jwt').ExtractJwt;
const jwt=require('jsonwebtoken');

const config=require('./config');

passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

exports.getToken=function(user){
    return jwt.sign(user,config.secretKey,{expiresIn:3600});
};

var opts={};
opts.jwtFromRequest=ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey=config.secretKey;

exports.jwtPassport=passport.use(new JwtStrategy(opts,(jwt_payload,done)=>{
    console.log("JWT payload:",jwt_payload);
    User.findOne({_id:jwt_payload._id},(err,user)=>{
        if(err){
            return done(err,false);
        }
        else if(user){
            return done(null,user);
        }
        else{
            return done(null,false);
        }
    });
}));

exports.verifyUser=passport.authenticate('jwt',{session:false});

exports.verifyAdmin=function(req,res,next){
    User.findOne({_id:req.user._id})
    .then((user)=>{
        console.log("User:",user);
        if (user.admin)
            next();
        else {
            err = new Error('You are not authorized to perform this operation!');
            err.status = 403;
            return next(err);
        }
    },(err)=>next(err))
    .catch((err)=>next(err))
};`

this is user.js /login route where uer can send POST req to login


router.post('/login',cors.corsWithOptions,passport.authenticate('local'),(req,res)=>{
  var token=authenticate.getToken({_id:req.user._id});
  res.statusCode=200;
  res.setHeader('Content-Type', 'application/json');
  res.json({ success: true,token: token, status: 'You are succesfullly logged in!' });

Everytime, I send a request from POSTMAN I get 'undefined' as the response.

It used to work but now POSTMAN shows UNAUTHORIZED on valid credentials.

enter image description here

It used to work but now POSTMAN shows UNAUTHORIZED on valid credentials.

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