Node解析jwt中间件

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中间件)