0

screenshot of error I am implementing a json web token authentication on an application and i got the error which says:

" Unhandled rejection TypeError: converting circular structure to JSON "

I further discovered that this error occurs when execution reaches my webtoken generation, because the error didn't occur when i commented it out.

please find my code below:

    const express= require("express");
    const router= express.Router();
    let Users = require("../models/users");
    const jwt= require("jsonwebtoken");
    const configuration = require("../config");
    const app = express();
    app.set("superSecret", configuration.secret);

    //registered user submitting signin form
    router.post("/users/signin", function(req, res, next){
      let confirm;
      Users.findOne({
        where:{ username: req.body.username}
      }).then(user => {
        if(!user){
          res.send("No such users found!")
        }
        else if(user){
           confirmed = user.password === req.body.password;
          if(confirmed){
            let token = jwt.sign(user, app.get("superSecret"), 
                {expiresIn: 1440}); 
                    //expiresInMinutes
                res.json({
                            success: true,
                            message: "enjoy your json",
                            token: token
                          })
          }
          else{
            res.send('incorrect password');
          }
        }
      })
    });

if i comment out the let token = jwt.sign(user, app.get("superSecret).. there would be no errors. Thanks in anticipation.

find below my Users model.

    const Sequelize= require('sequelize')
    const bcrypt = require('bcrypt-nodejs')
    const sequelStorage = new Sequelize('newtrial', 'olatunji', '5432', {
      host: 'localhost',
      dialect: 'postgres',

        pool: {
        max: 5,
        min: 0,
        idle: 10000
      },

    });


    let Users = sequelStorage.define('users', {
      username:{
        type: Sequelize.STRING,
        allowNull: false
      },
      email: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true,
        validate: { isEmail: true}
      },
      password:{
        type: Sequelize.STRING,
        allowNull:false
      },
      admin:{
        type: Sequelize.BOOLEAN,
        allowNull: false,
        default: false
      }
    })

    sequelStorage.sync()
   [enter image description here][1] .catch(function(error){
      console.log(error);
    });

    module.exports= Users;
OlatunjiYSO
  • 98
  • 15

1 Answers1

1

It seems that it's failing to stringify the payload (user object) at this line in a dependency of a dependency

Anyway, it's probably because of sequelize entity wrapper, try calling it like this

let token = jwt.sign(user.toJSON(), app.get("superSecret"), {expiresIn: 1440});

you can check this question for more ways to get the plain object from sequelize model: Sequelize, convert entity to plain object

gafi
  • 12,113
  • 2
  • 30
  • 32