Skip to content

Commit

Permalink
fix:解决存在执行刷新accessToken操作时,取到cookie里面的值时refreshToken为空的情况导致报错!
Browse files Browse the repository at this point in the history
cookie 设置的key:authorized-token 对象增加refreshToken
  • Loading branch information
fancyfff authored Jan 22, 2024
1 parent b13d745 commit bd79e23
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export function getToken(): DataInfo<number> {
/**
* @description 设置`token`以及一些必要信息并采用无感刷新`token`方案
* 无感刷新:后端返回`accessToken`(访问接口使用的`token`)、`refreshToken`(用于调用刷新`accessToken`的接口时所需的`token`,`refreshToken`的过期时间(比如30天)应大于`accessToken`的过期时间(比如2小时))、`expires`(`accessToken`的过期时间)
* 将`accessToken`、`expires`这两条信息放在key值为authorized-token的cookie里(过期自动销毁)
* 将`accessToken`、`expires`、`refreshToken`这三条信息放在key值为authorized-token的cookie里(过期自动销毁)
* 将`username`、`roles`、`refreshToken`、`expires`这四条信息放在key值为`user-info`的localStorage里(利用`multipleTabsKey`当浏览器完全关闭后自动销毁)
*/
export function setToken(data: DataInfo<Date>) {
let expires = 0;
const { accessToken, refreshToken } = data;
const { isRemembered, loginDay } = useUserStoreHook();
expires = new Date(data.expires).getTime(); // 如果后端直接设置时间戳,将此处代码改为expires = data.expires,然后把上面的DataInfo<Date>改成DataInfo<number>即可
const cookieString = JSON.stringify({ accessToken, expires });
const cookieString = JSON.stringify({ accessToken, expires, refreshToken });

expires > 0
? Cookies.set(TokenKey, cookieString, {
Expand Down

0 comments on commit bd79e23

Please sign in to comment.