2

我一直在尝试使用 promise 模式生成 jwt。但是我不断收到错误

{ [Error: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InN0aW5neW1lbkBnbWFpbC5jb20iLCJpYXQiOjE0NTM3MzAzMzV9.sW5HWYivigBF4UKXdWjeuJLAG3kzytefNEx5lOt6jvY] cause: [Error: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InN0aW5neW1lbkBnbWFpbC5jb20iLCJpYXQiOjE0NTM3MzAzMzV9.sW5HWYivigBF4UKXdWjeuJLAG3kzytefNEx5lOt6jvY], isOperational: true }

我用 rsa 格式生成 public.pem 并尝试了 algorithm: 'RS256' 但它也不起作用。

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

登录.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 = 路由器;

请给我一些建议..

4

0 回答 0