用户登录时如何在本地存储中存储访问令牌和userId? | React redux

问题描述 投票:-3回答:1

我想在用户登录时将用户访问令牌和用户ID存储在本地存储中,也存储在redux存储中。当用户登录时,我们将从API接收访问令牌和userId。

提前致谢。

javascript
1个回答
1
投票

保存到本地存储并将其设置为auth标头

// Get token
const { token } = res.data;

// Set token to local storage
// Note: Local storage only stores strings
localStorage.setItem("jwtToken", token);

// Set token to Auth header - if you are using axios
axios.defaults.headers.common["Authorization"] = token;
© www.soinside.com 2019 - 2024. All rights reserved.