Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add auth check timer (#15858)

boojack33016cb97b7e
diff --git a/frontend/src/AuthContext.vue b/frontend/src/AuthContext.vue
index 02a4c598d6..40c164a514 100644
--- a/frontend/src/AuthContext.vue
+++ b/frontend/src/AuthContext.vue
@@ -4,27 +4,55 @@
</template>
<script setup lang="ts">
-import { onMounted, watch } from "vue";
+import { onMounted, onUnmounted, ref, watch } from "vue";
import { useRouter } from "vue-router";
+import { isAuthRelatedRoute } from "@/utils/auth";
import SigninModal from "@/views/auth/SigninModal.vue";
import { t } from "./plugins/i18n";
import { AUTH_PASSWORD_RESET_MODULE } from "./router/auth";
import { WORKSPACE_ROOT_MODULE } from "./router/dashboard/workspaceRoutes";
import { useAuthStore, pushNotification, useWorkspaceV1Store } from "./store";
+// Check authorization every 60 seconds.
+const CHECK_AUTHORIZATION_INTERVAL = 60 * 1000;
+
const router = useRouter();
const authStore = useAuthStore();
const workspaceStore = useWorkspaceV1Store();
+const authCheckIntervalId = ref<NodeJS.Timeout>();
+
onMounted(() => {
- if (!authStore.isLoggedIn) {
- return;
- }
- if (authStore.requireResetPassword) {
- router.replace({
- name: AUTH_PASSWORD_RESET_MODULE,
- });
- return;
+ // Periodically checks if the user's session is still valid.
+ // Skips check if user is not logged in or on an auth-related route.
+ authCheckIntervalId.value = setInterval(async () => {
+ if (!authStore.isLoggedIn || authStore.unauthenticatedOccurred) {
+ return;
+ }
+ if (
+ router.currentRoute.value.name &&
+ isAuthRelatedRoute(router.currentRoute.value.name.toString())
+ ) {
+ return;
+ }
+
+ const user = await authStore.fetchCurrentUser();
+ if (!user) {
+ authStore.unauthenticatedOccurred = true;
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: t("auth.token-expired-title"),
+ description: t("auth.token-expired-description"),
+ });
+ }
+ }, CHECK_AUTHORIZATION_INTERVAL);
+});
+
+onUnmounted(() => {
+ // Clean up the interval when component is unmounted.
+ if (authCheckIntervalId.value) {
+ clearInterval(authCheckIntervalId.value);
}
});
@@ -55,8 +83,16 @@ watch(
watch(
() => authStore.isLoggedIn,
async () => {
- if (authStore.isLoggedIn) {
- await workspaceStore.fetchIamPolicy();
+ if (!authStore.isLoggedIn) {
+ return;
+ }
+
+ await workspaceStore.fetchIamPolicy();
+ if (authStore.requireResetPassword) {
+ router.replace({
+ name: AUTH_PASSWORD_RESET_MODULE,
+ });
+ return;
}
},
{
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index fbd3ea43d8..b162863634 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1317,7 +1317,9 @@
"login-as-another": {
"title": "You have logged in as another user",
"content": "We will refirect you to the home page."
- }
+ },
+ "token-expired-title": "Token expired",
+ "token-expired-description": "Token has expired, please login again"
},
"policy": {
"rollout": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index a695df62e1..6d185c86ca 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1317,7 +1317,9 @@
"login-as-another": {
"title": "Has iniciado sesión como otro usuario",
"content": "Lo redirigiremos a la página de inicio."
- }
+ },
+ "token-expired-title": "Token expirado",
+ "token-expired-description": "El token ha expirado, por favor inicia sesión nuevamente"
},
"policy": {
"rollout": {
@@ -2588,4 +2590,4 @@
"last-visit": "Última visita",
"changelog-for-version": "Registro de cambios para {version}"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index e154f96cf5..16f72d12f5 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -1317,7 +1317,9 @@
"login-as-another": {
"title": "別のユーザーとしてログインしています",
"content": "ホームページへリダイレクトさせていただきます。"
- }
+ },
+ "token-expired-title": "トークンの有効期限が切れました",
+ "token-expired-description": "トークンの有効期限が切れています。再度ログインしてください。"
},
"policy": {
"rollout": {
@@ -2588,4 +2590,4 @@
"last-visit": "最終訪問",
"changelog-for-version": "{version} の変更履歴"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index e82bdb9fc3..dee1198f39 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -1317,7 +1317,9 @@
"login-as-another": {
"title": "Bạn đã đăng nhập với tư cách người dùng khác",
"content": "Chúng tôi sẽ chuyển hướng bạn đến trang chủ."
- }
+ },
+ "token-expired-title": "Mã thông báo đã hết hạn",
+ "token-expired-description": "Mã thông báo đã hết hạn, vui lòng đăng nhập lại"
},
"policy": {
"rollout": {
@@ -2588,4 +2590,4 @@
"last-visit": "Lần truy cập cuối",
"changelog-for-version": "Nhật ký thay đổi cho phiên bản {version}"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index c8931b5891..d28b19da84 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1317,7 +1317,9 @@
"login-as-another": {
"title": "您已以其他用户身份登录",
"content": "我们将把您重定向到主页。"
- }
+ },
+ "token-expired-title": "Token 已过期",
+ "token-expired-description": "Token 已过期,请重新登录"
},
"policy": {
"rollout": {
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index dd4e63ae7c..5f3e45eb65 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -83,6 +83,8 @@ export const useAuthStore = defineStore("auth_v1", () => {
true // silent
);
+ // After user login, we need to reset the auth session key.
+ authSessionKey.value = uniqueId();
if (!unauthenticatedOccurred.value) {
const mode = useAppFeature("bb.feature.database-change-mode");
let nextPage = redirectUrl || "/";
@@ -142,10 +144,9 @@ export const useAuthStore = defineStore("auth_v1", () => {
const fetchCurrentUser = async () => {
try {
- const currentUser = await userStore.fetchCurrentUser();
- currentUserName.value = currentUser.name;
- // After user login, we need to reset the auth session key.
- authSessionKey.value = uniqueId();
+ const user = await userStore.fetchCurrentUser();
+ currentUserName.value = user.name;
+ return user;
} catch {
// do nothing.
}
diff --git a/frontend/src/utils/auth.ts b/frontend/src/utils/auth.ts
new file mode 100644
index 0000000000..513a3709ee
--- /dev/null
+++ b/frontend/src/utils/auth.ts
@@ -0,0 +1,23 @@
+import {
+ AUTH_MFA_MODULE,
+ AUTH_OAUTH_CALLBACK_MODULE,
+ AUTH_OIDC_CALLBACK_MODULE,
+ AUTH_PASSWORD_FORGOT_MODULE,
+ AUTH_PASSWORD_RESET_MODULE,
+ AUTH_SIGNIN_ADMIN_MODULE,
+ AUTH_SIGNIN_MODULE,
+ AUTH_SIGNUP_MODULE,
+} from "@/router/auth";
+
+export const isAuthRelatedRoute = (routeName: string) => {
+ return [
+ AUTH_SIGNIN_MODULE,
+ AUTH_SIGNIN_ADMIN_MODULE,
+ AUTH_SIGNUP_MODULE,
+ AUTH_MFA_MODULE,
+ AUTH_PASSWORD_RESET_MODULE,
+ AUTH_PASSWORD_FORGOT_MODULE,
+ AUTH_OAUTH_CALLBACK_MODULE,
+ AUTH_OIDC_CALLBACK_MODULE,
+ ].includes(routeName);
+};
diff --git a/frontend/src/views/auth/SigninModal.vue b/frontend/src/views/auth/SigninModal.vue
index 85a07ba58e..9a40990c0f 100644
--- a/frontend/src/views/auth/SigninModal.vue
+++ b/frontend/src/views/auth/SigninModal.vue
@@ -25,17 +25,8 @@ import { NButton } from "naive-ui";
import { computed } from "vue";
import { useRoute } from "vue-router";
import { BBModal } from "@/bbkit";
-import {
- AUTH_MFA_MODULE,
- AUTH_OAUTH_CALLBACK_MODULE,
- AUTH_OIDC_CALLBACK_MODULE,
- AUTH_PASSWORD_FORGOT_MODULE,
- AUTH_PASSWORD_RESET_MODULE,
- AUTH_SIGNIN_ADMIN_MODULE,
- AUTH_SIGNIN_MODULE,
- AUTH_SIGNUP_MODULE,
-} from "@/router/auth";
import { useAuthStore } from "@/store";
+import { isAuthRelatedRoute } from "@/utils/auth";
import Signin from "@/views/auth/Signin.vue";
const route = useRoute();
@@ -48,19 +39,7 @@ const logout = () => {
const shouldShow = computed(() => {
// Do not show the modal when the user is in auth related pages.
- if (
- route.name &&
- [
- AUTH_SIGNIN_MODULE,
- AUTH_SIGNIN_ADMIN_MODULE,
- AUTH_SIGNUP_MODULE,
- AUTH_MFA_MODULE,
- AUTH_PASSWORD_RESET_MODULE,
- AUTH_PASSWORD_FORGOT_MODULE,
- AUTH_OAUTH_CALLBACK_MODULE,
- AUTH_OIDC_CALLBACK_MODULE,
- ].includes(route.name.toString())
- ) {
+ if (route.name && isAuthRelatedRoute(route.name as string)) {
return false;
}