Skip to content

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

chore: update auth context (#15853)

boojackae2abddf3869
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 93088f122d..ccb1c36e0a 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -15,13 +15,7 @@
<OverlayStackManager>
<NotificationContext>
<AuthContext>
- <router-view v-if="initialized" />
- <div
- v-else
- class="fixed inset-0 bg-white flex flex-col items-center justify-center"
- >
- <NSpin />
- </div>
+ <router-view />
</AuthContext>
</NotificationContext>
</OverlayStackManager>
@@ -33,14 +27,13 @@
<script lang="ts" setup>
import { cloneDeep, isEqual } from "lodash-es";
import {
- NSpin,
NConfigProvider,
NDialogProvider,
NNotificationProvider,
} from "naive-ui";
import { ServerError } from "nice-grpc-common";
import { ClientError, Status } from "nice-grpc-web";
-import { onErrorCaptured, onMounted, ref, watchEffect } from "vue";
+import { onErrorCaptured, watchEffect } from "vue";
import { watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import Watermark from "@/components/misc/Watermark.vue";
@@ -51,11 +44,7 @@ import NotificationContext from "./NotificationContext.vue";
import OverlayStackManager from "./components/misc/OverlayStackManager.vue";
import { overrideAppProfile } from "./customAppProfile";
import { t } from "./plugins/i18n";
-import {
- useAuthStore,
- useNotificationStore,
- useWorkspaceV1Store,
-} from "./store";
+import { useNotificationStore } from "./store";
import { isDev } from "./utils";
// Show at most 3 notifications to prevent excessive notification when shit hits the fan.
@@ -64,18 +53,7 @@ const MAX_NOTIFICATION_DISPLAY_COUNT = 3;
const route = useRoute();
const router = useRouter();
const { key } = provideAppRootContext();
-const authStore = useAuthStore();
const notificationStore = useNotificationStore();
-const workspaceStore = useWorkspaceV1Store();
-const initialized = ref(false);
-
-onMounted(async () => {
- if (authStore.isLoggedIn) {
- await workspaceStore.fetchIamPolicy();
- }
-
- initialized.value = true;
-});
watchEffect(async () => {
// Override app profile.
diff --git a/frontend/src/AuthContext.vue b/frontend/src/AuthContext.vue
index 379ddac53a..02a4c598d6 100644
--- a/frontend/src/AuthContext.vue
+++ b/frontend/src/AuthContext.vue
@@ -4,22 +4,17 @@
</template>
<script setup lang="ts">
-import { onMounted, ref, watch } from "vue";
+import { onMounted, watch } from "vue";
import { useRouter } from "vue-router";
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 } from "./store";
-
-// Check expiration every 15 sec and:
-// 1. logout if expired.
-// 2. refresh pages if login as another user.
-const CHECK_LOGGEDIN_STATE_DURATION = 15 * 1000;
+import { useAuthStore, pushNotification, useWorkspaceV1Store } from "./store";
const router = useRouter();
const authStore = useAuthStore();
-const prevLoggedIn = ref(false);
+const workspaceStore = useWorkspaceV1Store();
onMounted(() => {
if (!authStore.isLoggedIn) {
@@ -31,25 +26,6 @@ onMounted(() => {
});
return;
}
-
- prevLoggedIn.value = authStore.isLoggedIn;
-
- setInterval(() => {
- const loggedIn = authStore.isLoggedIn;
- if (prevLoggedIn.value !== loggedIn) {
- prevLoggedIn.value = loggedIn;
- if (!loggedIn) {
- authStore.showLoginModal = true;
- pushNotification({
- module: "bytebase",
- style: "WARN",
- title: t("auth.token-expired-title"),
- description: t("auth.token-expired-description"),
- });
- return;
- }
- }
- }, CHECK_LOGGEDIN_STATE_DURATION);
});
// When current user changed, we need to redirect to the workspace root page.
@@ -75,4 +51,16 @@ watch(
}
}
);
+
+watch(
+ () => authStore.isLoggedIn,
+ async () => {
+ if (authStore.isLoggedIn) {
+ await workspaceStore.fetchIamPolicy();
+ }
+ },
+ {
+ immediate: true,
+ }
+);
</script>
diff --git a/frontend/src/components/PasswordSigninForm.vue b/frontend/src/components/PasswordSigninForm.vue
index ed011d5999..297b2285c0 100644
--- a/frontend/src/components/PasswordSigninForm.vue
+++ b/frontend/src/components/PasswordSigninForm.vue
@@ -25,7 +25,7 @@
<div>
<label
for="password"
- class="flex justify-between text-sm font-medium leading-5 text-control"
+ class="flex justify-between text-sm font-medium leading-5 gap-4 text-control"
>
<div>
{{ $t("common.password") }}
diff --git a/frontend/src/components/v2/Model/PagedTable.vue b/frontend/src/components/v2/Model/PagedTable.vue
index b663b18ff9..2f38d33925 100644
--- a/frontend/src/components/v2/Model/PagedTable.vue
+++ b/frontend/src/components/v2/Model/PagedTable.vue
@@ -52,8 +52,6 @@ type SessionState = {
pageSize: number;
};
-const SESSION_LIFE = 1 * 60 * 1000; // 1 minute
-
const props = withDefaults(
defineProps<{
// A unique key to identify the session state.
@@ -120,7 +118,7 @@ const onPageSizeChange = (size: number) => {
};
const fetchData = async (refresh = false) => {
- if (!authStore.isLoggedIn) {
+ if (!authStore.isLoggedIn || authStore.unauthenticatedOccurred) {
return;
}
@@ -175,17 +173,17 @@ const fetchNextPage = () => {
fetchData(false);
};
-if (Date.now() - sessionState.value.updatedTs > SESSION_LIFE) {
- // Reset session if it's outdated.
- resetSession();
-}
fetchData(true);
watch(
- () => authStore.currentUserName,
+ () => authStore.authSessionKey,
() => {
+ if (!authStore.isLoggedIn || authStore.unauthenticatedOccurred) {
+ return;
+ }
// Reset session when logging status changed.
resetSession();
+ refresh();
}
);
diff --git a/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
index 31ec8c61fb..e46006d83b 100644
--- a/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
+++ b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
@@ -46,7 +46,7 @@ export const authInterceptorMiddleware: ClientMiddleware<IgnoreErrorsOptions> =
}
// When receiving 401 and is returned by our server, it means the current
// login user's token becomes invalid. Thus we force the user to login again.
- useAuthStore().showLoginModal = true;
+ useAuthStore().unauthenticatedOccurred = true;
} else if (code === Status.PERMISSION_DENIED) {
// Jump to 403 page
router.push({ name: "error.403" });
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index b162863634..fbd3ea43d8 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1317,9 +1317,7 @@
"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 64528574f8..a695df62e1 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1317,9 +1317,7 @@
"login-as-another": {
"title": "Has iniciado sesión como otro usuario",
"content": "Lo redirigiremos a la página de inicio."
- },
- "token-expired-title": "Token caducado",
- "token-expired-description": "El token ha expirado, por favor inicie sesión nuevamente"
+ }
},
"policy": {
"rollout": {
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index b06f3171da..e154f96cf5 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -1317,9 +1317,7 @@
"login-as-another": {
"title": "別のユーザーとしてログインしています",
"content": "ホームページへリダイレクトさせていただきます。"
- },
- "token-expired-title": "トークンの有効期限が切れました",
- "token-expired-description": "トークンの有効期限が切れています。再度ログインしてください"
+ }
},
"policy": {
"rollout": {
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index af6dcdd570..e82bdb9fc3 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -1317,9 +1317,7 @@
"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": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index d28b19da84..c8931b5891 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1317,9 +1317,7 @@
"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 7560e1284a..dd4e63ae7c 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -1,5 +1,6 @@
import { useLocalStorage } from "@vueuse/core";
import axios from "axios";
+import { uniqueId } from "lodash-es";
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { userServiceClient } from "@/grpcweb";
@@ -19,7 +20,8 @@ import { User, UserType } from "@/types/proto/v1/user_service";
export const useAuthStore = defineStore("auth_v1", () => {
const userStore = useUserStore();
- const showLoginModal = ref<boolean>(false);
+ const authSessionKey = ref<string>(uniqueId());
+ const unauthenticatedOccurred = ref<boolean>(false);
// Format: users/{user}. {user} is a system-generated unique ID.
const currentUserName = ref<string | undefined>(undefined);
@@ -63,6 +65,7 @@ export const useAuthStore = defineStore("auth_v1", () => {
const { data } = await axios.post<LoginResponse>("/v1/auth/login", request);
const redirectUrl = redirect || getRedirectQuery();
if (data.mfaTempToken) {
+ unauthenticatedOccurred.value = false;
return router.push({
name: AUTH_MFA_MODULE,
query: {
@@ -77,20 +80,18 @@ export const useAuthStore = defineStore("auth_v1", () => {
await useSettingV1Store().getOrFetchSettingByName(
"bb.workspace.profile",
- /* silent */ true
+ true // silent
);
- if (!showLoginModal.value) {
- let nextPage = redirectUrl || "/";
+ if (!unauthenticatedOccurred.value) {
const mode = useAppFeature("bb.feature.database-change-mode");
-
+ let nextPage = redirectUrl || "/";
if (mode.value === DatabaseChangeMode.EDITOR) {
const route = router.resolve({
name: SQL_EDITOR_HOME_MODULE,
});
nextPage = route.fullPath;
}
-
if (data.requireResetPassword) {
return router.push({
name: AUTH_PASSWORD_RESET_MODULE,
@@ -99,9 +100,9 @@ export const useAuthStore = defineStore("auth_v1", () => {
},
});
}
-
return router.replace(nextPage);
}
+ unauthenticatedOccurred.value = false;
};
const signup = async (request: Partial<User>) => {
@@ -143,6 +144,8 @@ export const useAuthStore = defineStore("auth_v1", () => {
try {
const currentUser = await userStore.fetchCurrentUser();
currentUserName.value = currentUser.name;
+ // After user login, we need to reset the auth session key.
+ authSessionKey.value = uniqueId();
} catch {
// do nothing.
}
@@ -151,13 +154,14 @@ export const useAuthStore = defineStore("auth_v1", () => {
return {
currentUserName,
isLoggedIn,
+ unauthenticatedOccurred,
+ requireResetPassword,
+ authSessionKey,
login,
signup,
logout,
fetchCurrentUser,
- requireResetPassword,
setRequireResetPassword,
- showLoginModal,
};
});
diff --git a/frontend/src/views/auth/SigninModal.vue b/frontend/src/views/auth/SigninModal.vue
index 0bf81d4e31..85a07ba58e 100644
--- a/frontend/src/views/auth/SigninModal.vue
+++ b/frontend/src/views/auth/SigninModal.vue
@@ -1,12 +1,12 @@
<template>
<BBModal
- :show="authStore.showLoginModal"
+ :show="shouldShow"
:trap-focus="true"
:show-close="false"
:mask-closable="false"
:header-class="'!hidden'"
>
- <div style="width: 30vw; height: 70vh" class="flex items-center">
+ <div class="flex items-center w-auto md:min-w-96 max-w-full h-auto md:py-4">
<div class="flex flex-col justify-center items-center flex-1 space-y-2">
<Signin :allow-signup="false">
<template #footer>
@@ -22,9 +22,19 @@
<script lang="tsx" setup>
import { NButton } from "naive-ui";
-import { watch } from "vue";
+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 Signin from "@/views/auth/Signin.vue";
@@ -32,17 +42,28 @@ const route = useRoute();
const authStore = useAuthStore();
const logout = () => {
- authStore.showLoginModal = false;
+ authStore.unauthenticatedOccurred = false;
authStore.logout();
};
-// Auto-close the modal when the route changed.
-watch(
- () => route.name,
- () => {
- if (authStore.showLoginModal) {
- authStore.showLoginModal = false;
- }
+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())
+ ) {
+ return false;
}
-);
+
+ return Boolean(authStore.unauthenticatedOccurred);
+});
</script>