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

463
Views
req.headers('Authorization') is undefined

I'm having trouble with this code because token is returning undefined.

In my frontend:

export const fetchUser = async (token: any) => {
        const res = await axios.post('/user/getuser', {
            headers: {Authorization : token}
        });
        return res;
};

And in my middleware is:

const jwt = require('jsonwebtoken');
import {Request, Response, NextFunction} from 'express';

const auth = (req: Request | any, res: Response, next: NextFunction) => {
    try {
        const token = req.headers('Authorization');
        console.log(token);
        if(!token) return res.status(400).json({msg: 'Invalid Authentication.'});

        jwt.verify(token, process.env.ACCESS_TOKEN as string, (err: any, user: any) => {
            if(err) return res.status(400).json({msg: "Invalid Authentication token"})

            req.user = user;

            next();

        });
    } catch (error: any) {
        return res.status(500).json({msg: error.message});
    }
}
module.exports = auth;

I've tested it in Postman and there's no problem, however there's a problem when I make requests from my frontend.

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!