Skip to content

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

chore(frontend): introduce grpc-web auth interceptor middleware (#6543)

Liu Jia942c2d161d6
diff --git a/frontend/src/grpcweb/index.ts b/frontend/src/grpcweb/index.ts
index 3afb8bf8f0..8e8bb8c57f 100644
--- a/frontend/src/grpcweb/index.ts
+++ b/frontend/src/grpcweb/index.ts
@@ -3,6 +3,7 @@ import {
createClientFactory,
FetchTransport,
} from "nice-grpc-web";
+import { authInterceptorMiddleware } from "./middlewares";
import { AuthServiceDefinition } from "@/types/proto/v1/auth_service";
import { RoleServiceDefinition } from "@/types/proto/v1/role_service";
import { IdentityProviderServiceDefinition } from "@/types/proto/v1/idp_service";
@@ -36,7 +37,7 @@ const channel = createChannel(
})
);
-const clientFactory = createClientFactory();
+const clientFactory = createClientFactory().use(authInterceptorMiddleware);
export const authServiceClient = clientFactory.create(
AuthServiceDefinition,
diff --git a/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
new file mode 100644
index 0000000000..35bc3727a6
--- /dev/null
+++ b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
@@ -0,0 +1,54 @@
+import { ClientError, ServerError, Status } from "nice-grpc-common";
+import { ClientMiddleware } from "nice-grpc-web";
+
+import { useAuthStore } from "@/store";
+import { router } from "@/router";
+
+/**
+ * Way to define a grpc-web middleware
+ * ClientMiddleware<CallOptionsExt = {}, RequiredCallOptionsExt = {}>
+ * See
+ * - https://github.com/deeplay-io/nice-grpc/blob/master/packages/nice-grpc-client-middleware-deadline/src/index.ts
+ * - https://github.com/deeplay-io/nice-grpc/tree/master/packages/nice-grpc-web#middleware
+ * as an example.
+ */
+export const authInterceptorMiddleware: ClientMiddleware = async function* (
+ call,
+ options
+) {
+ if (!call.responseStream) {
+ try {
+ const response = yield* call.next(call.request, options);
+ return response;
+ } catch (error) {
+ await handleError(error);
+ }
+ } else {
+ try {
+ for await (const response of call.next(call.request, options)) {
+ yield response;
+ }
+ } catch (error) {
+ await handleError(error);
+ }
+
+ return;
+ }
+};
+
+const handleError = async (error: unknown) => {
+ if (error instanceof ClientError || error instanceof ServerError) {
+ if (error.code === Status.UNAUTHENTICATED) {
+ // "Kick out" sign in status if access token expires.
+ try {
+ await useAuthStore().logout();
+ } finally {
+ router.push({ name: "auth.signin" });
+ }
+ } else if (error.code === Status.PERMISSION_DENIED) {
+ // Jump to 403 page
+ router.push({ name: "error.403" });
+ }
+ }
+ throw error;
+};
diff --git a/frontend/src/grpcweb/middlewares/index.ts b/frontend/src/grpcweb/middlewares/index.ts
new file mode 100644
index 0000000000..d46db769e6
--- /dev/null
+++ b/frontend/src/grpcweb/middlewares/index.ts
@@ -0,0 +1 @@
+export * from "./authInterceptorMiddleware";