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

60
Vistas
JavaScript convert [object Object] to JSON

I am implementing a token based authentication and I need to access the token I have set when i check if the user is authenticated.

I am using this method to check if the user is authenticated:

function isAuthenticated(req, res, next) {
    const token = req.headers['authorization'];
    console.log(token);
    if (token) {
        req.token = token;
        next();
    } else {
        res.sendStatus(401);
    }
}

The console.log(token) prints out [object Object] how can I convert this to a json object?

The token is generated with the jsonwebtoken module this way:

jwt.sign({ user_id: user.user_id }, config.app.secretKey, { expiresIn: 60 * 60 * 24 * 7 }, (err, token) => {
    return res.send(token);
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Unfortunately, the string "[object Object]" doesn't have enough information to recreate back the original object. You'll have to debug the logic and find out why req.headers['authorization'] has such value in the first place.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use JSON.stringify

function isAuthenticated(req, res, next) {
    const token = req.headers['authorization'];
    console.log(JSON.stringify(token));
    if (token) {
        req.token = token;
        next();
    } else {
        res.sendStatus(401);
    }
}
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