Skip to content

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

chore: popup login modal if token expired (#14669)

ecmadao994291e59954
diff --git a/frontend/src/AuthContext.vue b/frontend/src/AuthContext.vue
index 29de320aab..a8fe000e98 100644
--- a/frontend/src/AuthContext.vue
+++ b/frontend/src/AuthContext.vue
@@ -1,14 +1,16 @@
<template>
<slot></slot>
+ <SigninModal />
</template>
<script setup lang="ts">
import { useDialog, type DialogReactive } from "naive-ui";
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
+import SigninModal from "@/views/auth/SigninModal.vue";
import { t } from "./plugins/i18n";
import { WORKSPACE_ROOT_MODULE } from "./router/dashboard/workspaceRoutes";
-import { useActuatorV1Store, useAuthStore } from "./store";
+import { useActuatorV1Store, useAuthStore, pushNotification } from "./store";
// Check expiration every 15 sec and:
// 1. logout if expired.
@@ -31,10 +33,18 @@ onMounted(() => {
}
const loggedIn = authStore.isLoggedIn();
+
if (prevLoggedIn.value != loggedIn) {
prevLoggedIn.value = loggedIn;
if (!loggedIn) {
- authStore.logout();
+ authStore.showLoginModal = true;
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: t("auth.token-expired-title"),
+ description: t("auth.token-expired-description"),
+ });
+ return;
}
}
if (
diff --git a/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
index f8228b5585..3fc35d3181 100644
--- a/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
+++ b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
@@ -36,8 +36,9 @@ export const authInterceptorMiddleware: ClientMiddleware<IgnoreErrorsOptions> =
// omit specified errors
} else {
if (code === Status.UNAUTHENTICATED) {
- // "Kick out" sign in status if access token expires.
- await useAuthStore().logout();
+ // 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;
} 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 13743f618b..ed0b56580a 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1743,7 +1743,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 d1e51516af..63d7f2dd01 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1743,7 +1743,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 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 9a8dd6e582..21af84463f 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -1743,7 +1743,9 @@
"login-as-another": {
"title": "別のユーザーとしてログインしています",
"content": "ホームページへリダイレクトさせていただきます。"
- }
+ },
+ "token-expired-title": "トークンの有効期限が切れました",
+ "token-expired-description": "トークンの有効期限が切れています。再度ログインしてください"
},
"policy": {
"rollout": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index fa2fb4ea6c..4a95cc1b01 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1743,7 +1743,9 @@
"login-as-another": {
"title": "您已以其他用户身份登录",
"content": "我们将把您重定向到主页。"
- }
+ },
+ "token-expired-title": "令牌已过期",
+ "token-expired-description": "Token已过期,请重新登录"
},
"policy": {
"rollout": {
@@ -3893,4 +3895,4 @@
"last-visit": "上次访问",
"changelog-for-version": "{version} 的更新日志"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/main.ts b/frontend/src/main.ts
index 12ecab0fb8..2a19e3ae9d 100644
--- a/frontend/src/main.ts
+++ b/frontend/src/main.ts
@@ -13,7 +13,7 @@ import i18n from "./plugins/i18n";
import NaiveUI from "./plugins/naive-ui";
import { isSilent } from "./plugins/silent-request";
import { router } from "./router";
-import { pinia, pushNotification, useAuthStore } from "./store";
+import { pinia, pushNotification } from "./store";
import {
humanizeTs,
humanizeDurationV1,
@@ -55,38 +55,6 @@ axios.interceptors.response.use(
},
async (error) => {
if (error.response) {
- // When receiving 401 and is returned by our server, it means the current
- // login user's token becomes invalid. Thus we force a logout.
- // We could receive 401 when calling external service such as VCS provider,
- // in such case, we shouldn't logout.
- if (error.response.status == 401) {
- const origin = location.origin;
- const pathname = location.pathname;
- if (
- !pathname.startsWith("/auth") &&
- error.response.request.responseURL.startsWith(origin)
- ) {
- // If the request URL starts with the browser's location origin
- // e.g. http://localhost:3000/
- // we know this is a request to Bytebase API endpoint (not an external service).
- // Means that the auth session is error or expired.
- // So we need to "kick out" here.
- await useAuthStore().logout();
- }
- }
-
- // in such case, we shouldn't logout.
- if (error.response.status == 403) {
- const origin = location.origin;
- if (error.response.request.responseURL.startsWith(origin)) {
- // If the request URL starts with the browser's location origin
- // e.g. http://localhost:3000/
- // we know this is a request to Bytebase API endpoint (not an external service).
- // Means that the API request is denied by authorization reasons.
- router.push({ name: "error.403" });
- }
- }
-
if (error.response.data?.message && !isSilent()) {
pushNotification({
module: "bytebase",
@@ -109,6 +77,7 @@ axios.interceptors.response.use(
throw error;
}
);
+
const app = createApp(App);
// Allow template to access various function
app.config.globalProperties.window = window;
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index a18c99b315..85a6f35438 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -21,6 +21,7 @@ import { getIntCookie } from "@/utils";
export const useAuthStore = defineStore("auth_v1", () => {
const userStore = useUserStore();
const currentUserId = ref<number | undefined>();
+ const showLoginModal = ref<boolean>(false);
const currentUser = computed(() => {
if (currentUserId.value) {
@@ -86,26 +87,30 @@ export const useAuthStore = defineStore("auth_v1", () => {
"bb.workspace.profile",
/* silent */ true
);
- 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,
- query: {
- redirect: nextPage,
- },
- });
+ if (!showLoginModal.value) {
+ let nextPage = redirectUrl || "/";
+ const mode = useAppFeature("bb.feature.database-change-mode");
+
+ 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,
+ query: {
+ redirect: nextPage,
+ },
+ });
+ }
+
+ return router.replace(nextPage);
}
-
- return router.replace(nextPage);
+ showLoginModal.value = false;
};
const signup = async (request: Partial<User>) => {
@@ -127,6 +132,7 @@ export const useAuthStore = defineStore("auth_v1", () => {
const logout = async () => {
try {
await axios.post("/v1/auth/logout");
+ showLoginModal.value = false;
} catch {
// nothing
} finally {
@@ -174,6 +180,7 @@ export const useAuthStore = defineStore("auth_v1", () => {
refreshUserIfNeeded,
requireResetPassword,
setRequireResetPassword,
+ showLoginModal,
};
});
diff --git a/frontend/src/views/Setup/ProvideSetupContext.vue b/frontend/src/views/Setup/ProvideSetupContext.vue
index 7d5a6541ce..6c8725766a 100644
--- a/frontend/src/views/Setup/ProvideSetupContext.vue
+++ b/frontend/src/views/Setup/ProvideSetupContext.vue
@@ -10,12 +10,7 @@ import {
type RouteLocationNormalizedLoadedGeneric,
} from "vue-router";
import MaskSpinner from "@/components/misc/MaskSpinner.vue";
-import {
- usePolicyV1Store,
- useRoleStore,
- useSettingV1Store,
- useAuthStore,
-} from "@/store";
+import { usePolicyV1Store, useRoleStore, useSettingV1Store } from "@/store";
import { PolicyResourceType } from "@/types/proto/v1/org_policy_service";
import { hasWorkspacePermissionV2 } from "@/utils";
@@ -50,7 +45,10 @@ onMounted(async () => {
(route) => {
if (!checkPermissions(route)) {
ready.value = false;
- return useAuthStore().logout();
+ router.push({
+ name: "error.403",
+ });
+ return;
}
ready.value = true;
},
diff --git a/frontend/src/views/auth/Signin.vue b/frontend/src/views/auth/Signin.vue
index 922c4f9e24..e3a40c7011 100644
--- a/frontend/src/views/auth/Signin.vue
+++ b/frontend/src/views/auth/Signin.vue
@@ -38,7 +38,7 @@
{{ $t("auth.sign-in.new-user") }}
</span>
<router-link
- :to="{ name: AUTH_SIGNUP_MODULE, query: $route.query }"
+ :to="{ name: AUTH_SIGNUP_MODULE, query: route.query }"
class="accent-link px-2"
>
{{ $t("common.sign-up") }}
@@ -162,7 +162,9 @@
</template>
</div>
</div>
- <AuthFooter />
+ <slot name="footer">
+ <AuthFooter />
+ </slot>
</template>
<template v-else>
<div class="inset-0 absolute flex flex-row justify-center items-center">
@@ -193,6 +195,15 @@ import { IdentityProviderType } from "@/types/proto/v1/idp_service";
import { openWindowForSSO } from "@/utils";
import AuthFooter from "./AuthFooter.vue";
+const props = withDefaults(
+ defineProps<{
+ allowSignup?: boolean;
+ }>(),
+ {
+ allowSignup: true,
+ }
+);
+
interface LocalState {
email: string;
password: string;
@@ -213,8 +224,11 @@ const state = reactive<LocalState>({
isLoading: false,
});
const initialized = ref(false);
-const { isDemo, disallowSignup, disallowPasswordSignin } =
- storeToRefs(actuatorStore);
+const { isDemo, disallowPasswordSignin } = storeToRefs(actuatorStore);
+
+const disallowSignup = computed(
+ () => !props.allowSignup || actuatorStore.disallowSignup
+);
const separatedIdentityProviderList = computed(() =>
identityProviderStore.identityProviderList.filter(
diff --git a/frontend/src/views/auth/SigninModal.vue b/frontend/src/views/auth/SigninModal.vue
new file mode 100644
index 0000000000..b3cd2cd31b
--- /dev/null
+++ b/frontend/src/views/auth/SigninModal.vue
@@ -0,0 +1,35 @@
+<template>
+ <BBModal
+ :show="authStore.showLoginModal"
+ :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 flex-col justify-center items-center flex-1 space-y-2">
+ <Signin :allow-signup="false">
+ <template #footer>
+ <NButton quaternary size="small" @click="logout">
+ {{ $t("common.logout") }}
+ </NButton>
+ </template>
+ </Signin>
+ </div>
+ </div>
+ </BBModal>
+</template>
+
+<script lang="tsx" setup>
+import { NButton } from "naive-ui";
+import { BBModal } from "@/bbkit";
+import { useAuthStore } from "@/store";
+import Signin from "@/views/auth/Signin.vue";
+
+const authStore = useAuthStore();
+
+const logout = () => {
+ authStore.showLoginModal = false;
+ authStore.logout();
+};
+</script>