gpt4 book ai didi

reactjs - 解码 Jwt token react

转载 作者:行者123 更新时间:2023-12-04 04:28:08 25 4
gpt4 key购买 nike

我使用 jsonwebtoken 来解码我的 token 以查看它是否已过期。但是,console.log 返回 null。

 var token = response.headers.authorization;
token = token.replace('Bearer','');
var jwt = require('jsonwebtoken');
var decoded = jwt.decode(token);
console.log(decoded);

我不明白,因为我的 token 不为空

最佳答案

看起来你在使用 JWT。要解码这种类型的 token ,您可以简单地使用 jwt-decode library .例如在 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());
});

关于reactjs - 解码 Jwt token react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53835816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com