I'm new to the nodeJS and JWT. I want to create token on registration and confirm it on log in. But it keeps throwing syntax error and I dont know why. Also please if I'm doing anything wrong besides syntax,tell me.
//Add new user A.K.A Registration
app.post('/addUser', (req, res) => {
const addUser = new User({username: req.body.username, password: req.body.password})
addUser.save().then(result => res.status(200).json(result)).catch((err) => console.log(err))
jwt.sign(addUser,'secretKey',{expiresIn:'30h'},(err,token)=>{
res.json({token})
})
})
//Log in A.K.A Sign Up
app.post('/logIn',verifyToken ,(req, res) => {
User.findOne({username: req.body.username, password: req.body.password}).then(result => {
if (result.username === req.body.username && result.password === req.body.password) {
res.status(200).send({
message: 'Successful login'})
jwt.verify(req.token, 'secretkey', (err, authData) => {
if(err) {
res.sendStatus(403);
} else {
res.json({
message: 'Post created...',
authData
});
}
})
} else {
res.status(404).send({
message: 'Invalid Login'
})
}
})
})
you should pass an object to json method
// addUser.save().then(result => res.status(200).json(result))
.then(result => {
res.json({result})
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.