Skip to content

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

Try to restore previously logged in user on startup.

天舟03f39f27c43a
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 8a45bd815b..279655a9fd 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -12,6 +12,11 @@ export default {
setup(props, ctx) {
const store = useStore();
+ // Restore previously logged in user if applicable.
+ store.dispatch("auth/restoreUser").catch((error: Error) => {
+ console.log(error);
+ });
+
onErrorCaptured((e: any, _, info) => {
store.dispatch("notification/pushNotification", {
module: "bytebase",
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index 4cd8b37af6..286c006756 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -23,7 +23,7 @@ function convert(user: ResourceObject): User {
}
const state: () => AuthState = () => ({
- currentUser: GUEST,
+ currentUser: undefined,
});
const getters = {
@@ -82,17 +82,19 @@ const actions = {
return activatedUser;
},
- async fetchCurrentUser({ commit }: any) {
- const currentUser = convert((await axios.get("/api/user/1")).data.data);
-
- localStorage.setItem("bb.auth.user", JSON.stringify(currentUser));
- commit("setCurrentUser", currentUser);
- return currentUser;
+ async restoreUser({ commit }: any) {
+ const jsonUser = localStorage.getItem("bb.auth.user");
+ if (jsonUser) {
+ const user: User = JSON.parse(jsonUser);
+ commit("setCurrentUser", user);
+ return user;
+ }
+ return undefined;
},
async logout({ commit }: any) {
localStorage.removeItem("bb.auth.user");
- commit("setCurrentUser", GUEST);
+ commit("setCurrentUser", undefined);
return GUEST;
},
};
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 8e94ae885a..4c52883269 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -327,7 +327,7 @@ export type NotificationFilter = {
// Store
export interface AuthState {
- currentUser: User | null;
+ currentUser: User | undefined;
}
export interface RoleMappingState {