Skip to content

Commit 3d91fe8

Browse files
authored
fix(site): fix redirection to login after logout/change password (#6870)
* fix(site): fix redirection to login after logout/change password * chore: add login verification assert * prettier
1 parent 90da09b commit 3d91fe8

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

site/.eslintrc.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ overrides:
4343
# https://coder.com/docs/v2/latest/contributing/frontend#tests-getting-too-slow.
4444
testing-library/no-node-access: off
4545
testing-library/no-container: off
46+
- files: ["e2e/**/*.[tj]s"]
47+
extends: ["plugin:testing-library/react", "plugin:testing-library/dom"]
48+
rules:
49+
# Sometimes the eslint-plugin-testing-library believes playwright queries are
50+
# also react-testing-library queries, which is not the case. So we disable this
51+
# rule for all e2e tests.
52+
testing-library/prefer-screen-queries: "off"
4653
root: true
4754
rules:
4855
"@typescript-eslint/brace-style":
@@ -156,7 +163,6 @@ rules:
156163
message: "Default React import not allowed",
157164
},
158165
]
159-
160166
settings:
161167
react:
162168
version: detect

site/e2e/tests/logout.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { test, expect } from "@playwright/test"
2+
import { getStatePath } from "../helpers"
3+
4+
test.use({ storageState: getStatePath("authState") })
5+
6+
test("signing out redirects to login page", async ({ page, baseURL }) => {
7+
await page.goto(`${baseURL}/`, { waitUntil: "networkidle" })
8+
9+
await page.getByTestId("user-dropdown-trigger").click()
10+
await page.getByRole("menuitem", { name: "Sign Out" }).click()
11+
12+
await expect(
13+
page.getByRole("heading", { name: "Sign in to Coder" }),
14+
).toBeVisible()
15+
16+
expect(page.url()).toMatch(/\/login$/) // ensure we're on the login page with no query params
17+
})

site/src/pages/UserSettingsPage/SecurityPage/SecurityPage.test.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ describe("SecurityPage", () => {
4747
expect(successMessage).toBeDefined()
4848
expect(API.updateUserPassword).toBeCalledTimes(1)
4949
expect(API.updateUserPassword).toBeCalledWith(user.id, newData)
50+
51+
await waitFor(() => expect(window.location).toBeAt("/"))
5052
})
5153
})
5254

site/src/xServices/auth/authXService.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,10 @@ export const authMachine =
389389
}),
390390
redirect: (_, { data }) => {
391391
if (!("redirectUrl" in data)) {
392-
throw new Error(
393-
"Redirect only should be called with data.redirectUrl",
394-
)
392+
window.location.href = location.origin
393+
} else {
394+
window.location.replace(data.redirectUrl)
395395
}
396-
397-
window.location.replace(data.redirectUrl)
398396
},
399397
},
400398
guards: {

site/src/xServices/userSecuritySettings/userSecuritySettingsXService.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const userSecuritySettingsMachine = createMachine(
3535
src: "updateSecurity",
3636
onDone: [
3737
{
38-
actions: "notifyUpdate",
38+
actions: ["notifyUpdate", "redirectToHome"],
3939
target: "idle",
4040
},
4141
],
@@ -66,6 +66,9 @@ export const userSecuritySettingsMachine = createMachine(
6666
assignError: assign({
6767
error: (_, event) => event.data,
6868
}),
69+
redirectToHome: () => {
70+
window.location.href = location.origin
71+
},
6972
},
7073
},
7174
)

0 commit comments

Comments
 (0)