Skip to content

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

Introduce auth/isLoggedIn and returns true if “user” cookie has valid value

天舟26eff4808d01
diff --git a/backend/server/jwt.go b/backend/server/jwt.go
index 3f721e7bff..a5830f4e5e 100644
--- a/backend/server/jwt.go
+++ b/backend/server/jwt.go
@@ -29,7 +29,7 @@ const (
// Suppose we have a valid refresh token, we should refresh the token in 2 cases:
// 1. The access token is about to expire in <<refreshThresholdDuration>>
// 2. The access token has already expired, we refresh the token so that the ongoing request can pass through
- cookieExpDuration = refreshTokenDuration - 1*time.Minute
+ cookieExpDuration = 24 * time.Hour //refreshTokenDuration - 1*time.Minute
// The key name used to store jwt token in the context
tokenContextKey = "token"
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 0826d8e41c..5b8de990c6 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -545,8 +545,7 @@ export const router = createRouter({
});
router.beforeEach((to, from, next) => {
- const loginUser: Principal = store.getters["auth/currentUser"]();
- const isGuest = loginUser.id == "0";
+ const isLoggedIn = store.getters["auth/isLoggedIn"]();
const fromModule = from.name
? from.name.toString().split(".")[0]
@@ -563,7 +562,7 @@ router.beforeEach((to, from, next) => {
to.name === ACTIVATE_MODULE ||
to.name === PASSWORD_RESET_MODULE
) {
- if (!isGuest) {
+ if (isLoggedIn) {
next({ name: HOME_MODULE, replace: true });
} else {
if (to.name === ACTIVATE_MODULE) {
@@ -581,12 +580,13 @@ router.beforeEach((to, from, next) => {
}
return;
} else {
- if (isGuest) {
+ if (!isLoggedIn) {
next({ name: SIGNIN_MODULE, replace: true });
return;
}
}
+ const loginUser: Principal = store.getters["auth/currentUser"]();
if (to.name === "workspace.environment" || to.name === "workspace.instance") {
if (isDBAOrOwner(loginUser.role)) {
next();
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index d3066b9798..a722cd889c 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -1,5 +1,6 @@
import axios from "axios";
import isEqual from "lodash-es/isEqual";
+import isEmpty from "lodash-es/isEmpty";
import {
Principal,
AuthState,
@@ -7,32 +8,23 @@ import {
SignupInfo,
ActivateInfo,
ResourceObject,
- empty,
- EMPTY_ID,
+ unknown,
} from "../../types";
-
-const GUEST: Principal = {
- id: EMPTY_ID,
- creator: empty("PRINCIPAL") as Principal,
- updater: empty("PRINCIPAL") as Principal,
- createdTs: 0,
- updatedTs: 0,
- status: "ACTIVE",
- type: "END_USER",
- name: "Guest",
- email: "[email protected]",
- role: "GUEST",
-};
+import { getCookie } from "../../utils";
function convert(user: ResourceObject, rootGetters: any): Principal {
return rootGetters["principal/principalById"](user.id);
}
const state: () => AuthState = () => ({
- currentUser: GUEST,
+ currentUser: unknown("PRINCIPAL") as Principal,
});
const getters = {
+ isLoggedIn: (state: AuthState) => (): boolean => {
+ return !isEmpty(getCookie("user"));
+ },
+
currentUser: (state: AuthState) => (): Principal => {
return state.currentUser;
},
@@ -102,7 +94,7 @@ const actions = {
commit("setCurrentUser", user);
return user;
}
- return GUEST;
+ return unknown("PRINCIPAL") as Principal;
},
async refreshUser({ commit, state, rootGetters }: any) {
@@ -118,8 +110,8 @@ const actions = {
async logout({ commit }: any) {
localStorage.removeItem("bb.auth.user");
- commit("setCurrentUser", GUEST);
- return GUEST;
+ commit("setCurrentUser", unknown("PRINCIPAL") as Principal);
+ return unknown("PRINCIPAL") as Principal;
},
};
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index 16fb2d9032..0ee1136ebc 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -79,3 +79,16 @@ export function randomString(n?: number): string {
}
return result;
}
+
+export function getCookie(name: string): string {
+ const list = document.cookie.split(";");
+ for (var i = 0; i < list.length; i++) {
+ const parts = list[i].split("=");
+ if (parts[0] == name) {
+ // For now, just assumes strings are enclosed by quotes
+ return parts.length > 1 ? parts[1].slice(1, -1) : "";
+ }
+ }
+
+ return "";
+}