feature: auto refresh user token when token exp in 7 days (#644)

This commit is contained in:
Dream Hunter
2025-04-26 21:22:26 +08:00
committed by GitHub
parent 327962432a
commit 101a561894
7 changed files with 32 additions and 4 deletions

View File

@@ -25,7 +25,7 @@ const apiFetch = async (path, options = {}) => {
data: options.body || null,
headers: {
'x-lang': i18n.global.locale.value,
'x-user-token': userJwt.value,
'x-user-token': options.userJwt || userJwt.value,
'x-user-access-token': userSettings.value.access_token,
'x-custom-auth': auth.value,
'x-admin-auth': adminAuth.value,
@@ -139,6 +139,19 @@ const getUserSettings = async (message) => {
if (!userJwt.value) return;
const res = await api.fetch("/user_api/settings")
Object.assign(userSettings.value, res)
// auto refresh user jwt
if (userSettings.value.new_user_token) {
try {
await api.fetch("/user_api/settings", {
userJwt: userSettings.value.new_user_token,
})
userJwt.value = userSettings.value.new_user_token;
console.log("User JWT updated successfully");
}
catch (error) {
console.error("Failed to update user JWT", error);
}
}
} catch (error) {
message?.error(error.message || "error");
} finally {

View File

@@ -92,6 +92,8 @@ export const useGlobalState = createGlobalState(
is_admin: false,
/** @type {string | null} */
access_token: null,
/** @type {string | null} */
new_user_token: null,
/** @type {null | {domains: string[] | undefined | null, role: string, prefix: string | undefined | null}} */
user_role: null,
});