Skip to content

Commit 537e096

Browse files
authored
fix: Clear auto-login cookie on logout (#10528)
* add clear cookie session after logout * fix jest test
1 parent d3f95a3 commit 537e096

4 files changed

Lines changed: 80 additions & 2 deletions

File tree

src/frontend/src/stores/authStore.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
LANGFLOW_REFRESH_TOKEN,
88
} from "@/constants/constants";
99
import type { AuthStoreType } from "@/types/zustand/auth";
10-
import { getCookiesInstance } from "@/utils/cookie-manager";
10+
import { cookieManager, getCookiesInstance } from "@/utils/cookie-manager";
1111

1212
const cookies = getCookiesInstance();
1313
const useAuthStore = create<AuthStoreType>((set, get) => ({
@@ -33,6 +33,8 @@ const useAuthStore = create<AuthStoreType>((set, get) => ({
3333
localStorage.removeItem(LANGFLOW_API_TOKEN);
3434
localStorage.removeItem(LANGFLOW_REFRESH_TOKEN);
3535

36+
cookieManager.clearAuthCookies();
37+
3638
get().setIsAuthenticated(false);
3739
get().setIsAdmin(false);
3840

src/frontend/src/utils/__tests__/cookie-manager.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ describe("CookieManager", () => {
289289
it("should clear all auth-related cookies", () => {
290290
cookieManager.clearAuthCookies();
291291

292-
expect(mockCookiesInstance.remove).toHaveBeenCalledTimes(3);
292+
expect(mockCookiesInstance.remove).toHaveBeenCalledTimes(4);
293293
expect(mockCookiesInstance.remove).toHaveBeenCalledWith(
294294
"access_token_lf",
295295
{
@@ -314,6 +314,11 @@ describe("CookieManager", () => {
314314
sameSite: "lax",
315315
},
316316
);
317+
expect(mockCookiesInstance.remove).toHaveBeenCalledWith("auto_login_lf", {
318+
path: "/",
319+
secure: false,
320+
sameSite: "lax",
321+
});
317322
});
318323
});
319324

src/frontend/src/utils/cookie-manager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Cookies } from "react-cookie";
22
import {
33
LANGFLOW_ACCESS_TOKEN,
44
LANGFLOW_API_TOKEN,
5+
LANGFLOW_AUTO_LOGIN_OPTION,
56
LANGFLOW_REFRESH_TOKEN,
67
} from "@/constants/constants";
78

@@ -71,6 +72,7 @@ class CookieManager {
7172
this.remove(LANGFLOW_ACCESS_TOKEN);
7273
this.remove(LANGFLOW_API_TOKEN);
7374
this.remove(LANGFLOW_REFRESH_TOKEN);
75+
this.remove(LANGFLOW_AUTO_LOGIN_OPTION);
7476
}
7577
}
7678

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect, test } from "../../fixtures";
2+
3+
test(
4+
"user must not be able to login after logout and refresh the page when auto_login is false",
5+
{ tag: ["@release", "@api"] },
6+
async ({ page }) => {
7+
await page.route("**/api/v1/auto_login", (route) => {
8+
route.fulfill({
9+
status: 500,
10+
contentType: "application/json",
11+
body: JSON.stringify({
12+
detail: { auto_login: false },
13+
}),
14+
});
15+
});
16+
17+
await page.addInitScript(() => {
18+
window.process = window.process || {};
19+
20+
const newEnv = { ...window.process.env, LANGFLOW_AUTO_LOGIN: "false" };
21+
22+
Object.defineProperty(window.process, "env", {
23+
value: newEnv,
24+
writable: true,
25+
configurable: true,
26+
});
27+
28+
sessionStorage.setItem("testMockAutoLogin", "true");
29+
});
30+
31+
await page.goto("/");
32+
33+
await page.waitForSelector("text=sign in to langflow", { timeout: 30000 });
34+
35+
await page.getByPlaceholder("Username").fill("langflow");
36+
await page.getByPlaceholder("Password").fill("langflow");
37+
38+
await page.evaluate(() => {
39+
sessionStorage.removeItem("testMockAutoLogin");
40+
});
41+
42+
await page.getByRole("button", { name: "Sign In" }).click();
43+
44+
await page.waitForSelector('[data-testid="mainpage_title"]', {
45+
timeout: 30000,
46+
});
47+
48+
await page.getByTestId("user-profile-settings").click();
49+
50+
await page.evaluate(() => {
51+
sessionStorage.setItem("testMockAutoLogin", "true");
52+
});
53+
54+
await page.getByText("Logout", { exact: true }).click();
55+
56+
await page.waitForTimeout(1000);
57+
58+
await page.reload();
59+
60+
await page.waitForSelector("text=sign in to langflow", { timeout: 30000 });
61+
62+
const isLoggedIn = await page
63+
.getByTestId("mainpage_title")
64+
.isVisible()
65+
.catch(() => false);
66+
67+
expect(isLoggedIn).toBeFalsy();
68+
},
69+
);

0 commit comments

Comments
 (0)