Skip to content

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

Handle jwt expiration and auto log out

天舟ab2e8e70f0f3
diff --git a/backend/server/jwt.go b/backend/server/jwt.go
index 57d522d1e0..3da8cbe347 100644
--- a/backend/server/jwt.go
+++ b/backend/server/jwt.go
@@ -200,5 +200,5 @@ func TokenMiddleware(l *bytebase.Logger, next echo.HandlerFunc) echo.HandlerFunc
// JWTErrorChecker will be executed when user try to access a protected path.
func JWTErrorChecker(l *bytebase.Logger, err error, c echo.Context) error {
l.Logf(bytebase.INFO, "Unauthorized to access protected route %s, err: %v", c.Path(), err)
- return fmt.Errorf("invalid access token: %w", err)
+ return echo.NewHTTPError(http.StatusUnauthorized, "Invalid access token.").SetInternal(err)
}
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 8db1e91f68..f6332f1e9a 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -12,15 +12,20 @@
<script lang="ts">
import { reactive, watchEffect, onErrorCaptured } from "vue";
import { useStore } from "vuex";
+import { useRouter } from "vue-router";
import ProvideContext from "./components/ProvideContext.vue";
import { isDev } from "./utils";
import { Notification } from "./types";
import { BBNotificationItem } from "./bbkit/types";
+// Check expiration every 30 sec and logout if expired
+const CHECK_LOGGEDIN_STATE_DURATION = 30 * 1000;
+
const NOTIFICAITON_DURATION = 8000;
interface LocalState {
notificationList: BBNotificationItem[];
+ prevLoggedIn: boolean;
}
export default {
@@ -28,11 +33,25 @@ export default {
components: { ProvideContext },
setup(props, ctx) {
const store = useStore();
+ const router = useRouter();
const state = reactive<LocalState>({
notificationList: [],
+ prevLoggedIn: store.getters["auth/isLoggedIn"](),
});
+ setInterval(() => {
+ const loggedIn = store.getters["auth/isLoggedIn"]();
+ if (state.prevLoggedIn != loggedIn) {
+ state.prevLoggedIn = loggedIn;
+ if (!loggedIn) {
+ store.dispatch("auth/logout").then(() => {
+ router.push({ name: "auth.signin" });
+ });
+ }
+ }
+ }, CHECK_LOGGEDIN_STATE_DURATION);
+
const removeNotification = (id: string) => {
state.notificationList.shift();
};
diff --git a/frontend/src/main.ts b/frontend/src/main.ts
index 6c601a3cac..64a4791a2d 100644
--- a/frontend/src/main.ts
+++ b/frontend/src/main.ts
@@ -112,15 +112,28 @@ axios.interceptors.response.use(
},
(error) => {
if (error.response) {
- if (error.response.data.message) {
- store.dispatch("notification/pushNotification", {
- module: "bytebase",
- style: "CRITICAL",
- title: error.response.data.message,
+ if (error.response.status == 401) {
+ // Because frontend won't allow visiting unauthorized page and send request
+ // to unauthorized api endpoint (e.g. a Develoepr role requesting DBA endpoint).
+ // So if it's a 401, it's likely the user tries to visit a proper endpoint, however
+ // the jwt token has expired. App.vue has a timer to check if the user is still
+ // considered loggin and will automatically logout if not. But we may still run into
+ // this situation if the frontend code hasn't cleared the cookie determining the logged
+ // state in time. So we force a logout here to catch this case.
+ store.dispatch("auth/logout").then(() => {
+ router.push({ name: "auth.signin" });
});
+ } else {
+ if (error.response.data.message) {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "CRITICAL",
+ title: error.response.data.message,
+ });
+ }
+ throw error;
}
}
- throw error;
}
);