Skip to content

Commit a9d4d2b

Browse files
author
Saravana Kumar Rajendran
committed
refactor: move clerk mock mutation to util
1 parent 9521f82 commit a9d4d2b

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Clerk Token Refresh Behavior
3+
slug: /clerk-token-refresh
4+
---
5+
6+
This page summarizes how Langflow handles access token refreshes in different authentication modes and what state changes occur when using Clerk.
7+
8+
## Refresh scenarios when Clerk authentication is disabled
9+
10+
When `CLERK_AUTH_ENABLED` is `false`, Langflow relies on its internal authentication. Tokens are refreshed in these situations:
11+
12+
1. **Protected routes** – the `ProtectedRoute` component periodically calls the `/refresh` endpoint using the `useRefreshAccessToken` hook.
13+
2. **API requests** – the API interceptor automatically triggers the same hook when a request fails with a `401` or `403` error.
14+
15+
Both cases invoke the `useRefreshAccessToken` hook which posts to `/refresh` and updates the refresh token cookie.
16+
The default interval used by `ProtectedRoute` is defined by `LANGFLOW_ACCESS_TOKEN_EXPIRE_SECONDS` (54 minutes by default) or the value of the `ACCESS_TOKEN_EXPIRE_SECONDS` environment variable.
17+
18+
## State updates when using Clerk
19+
20+
When `CLERK_AUTH_ENABLED` is `true`, token refresh from the frontend is skipped. After signing in with Clerk, `ClerkAuthAdapter` logs the user in to the backend and calls `login()` from the authentication context, which updates several states:
21+
22+
| State location | Value after Clerk token update |
23+
|----------------|--------------------------------|
24+
| `access_token_lf` cookie | Clerk session token |
25+
| `refresh_token_lf` cookie | Backend refresh token |
26+
| `auto_login_lf` cookie | `"login"` |
27+
| Local storage `access_token_lf` | Clerk session token |
28+
| `authStore.accessToken` | Clerk session token |
29+
| `authStore.isAuthenticated` | `true` |
30+
| `authContext.userData` | populated from `/users/whoami` |
31+
32+
With Clerk enabled the periodic refresh is disabled and all further state changes rely on Clerk sessions.

src/frontend/src/controllers/API/queries/auth/use-post-refresh-access.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IS_CLERK_AUTH } from "@/constants/clerk";
2+
import { mockClerkMutation } from "@/utils/mocks/mockMutationClerk";
13
import { IS_AUTO_LOGIN, LANGFLOW_REFRESH_TOKEN } from "@/constants/constants";
24
import useAuthStore from "@/stores/authStore";
35
import { useMutationFunctionType } from "@/types/api";
@@ -20,6 +22,10 @@ export const useRefreshAccessToken: useMutationFunctionType<
2022
const cookies = new Cookies();
2123
const autoLogin = useAuthStore((state) => state.autoLogin);
2224

25+
if (IS_CLERK_AUTH) {
26+
return mockClerkMutation;
27+
}
28+
2329
async function refreshAccess(): Promise<IRefreshAccessToken> {
2430
const res = await api.post<IRefreshAccessToken>(`${getURL("REFRESH")}`);
2531
cookies.set(LANGFLOW_REFRESH_TOKEN, res.data.refresh_token, { path: "/" });
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const mockClerkMutation = {
2+
mutate: () => {},
3+
mutateAsync: async () => undefined,
4+
isError: false,
5+
isIdle: true,
6+
isPending: false,
7+
isSuccess: true,
8+
reset: () => {},
9+
status: "success",
10+
variables: undefined,
11+
data: undefined,
12+
error: null,
13+
} as any;

0 commit comments

Comments
 (0)