screenshot of error I recently encountered an issue while implementing JSON web token authentication on my application. The error message displayed was:
" Unhandled rejection TypeError: converting circular structure to JSON "
Upon further investigation, I found that this error occurred during the webtoken generation process, as the error disappeared when I commented out that specific section of the code.
Here is the snippet of the code in question:
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');
}
}
})
});
Removing the let token = jwt.sign(user, app.get("superSecret).. section eliminates the errors. Thank you for any assistance provided.
Below is the snippet of 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;