reactjs - react中如何使用多个axios请求实现jwt token并保存数据
问题描述
如果用户提供电子邮件和密码。它将给出输入:{token:"asdfasd", auth: 'auth"}。如果用户被授权,它将调用第二个 api 提供一定量的数据,否则,它将返回 unauth
最初,我第一次得到两个数据,但是当我刷新时,我没有在减速器中看到我的第二个数据,并且我的身份验证是未经授权的。
compententDidmount(){
this.props.loginuser()
}
export const loginUser = userData => dispatch => {
axios
.get('/api/users/login', params:{userdata:userdata})
.then(res => {
// Save to localStorage
const { token } = res.data;
// Set token to ls
localStorage.setItem('jwtToken', token);
// Set token to Auth header
setAuthToken(token);
// Decode token to get user data
const decoded = jwt_decode(token);
// Set current user
dispatch(setCurrentUser(decoded));
if(res.data.authorized === "authorized"){
axios.get('2nd api', {params:{user:userdata.email}}).then(res=> {
console.log(res.data) //authorized
})} else {
console.log(res.data) // unauthorized }
})};
// Check for token
if (localStorage.jwtToken) {
// Set auth token header auth
setAuthToken(localStorage.jwtToken);
// Decode token and get user info and exp
const decoded = jwt_decode(localStorage.jwtToken);
// Set user and isAuthenticated
store.dispatch(setCurrentUser(decoded));}
刷新浏览器时需要获取两个数据。我该如何解决这个问题?
解决方案
推荐阅读
- c# - 如何解决这个委托?
- php - 具有计数的非聚合列
- javascript - 使用 reduce 并向结果对象添加另一个值
- javascript - 如何从 Google Apps 脚本中的电子邮件线程收集数据?我的收件箱长度重复相同的线程
- r - R中相同ID的多个条目的时间差异
- c# - 如何正确地将身份验证令牌放在 GET 请求的标头中
- reactjs - 如何通过对状态的精细控制来提升功能组件中的状态
- google-apps-script - Google 表格“文本”功能和条件格式
- python - Windows 10 上的 Javascript 安装错误
- python - 为什么范围类的基础中没有 Iterable 但它的对象是 Iterable 实例?