Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从JWT的有效负载获取所有字段_Javascript_Node.js_Jwt_Mean Stack - Fatal编程技术网

Javascript 无法从JWT的有效负载获取所有字段

Javascript 无法从JWT的有效负载获取所有字段,javascript,node.js,jwt,mean-stack,Javascript,Node.js,Jwt,Mean Stack,我已经创建了一个具有登录注销选项的MEAN stack web应用程序。对于要登录的用户,我使用了jsonwebtoken npm包。 这里的主要问题是,当用户登录JWT时,该令牌的负载中包含用户名、电子邮件和全名,但在解码令牌时,我只获得用户名和电子邮件字段 这里是jwt.sign函数- var token=jwt.sign({userid:user.username,email:user.email,fullname:user.fullname},secret,{expiresIn:'24h

我已经创建了一个具有登录注销选项的MEAN stack web应用程序。对于要登录的用户,我使用了jsonwebtoken npm包。 这里的主要问题是,当用户登录JWT时,该令牌的负载中包含用户名、电子邮件和全名,但在解码令牌时,我只获得用户名和电子邮件字段

这里是jwt.sign函数-

var token=jwt.sign({userid:user.username,email:user.email,fullname:user.fullname},secret,{expiresIn:'24h'});
这里是jwt.verify方法-

jwt.verify(token,secret,function(err,decoded){
    if(err){
        res.json({success:false,message:"invalid token"});
    } else {
        req.decoded=decoded;
        next();
    }
})
象征性示例-

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJuaXRpbi5zYWNoZGV2IiwiZW1haWwiOiJuaXRpbi5zYWNoZGV2QGVtYWlsLmNvbSIsImlhdCI6MTQ4NDU1ODAxMiwiZXhwIjoxNDg0NjQ0NDEyfQ.fWogT-aHJY4Xyc8Ebm4OXPkWD3poaWG3IAAf9VS-q58
解码令牌-

{
    "userid": "nitin.sachdev",
    "email": "nitin.sachdev@email.com",
    "iat": 1484558012,
    "exp": 1484644412
}
用户模式-

 var UserSchema=new Schema({
    fullname:String,
    username:{type:String,lowercase:true,required:true,unique:true},
    password:{type:String,required:true},
    email:{type:String,required:true,lowercase:true,unique:true},
    contactno:Number,
    orgname:String
});

我发现了错误,在使用.findOne()方法查找用户时,我忘记在.select()方法中输入fullname作为参数

这是正确的代码-

                                                 //mistake was actually here    
User.findOne({username:req.body.username}).select('email username password fullname').exec(function(err,user){
                //if(err) throw err;
                if(!req.body.password||!req.body.username)
                {
                        res.json({success:false,mesage:"please enter all the fields"});

                }
                else
                {
                    if(!user){
                    res.json({success:false,message:"couldnt find the user"});
                }
                else if(user)
                {
                    if(!req.body.password)
                        res.json({success:false,mesage:"please enter password"});
                    var validPassword=user.comparePassword(req.body.password);
                    if(validPassword){
                        //jwt.sign ismethod to create JWT. 1st par is object containing data that token will contain.
                        console.log(user.fullname);
                        var token=jwt.sign({userid:user.username,email:user.email,fullname:user.orgname},secret,{expiresIn:'24h'});
                        res.json({success:true,message:"loggedin Successfully",token: token});
                    }
                    else
                    {
                        res.json({success:false,message:"wrong password"});
                    }
                }   
                }

            });