mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-06 20:32:55 +08:00
feature: auto refresh user token when token exp in 7 days (#644)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user