node js jsonwebtoken叹息错误

时间:2016-01-26 04:11:50

标签: node.js jwt json-web-token

我一直在尝试使用promise模式生成jwt。但是我一直在收到像

这样的错误

{[错误:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InN0aW5neW1lbkBnbWFpbC5jb20iLCJpYXQiOjE0NTM3MzAzMzV9.sW5HWYivigBF4UKXdWjeuJLAG3kzytefNEx5lOt6jvY]   原因:[错误:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InN0aW5neW1lbkBnbWFpbC5jb20iLCJpYXQiOjE0NTM3MzAzMzV9.sW5HWYivigBF4UKXdWjeuJLAG3kzytefNEx5lOt6jvY],   isOperational:true}

我生成了带有rsa格式的public.pem并尝试了算法:'RS256',但它也不起作用。

错误信息实际上包含我想要的jwt ..

Login.js

var express = require('express'),
    router = express.Router(),
    rootPath = require('app-root-path'),
    User = require(rootPath+'/app/models/user'),
    authConfig = require(rootPath + '/config/auth'),
    jsonwebtoken = require('jsonwebtoken'),
    Promise = require('bluebird'),
    jwtSign = new Promise.promisify(jsonwebtoken.sign);

router.post('/',function(req,res,next){
    //if body is not empty
    if(req.body==='undefined' && !req.body ){
        res.status(500).send("Invalid register");
    }
    //find user from db
    var newUser;
    User.findOne({email : req.body.email},'email').then(function(user){
        newUser = user;`enter code here`
        return jwtSign({email:user.email},authConfig.getAuthElement('publicKey'),{ algorithm: 'HS256'});
    }).then(function(token){
        newUser.access_token = token;
        newUser.save();
        res.json({data:newUser});
    }).catch(function(error){
        console.log(error);
        res.status(500).send(error);
    });
});

module.exports = router;

请给我一些建议..

0 个答案:

没有答案