美文网首页
Node解析jwt中间件

Node解析jwt中间件

作者: 辻子路 | 来源:发表于2021-11-17 15:03 被阅读0次
export function decodeToken(req, res, next) {
    const base64Url = req.headers.authorization.split('.')[1];
    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    const jsonPayload = decodeURIComponent(
        Buffer.from(base64, 'base64')
            .toString()
            .split('')
            .map(function(c) {
                return `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`;
            })
            .join('')
    );

    const user = JSON.parse(jsonPayload);
    req.user = user;
    next();
}

相关文章

网友评论

      本文标题:Node解析jwt中间件

      本文链接:https://www.haomeiwen.com/subject/nybltrtx.html