我使用 jsonwebtoken 来解码我的令牌以查看它是否已过期。但是,console.log 返回 null。
var token = response.headers.authorization;
token = token.replace('Bearer','');
var jwt = require('jsonwebtoken');
var decoded = jwt.decode(token);
console.log(decoded);
我不明白,因为我的令牌不为空
看来您正在使用 JWT。要解码这种类型的令牌,您可以简单地使用jwt 解码库 https://github.com/auth0/jwt-decode。例如,在 ReactJS 中:
import jwt from 'jwt-decode' // import dependency
...
// some logic
axios.post(`${axios.defaults.baseURL}/auth`, { email, password })
.then(res => {
const token = res.data.token;
const user = jwt(token); // decode your token here
localStorage.setItem('token', token);
dispatch(actions.authSuccess(token, user));
})
.catch(err => {
dispatch(actions.loginUserFail());
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)