我是NodeJS的新手。当我尝试通过在标题中使用jwt Web令牌进行身份验证时,我的代码给我一个错误。我无法弄清楚代码出了什么问题。
当我输入jwt令牌作为标题时,我的意图是它应该检索正确的用户详细信息。
我尝试使用邮递员来http://localhost:3000/user/profile URL。我将授权插入到jwt键作为标题。
这是我的路线users.js
const express = require('express');
const router = express.Router();
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const config = require('../config/database');
const passport =require('passport');
//const requireToken = passportService.authenticateJWT;
router.get("",function (req,res){
res.send("hello users");
});
router.post("/register",function (req,res){
const newUser = new User({
username:req.body.username,
name:req.body.name,
email:req.body.email,
password:req.body.password
});
User.saveUser(newUser,function (err,user) {
if(err){
res.json({state:false,msg:"data not inserted"});
}
if(user){
res.json({state:true,msg:"data inserted"});
}
});
});
router.post("/login",function (req,res){
const email = req.body.email;
const password = req.body.password;
// console.log(email);
User.findByEmail(email,function (err,user) {
if(err) throw err;
if (!user){
res.json({state:false,msg:"No user found"});
//return false;
}
User.passwordCheck(password,user.password,function (err,match) {
if (err) throw err;
if (match){
//console.log("combined");
const token = jwt.sign(user.toObject(), config.secret,{expiresIn:86400});
res.json(
{
state:true,
token:'bearer ' + token,
user:{
id:user._id,
name:user.name,
username:user.username,
email:user.email
}
}
)
}
});
});
});
router.get('/profile', passport.authenticate('jwt', { session: false}), function(req, res) {
res.json({user:req.user});
}
);
module.exports = router;
这是我的模型,它是user.js
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const schema = mongoose.Schema;
const userSchema = new schema({
username:{type:String,required:true},
name:{type:String,required:true},
email:{type:String,required:true},
password:{type:String,required:true}
});
module.exports = mongoose.model("User",userSchema);
module.exports.saveUser = function (newUser,callback) {
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
console.log(hash);
newUser.password = hash;
if (err) throw err;
newUser.save(callback);
});
});
};
const User = module.exports = mongoose.model("User",userSchema);
module.exports.saveUser = function (newUser,callback) {
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
if (err) throw err;
newUser.save(callback);
});
});
};
module.exports.findByEmail = function (email,callback) {
const query = {email:email};
User.findOne(query,callback);
};
module.exports.passwordCheck = function (plainpassword,hash,callback) {
bcrypt.compare(plainpassword, hash, function(err, res) {
if(err) throw err;
if (res){
callback(null,res);
} else{
callback(null,false)
}
});
};
module.exports.findUserbyId = function (id,callback) {
User.findOne(id,callback);
};
这是passport.js文件
const JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
const config = require('./database');
const User = require('../models/user');
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('Bearer');
opts.secretOrKey = config.secret ;
module.exports = function (passport) {
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
User.findUserbyId({_id: jwt_payload._doc._id}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false);
}
});
}));
}
时,这是错误
我不知道这是怎么了。由于我是NodeJ的新手,请帮助解决此问题。