Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: refresh pages if login as another user (#13186)
ecmadao13892268a87f
diff --git a/frontend/src/App.vue b/frontend/src/App.vueindex b82c47dbbd..afa76da375 100644--- a/frontend/src/App.vue+++ b/frontend/src/App.vue@@ -15,13 +15,15 @@<OverlayStackManager><KBarWrapper><NotificationContext>- <router-view v-if="initialized" />- <div- v-else- class="fixed inset-0 bg-white flex flex-col items-center justify-center"- >- <NSpin />- </div>+ <AuthContext>+ <router-view v-if="actuatorStore.initialized" />+ <div+ v-else+ class="fixed inset-0 bg-white flex flex-col items-center justify-center"+ >+ <NSpin />+ </div>+ </AuthContext></NotificationContext></KBarWrapper></OverlayStackManager>@@ -33,24 +35,24 @@<script lang="ts" setup>import { cloneDeep, isEqual } from "lodash-es";import {+ NSpin,NConfigProvider,NDialogProvider,NNotificationProvider,} from "naive-ui";-import { NSpin } from "naive-ui";import { ServerError } from "nice-grpc-common";import { ClientError, Status } from "nice-grpc-web";-import { onErrorCaptured, onMounted, ref } from "vue";+import { onErrorCaptured, onMounted } from "vue";import { watch } from "vue";import { useRoute, useRouter } from "vue-router";import Watermark from "@/components/misc/Watermark.vue";import { themeOverrides, dateLang, generalLang } from "../naive-ui.config";import { provideAppRootContext } from "./AppRootContext";+import AuthContext from "./AuthContext.vue";import NotificationContext from "./NotificationContext.vue";import KBarWrapper from "./components/KBar/KBarWrapper.vue";import OverlayStackManager from "./components/misc/OverlayStackManager.vue";import { t } from "./plugins/i18n";-import { AUTH_SIGNIN_MODULE } from "./router/auth";import {useActuatorV1Store,useAuthStore,@@ -62,20 +64,16 @@ import { isDev } from "./utils";// Show at most 3 notifications to prevent excessive notification when shit hits the fan.const MAX_NOTIFICATION_DISPLAY_COUNT = 3;-// Check expiration every 30 sec and logout if expired-const CHECK_LOGGEDIN_STATE_DURATION = 30 * 1000;-const route = useRoute();const router = useRouter();const { key } = provideAppRootContext();const authStore = useAuthStore();const notificationStore = useNotificationStore();-const initialized = ref(false);-const prevLoggedIn = ref(false);+const actuatorStore = useActuatorV1Store();onMounted(async () => {const initActuator = async () => {- useActuatorV1Store().fetchServerInfo();+ actuatorStore.fetchServerInfo();};const initSubscription = async () => {await useSubscriptionV1Store().fetchSubscription();@@ -97,24 +95,7 @@ onMounted(async () => {};await initBasicModules();- initialized.value = true;- prevLoggedIn.value = authStore.isLoggedIn();-- setInterval(() => {- if (!initialized.value) {- return;- }-- const loggedIn = authStore.isLoggedIn();- if (prevLoggedIn.value != loggedIn) {- prevLoggedIn.value = loggedIn;- if (!loggedIn) {- authStore.logout().then(() => {- router.push({ name: AUTH_SIGNIN_MODULE });- });- }- }- }, CHECK_LOGGEDIN_STATE_DURATION);+ actuatorStore.initialized = true;});onErrorCaptured((error: any /* , _, info */) => {diff --git a/frontend/src/AuthContext.vue b/frontend/src/AuthContext.vuenew file mode 100644index 0000000000..bf9443038a--- /dev/null+++ b/frontend/src/AuthContext.vue@@ -0,0 +1,71 @@+<template>+ <slot></slot>+</template>++<script setup lang="ts">+import { useDialog, type DialogReactive } from "naive-ui";+import { onMounted, ref } from "vue";+import { useRouter } from "vue-router";+import { t } from "./plugins/i18n";+import { AUTH_SIGNIN_MODULE } from "./router/auth";+import { WORKSPACE_ROOT_MODULE } from "./router/dashboard/workspaceRoutes";+import { useActuatorV1Store, useAuthStore } 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;++const router = useRouter();+const authStore = useAuthStore();+const actuatorStore = useActuatorV1Store();+const dialog = useDialog();+const $dialog = ref<DialogReactive | undefined>();+const prevLoggedIn = ref(false);++onMounted(() => {+ prevLoggedIn.value = authStore.isLoggedIn();++ setInterval(() => {+ if (!actuatorStore.initialized) {+ return;+ }++ const loggedIn = authStore.isLoggedIn();+ if (prevLoggedIn.value != loggedIn) {+ prevLoggedIn.value = loggedIn;+ if (!loggedIn) {+ authStore.logout().then(() => {+ router.push({ name: AUTH_SIGNIN_MODULE });+ });+ }+ }+ if (+ loggedIn &&+ authStore.currentUserId !== authStore.getUserIdFromCookie() &&+ $dialog.value === undefined+ ) {+ $dialog.value = dialog.create({+ title: t("auth.login-as-another.title"),+ content: t("auth.login-as-another.content"),+ type: "warning",+ autoFocus: false,+ closable: false,+ maskClosable: false,+ closeOnEsc: false,+ onPositiveClick() {+ authStore.restoreUser().then(() => {+ router.push({+ name: WORKSPACE_ROOT_MODULE,+ // to force route reload+ query: { _r: Date.now() },+ });+ });+ },+ positiveText: t("common.confirm"),+ showIcon: false,+ });+ }+ }, CHECK_LOGGEDIN_STATE_DURATION);+});+</script>diff --git a/frontend/src/components/Project/useRecentProjects.ts b/frontend/src/components/Project/useRecentProjects.tsindex 22673a4b3a..40ae34f979 100644--- a/frontend/src/components/Project/useRecentProjects.ts+++ b/frontend/src/components/Project/useRecentProjects.ts@@ -11,7 +11,7 @@ export const useRecentProjects = () => {const currentUser = useCurrentUserV1();const recentViewProjectNames = useLocalStorage<string[]>(- "bb.project.recent-view",+ `bb.project.recent-view.${currentUser.value.name}`,[]);diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex edca39febf..ac185d8f7c 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -1617,6 +1617,10 @@},"activate": {"title": "Activate your {type} account"+ },+ "login-as-another": {+ "title": "You have logged in as another user",+ "content": "We will refirect you to the home page."}},"policy": {diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 0d23fab6c2..19bc3354b1 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -97,6 +97,7 @@"stage": "Etapa","task": "Tarea","tasks": "Tareas",+ "blocking-task": "Tarea de bloqueo","sql": "SQL","unassigned": "No asignado","new": "Nuevo",@@ -279,8 +280,7 @@"options": "Opciones","reset": "Reiniciar","refresh": "Actualizar",- "rollback": "Retroceder",- "blocking-task": "Tarea de bloqueo"+ "rollback": "Retroceder"},"error-page": {"go-back-home": "Volver al inicio"@@ -1617,6 +1617,10 @@},"activate": {"title": "Active su cuenta {type}"+ },+ "login-as-another": {+ "title": "Has iniciado sesión como otro usuario",+ "content": "Lo redirigiremos a la página de inicio."}},"policy": {@@ -3637,4 +3641,4 @@"self": "Última actualización"}}-}+}\ No newline at end of filediff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 8411f7234b..6b2f7f5d70 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -97,6 +97,7 @@"stage": "ステージ","task": "タスク","tasks": "タスク",+ "blocking-task": "タスクをブロック中","sql": "SQL","unassigned": "割り当てられていない","new": "作成する",@@ -279,8 +280,7 @@"options": "オプション","reset": "リセット","refresh": "リフレッシュ",- "rollback": "ロールバック",- "blocking-task": "タスクをブロック中"+ "rollback": "ロールバック"},"error-page": {"go-back-home": "ホームページに戻る"@@ -1617,6 +1617,10 @@},"activate": {"title": "{type} アカウントをアクティブ化します"+ },+ "login-as-another": {+ "title": "別のユーザーとしてログインしています",+ "content": "ホームページへリダイレクトさせていただきます。"}},"policy": {@@ -3637,4 +3641,4 @@"self": "最後の更新"}}-}+}\ No newline at end of filediff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex d86e2eb029..c30175d350 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -97,6 +97,7 @@"stage": "阶段","task": "任务","tasks": "任务",+ "blocking-task": "阻塞任务","sql": "SQL","unassigned": "未指派","new": "创建",@@ -279,8 +280,7 @@"options": "配置项","reset": "重置","refresh": "刷新",- "rollback": "回滚",- "blocking-task": "阻塞任务"+ "rollback": "回滚"},"error-page": {"go-back-home": "回到主页"@@ -1617,6 +1617,10 @@},"activate": {"title": "激活您的 {type} 账号"+ },+ "login-as-another": {+ "title": "您已以其他用户身份登录",+ "content": "我们将把您重定向到主页。"}},"policy": {@@ -3637,4 +3641,4 @@"self": "最后更新"}}-}+}\ No newline at end of filediff --git a/frontend/src/router/useRecentVisit.ts b/frontend/src/router/useRecentVisit.tsindex 59d5087d95..9e76657814 100644--- a/frontend/src/router/useRecentVisit.ts+++ b/frontend/src/router/useRecentVisit.ts@@ -1,6 +1,7 @@import { useLocalStorage, useDebounce } from "@vueuse/core";import { computed, ref, watch, nextTick } from "vue";import { useRoute } from "vue-router";+import { useAuthStore } from "@/store";import { WORKSPACE_ROOT_MODULE } from "./dashboard/workspaceRoutes";type RecentVisit = {@@ -13,7 +14,11 @@ const MAX_HISTORY = 3;export function useRecentVisit() {const route = useRoute();- const recentVisit = useLocalStorage(STORAGE_KEY, [] as RecentVisit[]);+ const authStore = useAuthStore();+ const recentVisit = useLocalStorage(+ `${STORAGE_KEY}.${authStore.currentUserId ?? 0}`,+ [] as RecentVisit[]+ );const lastVisit = computed(() => {if (recentVisit.value.length === 0) {diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.tsindex de9a8b2a35..6b6b3d3a3e 100644--- a/frontend/src/store/modules/auth.ts+++ b/frontend/src/store/modules/auth.ts@@ -24,7 +24,11 @@ export const useAuthStore = defineStore("auth_v1", () => {});const isLoggedIn = () => {- return getIntCookie("user") != undefined;+ return getUserIdFromCookie() != undefined;+ };++ const getUserIdFromCookie = () => {+ return getIntCookie("user");};const login = async (request: Partial<LoginRequest>) => {@@ -35,11 +39,7 @@ export const useAuthStore = defineStore("auth_v1", () => {return mfaTempToken;}- currentUserId.value = getIntCookie("user");- if (currentUserId.value) {- await useUserStore().getOrFetchUserById(String(currentUserId.value));- await useWorkspaceV1Store().fetchIamPolicy();- }+ await restoreUser();};const signup = async (request: Partial<User>) => {@@ -69,7 +69,7 @@ export const useAuthStore = defineStore("auth_v1", () => {};const restoreUser = async () => {- currentUserId.value = getIntCookie("user");+ currentUserId.value = getUserIdFromCookie();if (currentUserId.value) {await useUserStore().getOrFetchUserById(String(currentUserId.value),@@ -92,6 +92,7 @@ export const useAuthStore = defineStore("auth_v1", () => {currentUser,currentUserId,isLoggedIn,+ getUserIdFromCookie,login,signup,logout,diff --git a/frontend/src/store/modules/v1/actuator.ts b/frontend/src/store/modules/v1/actuator.tsindex f76ad49163..f561c0dfac 100644--- a/frontend/src/store/modules/v1/actuator.ts+++ b/frontend/src/store/modules/v1/actuator.ts@@ -24,6 +24,8 @@ const GITHUB_API_LIST_BYTEBASE_RELEASE ="https://api.github.com/repos/bytebase/bytebase/releases";interface ActuatorState {+ // Whether the app is initialized or not.+ initialized: boolean;serverInfo?: ActuatorInfo;resourcePackage?: ResourcePackage;releaseInfo: RemovableRef<ReleaseInfo>;@@ -32,6 +34,7 @@ interface ActuatorState {export const useActuatorV1Store = defineStore("actuator_v1", {state: (): ActuatorState => ({+ initialized: false,serverInfo: undefined,resourcePackage: undefined,releaseInfo: useLocalStorage("bytebase_release", {