Skip to content

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

fix: document.cookie not works with computed or watch (#12897)

ecmadaoaa42fbc21d9d
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 6a36eeb463..244e705677 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -40,7 +40,7 @@ import {
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, ref, watchEffect } from "vue";
import { watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import Watermark from "@/components/misc/Watermark.vue";
@@ -100,9 +100,6 @@ onMounted(async () => {
await initBasicModules();
initialized.value = true;
prevLoggedIn.value = authStore.isLoggedIn();
- if (prevLoggedIn.value) {
- await useWorkspaceV1Store().fetchIamPolicy();
- }
setInterval(() => {
if (!initialized.value) {
@@ -121,6 +118,12 @@ onMounted(async () => {
}, CHECK_LOGGEDIN_STATE_DURATION);
});
+watchEffect(async () => {
+ if (authStore.currentUserId) {
+ await useWorkspaceV1Store().fetchIamPolicy();
+ }
+});
+
onErrorCaptured((error: any /* , _, info */) => {
// Handle grpc request error.
// It looks like: `{"path":"/bytebase.v1.AuthService/Login","code":2,"details":"Response closed without headers"}`
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index 2629bd7868..dee9ef8c25 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -1,6 +1,6 @@
import axios from "axios";
import { defineStore } from "pinia";
-import { computed } from "vue";
+import { computed, ref } from "vue";
import { authServiceClient } from "@/grpcweb";
import type { SignupInfo, ActivateInfo } from "@/types";
import { unknownUser } from "@/types";
@@ -14,11 +14,11 @@ import { useUserStore } from ".";
export const useAuthStore = defineStore("auth_v1", () => {
const userStore = useUserStore();
+ const currentUserId = ref<number | undefined>();
const currentUser = computed(() => {
- const userId = getIntCookie("user");
- if (userId) {
- return userStore.getUserById(`${userId}`) ?? unknownUser();
+ if (currentUserId.value) {
+ return userStore.getUserById(`${currentUserId.value}`) ?? unknownUser();
}
return unknownUser();
});
@@ -35,9 +35,9 @@ export const useAuthStore = defineStore("auth_v1", () => {
return mfaTempToken;
}
- const userId = getIntCookie("user");
- if (userId) {
- await useUserStore().getOrFetchUserById(String(userId));
+ currentUserId.value = getIntCookie("user");
+ if (currentUserId.value) {
+ await useUserStore().getOrFetchUserById(String(currentUserId.value));
}
};
@@ -60,6 +60,7 @@ export const useAuthStore = defineStore("auth_v1", () => {
const logout = async () => {
try {
await axios.post("/v1/auth/logout");
+ currentUserId.value = undefined;
} catch {
// nothing
}
@@ -72,6 +73,8 @@ export const useAuthStore = defineStore("auth_v1", () => {
})
).data.data;
+ currentUserId.value = activatedUser.id;
+
// Refresh the corresponding user.
const user = await useUserStore().getOrFetchUserById(
String(activatedUser.id)
@@ -80,10 +83,10 @@ export const useAuthStore = defineStore("auth_v1", () => {
};
const restoreUser = async () => {
- const userId = getIntCookie("user");
- if (userId) {
+ currentUserId.value = getIntCookie("user");
+ if (currentUserId.value) {
await useUserStore().getOrFetchUserById(
- String(userId),
+ String(currentUserId.value),
true // silent
);
}
@@ -100,6 +103,7 @@ export const useAuthStore = defineStore("auth_v1", () => {
return {
currentUser,
+ currentUserId,
isLoggedIn,
login,
signup,