改进 刷新失败的情况
This commit is contained in:
parent
34630760e3
commit
ac3f01a11d
@ -1,6 +1,9 @@
|
|||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { useConfigStore } from "@/stores/config";
|
import { useConfigStore } from "@/stores/config";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
|
let timer: any = null;
|
||||||
|
|
||||||
export const useUserStore = defineStore("user", {
|
export const useUserStore = defineStore("user", {
|
||||||
persist: true,
|
persist: true,
|
||||||
state: () => ({
|
state: () => ({
|
||||||
@ -39,20 +42,18 @@ export const useUserStore = defineStore("user", {
|
|||||||
this.logined = true;
|
this.logined = true;
|
||||||
},
|
},
|
||||||
setupTimer() {
|
setupTimer() {
|
||||||
// 如果有 timer
|
timer = setInterval(() => {
|
||||||
if (this.timer) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.timer = setInterval(() => {
|
|
||||||
if (this.logined) {
|
if (this.logined) {
|
||||||
if (this.expired_at - Date.now() < 60000) {
|
if (this.expired_at - Date.now() < 60000) {
|
||||||
this.refresh();
|
this.refresh();
|
||||||
|
} else {
|
||||||
|
// 显示还有多长时间过期
|
||||||
|
console.log(
|
||||||
|
"Token will expire in " +
|
||||||
|
(this.expired_at - Date.now()) / 1000 +
|
||||||
|
" seconds"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// else {
|
|
||||||
// // 显示还有多长时间过期
|
|
||||||
// // console.log("Token will expire in " + (this.expired_at - Date.now()) / 1000 + " seconds");
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}, 10 * 1000);
|
}, 10 * 1000);
|
||||||
},
|
},
|
||||||
@ -69,15 +70,26 @@ export const useUserStore = defineStore("user", {
|
|||||||
data.set("client_id", configStore.oauth_client_id);
|
data.set("client_id", configStore.oauth_client_id);
|
||||||
data.set("scope", configStore.oauth_scope);
|
data.set("scope", configStore.oauth_scope);
|
||||||
|
|
||||||
// axios
|
axios
|
||||||
let refresh = await axios.post(discovery.data.token_endpoint, data);
|
.post(discovery.data.token_endpoint, data)
|
||||||
|
.then((response) => {
|
||||||
this.login(
|
this.login(
|
||||||
refresh.data.id_token,
|
response.data.id_token,
|
||||||
refresh.data.access_token,
|
response.data.access_token,
|
||||||
refresh.data.refresh_token,
|
response.data.refresh_token,
|
||||||
refresh.data.expires_in
|
response.data.expires_in
|
||||||
);
|
);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
// if 401
|
||||||
|
if (error.response.status == 401) {
|
||||||
|
console.log("Refresh token failed");
|
||||||
|
}
|
||||||
|
// logout
|
||||||
|
this.logout();
|
||||||
|
clearInterval(timer);
|
||||||
|
return;
|
||||||
|
});
|
||||||
},
|
},
|
||||||
logout() {
|
logout() {
|
||||||
this.user = {
|
this.user = {
|
||||||
|
Loading…
Reference in New Issue
Block a user