From a63dc65ab6bda3cae66a11da929d23e95f3c5c4c Mon Sep 17 00:00:00 2001 From: veypi Date: Thu, 30 Apr 2026 15:14:08 +0800 Subject: [PATCH] refactor(ui): Rename $vbase to $auth across frontend - Rename $mod.$vbase to $mod.$auth in env.js initialization - Add guard condition to prevent duplicate VBase initialization - Update all page and layout templates to use $auth instead of $vbase - Update route guard in routes.js to reference $mod.$auth --- ui/env.js | 17 ++++++++--------- ui/ico.html | 4 ++-- ui/layout/default.html | 4 ++-- ui/layout/ico.html | 4 ++-- ui/page/auth/callback.html | 6 +++--- ui/page/auth/login.html | 20 ++++++++++---------- ui/page/sys/oauth/providers.html | 2 +- ui/page/user/profile.html | 6 +++--- ui/routes.js | 2 +- 9 files changed, 32 insertions(+), 33 deletions(-) diff --git a/ui/env.js b/ui/env.js index 640e136..afbb91c 100644 --- a/ui/env.js +++ b/ui/env.js @@ -9,15 +9,14 @@ export default async ($mod) => { } catch (e) { console.error('Failed to load langs.json', e) } - - // Initialize VBase Service - $mod.users = {} - const vbase = new VBase('vb', $mod.scoped, null, $mod.users); // Relative path - $mod.$vbase = vbase; - - // Wrap Axios: add auth header - vbase.wrapAxios($mod.$axios); - + if (!$mod.$auth) { + // Initialize VBase Service + $mod.users = {} + const vbase = new VBase('vb', $mod.scoped, null, $mod.users); // Relative path + $mod.$auth = vbase; + // Wrap Axios: add auth header + vbase.wrapAxios($mod.$axios); + } $mod.$axios.interceptors.response.use(function(response) { return response?.data }, function(error) { diff --git a/ui/ico.html b/ui/ico.html index b31cd96..cf8207d 100644 --- a/ui/ico.html +++ b/ui/ico.html @@ -52,7 +52,7 @@ diff --git a/ui/layout/default.html b/ui/layout/default.html index 07896f6..8848efa 100644 --- a/ui/layout/default.html +++ b/ui/layout/default.html @@ -123,7 +123,7 @@ diff --git a/ui/layout/ico.html b/ui/layout/ico.html index ff93edb..cdcc935 100644 --- a/ui/layout/ico.html +++ b/ui/layout/ico.html @@ -157,7 +157,7 @@ {label: () => $t('nav.oauth_providers'), icon: "", path: "/oauth/providers"}, {label: () => $t('nav.settings'), icon: "", path: "/settings"}, ]; - user = $vbase.user || {} + user = $auth.user || {} dropdownOpen = false toggleDropdown = (event) => { @@ -168,7 +168,7 @@ logout = (event) => { event.stopPropagation() dropdownOpen = false - $vbase.logout(window.location.href) + $auth.logout(window.location.href) } diff --git a/ui/page/auth/callback.html b/ui/page/auth/callback.html index 72ef703..5b363b0 100644 --- a/ui/page/auth/callback.html +++ b/ui/page/auth/callback.html @@ -492,7 +492,7 @@ return; } - const data = await $mod.$vbase.oauthCallback(provider, code, state); + const data = await $mod.$auth.oauthCallback(provider, code, state); if (data.access_token || data.token) { // Already bound, login success (token set by vbase) @@ -527,7 +527,7 @@ submitting = true; try { - const data = await $mod.$vbase.bindAccount(tempToken, bindForm.username, bindForm.password); + const data = await $mod.$auth.bindAccount(tempToken, bindForm.username, bindForm.password); $message.success($t('auth.bind_success') || 'Binding successful'); setTimeout(() => $router.push('/'), 1000); @@ -546,7 +546,7 @@ submitting = true; try { - const data = await $mod.$vbase.bindRegister(tempToken, regForm.username, regForm.email || undefined); + const data = await $mod.$auth.bindRegister(tempToken, regForm.username, regForm.email || undefined); $message.success($t('auth.register_success') || 'Registration successful'); setTimeout(() => $router.push('/'), 1000); diff --git a/ui/page/auth/login.html b/ui/page/auth/login.html index 987b28c..53a37e5 100644 --- a/ui/page/auth/login.html +++ b/ui/page/auth/login.html @@ -690,7 +690,7 @@ } try { - await $mod.$vbase.request('POST', '/api/verification/send', { + await $mod.$auth.request('POST', '/api/verification/send', { type: type, target: signInForm.target, purpose: 'login' @@ -729,16 +729,16 @@ } const type = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(signInForm.target) ? 'email' : 'phone'; - const data = await $mod.$vbase.request('POST', '/api/auth/login/code', { + const data = await $mod.$auth.request('POST', '/api/auth/login/code', { type: type, target: signInForm.target, code: signInForm.code }); if (data && data.access_token) { - $mod.$vbase.token = data.access_token; - if (data.refresh_token) $mod.$vbase.refreshToken = data.refresh_token; - if (data.user) $mod.$vbase.user = data.user; + $mod.$auth.token = data.access_token; + if (data.refresh_token) $mod.$auth.refreshToken = data.refresh_token; + if (data.user) $mod.$auth.user = data.user; $message.success($t('auth.login_success')); if (redirect === '/' || redirect.startsWith('http')) { @@ -755,7 +755,7 @@ throw new Error($t('auth.fill_all_fields')); } - const success = await $mod.$vbase.login(signInForm.username, signInForm.password); + const success = await $mod.$auth.login(signInForm.username, signInForm.password); if (success) { $message.success($t('auth.login_success')); @@ -803,7 +803,7 @@ try { // Use vbase.request for consistent API handling - const data = await $mod.$vbase.request('POST', '/api/auth/register', { + const data = await $mod.$auth.request('POST', '/api/auth/register', { username: signUpForm.username, email: signUpForm.email || undefined, phone: signUpForm.phone || undefined, @@ -812,12 +812,12 @@ if (data && data.access_token) { // Auto login after register using vbase setters - $mod.$vbase.token = data.access_token; + $mod.$auth.token = data.access_token; if (data.refresh_token) { - $mod.$vbase.refreshToken = data.refresh_token; + $mod.$auth.refreshToken = data.refresh_token; } if (data.user) { - $mod.$vbase.user = data.user; + $mod.$auth.user = data.user; } $message.success($t('auth.register_success')); diff --git a/ui/page/sys/oauth/providers.html b/ui/page/sys/oauth/providers.html index 080aca8..0c64776 100644 --- a/ui/page/sys/oauth/providers.html +++ b/ui/page/sys/oauth/providers.html @@ -200,7 +200,7 @@ loadProviders = async () => { try { - const res = await $vbase.request('GET', '/api/oauth/providers'); + const res = await $auth.request('GET', '/api/oauth/providers'); providers = res.items || []; } catch (e) { $message.error(e.message); diff --git a/ui/page/user/profile.html b/ui/page/user/profile.html index 645feb9..034cb7c 100644 --- a/ui/page/user/profile.html +++ b/ui/page/user/profile.html @@ -239,7 +239,7 @@