Skip to content

Commit 34b8995

Browse files
authored
fix: remove unnecessary /signout appending to signout form action URL (#9901)
* fix: remove unnecessary /signout appending to signout form action URL * fix: default to /signout * fix: default to /auth/signout * fix: add tests for signout page * fix: more testing with mocking component that returns jsx * fix: cleanup core/pages test * fix: export SigninProps * fix: add preact vitest plugin and cleanup pages rendering tests * Discard changes to packages/core/src/lib/pages/signin.tsx * fix: signout page, default to instance of URL * fix: add optional chain to url * fix: update fixture * fix: fixture AuthConfig * fix: cleanup pages test
1 parent 8cbf38c commit 34b8995

File tree

6 files changed

+305
-8
lines changed

6 files changed

+305
-8
lines changed

packages/core/src/lib/pages/signout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function SignoutPage(props: SignoutProps) {
3737
{theme?.logo && <img src={theme.logo} alt="Logo" className="logo" />}
3838
<h1>Signout</h1>
3939
<p>Are you sure you want to sign out?</p>
40-
<form action={`${url}/signout`} method="POST">
40+
<form action={url?.toString()} method="POST">
4141
<input type="hidden" name="csrfToken" value={csrfToken} />
4242
<button id="submitButton" type="submit">
4343
Sign out
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { AuthConfig } from "../../src/types"
2+
3+
const userProfile = {
4+
id: "abc",
5+
name: "Fill Murray",
6+
7+
image: "https://source.boringavatars.com/marble",
8+
}
9+
10+
export const authOptions = {
11+
debug: false,
12+
pages: {},
13+
theme: {
14+
colorScheme: "auto" as const,
15+
logo: "",
16+
brandColor: "",
17+
buttonText: "",
18+
},
19+
session: {
20+
strategy: "jwt" as const,
21+
maxAge: 2592000,
22+
updateAge: 86400,
23+
generateSessionToken: () => "abc123",
24+
},
25+
providers: [
26+
{
27+
id: "github",
28+
name: "GitHub",
29+
type: "oauth" as const,
30+
authorization: `https://github.com/login/oauth/authorize`,
31+
token: `https://github.com/login/oauth/access_token`,
32+
userinfo: {
33+
url: `https://github.com/user`,
34+
async request() {
35+
return userProfile
36+
},
37+
},
38+
profile: () => userProfile,
39+
style: { logo: "", bg: "", text: "" },
40+
clientId: "abc",
41+
clientSecret: "abc",
42+
redirectProxyUrl: undefined,
43+
checks: [],
44+
account: () => {},
45+
},
46+
],
47+
basePath: "/auth",
48+
secret: ["abc"],
49+
redirectProxyUrl: undefined,
50+
trustHost: true,
51+
adapter: undefined,
52+
logger: {
53+
error: (msg: Error) => console.error(msg),
54+
},
55+
experimental: {},
56+
} satisfies AuthConfig

packages/core/test/pages.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { vi, expect, it, describe, beforeEach } from "vitest"
2+
import renderPage from "../src/lib/pages/index"
3+
import { authOptions } from "./fixtures/pages"
4+
import { init } from "../src/lib/init"
5+
6+
describe("pages", () => {
7+
describe("sign-out", () => {
8+
beforeEach(() => {
9+
vi.resetAllMocks()
10+
})
11+
12+
it("should attempt to render signout page", async () => {
13+
// Generated when visiting `/auth/signout`
14+
const { options } = await init({
15+
authOptions,
16+
action: "signout",
17+
providerId: "github",
18+
url: new URL("http://localhost:3000/auth/signout"),
19+
cookies: {},
20+
isPost: true,
21+
csrfDisabled: true,
22+
})
23+
24+
const render = renderPage({ ...options, query: {}, cookies: [] })
25+
const signOutPage = render.signout()
26+
27+
expect(signOutPage.body).toContain(`<title>Sign Out</title>`)
28+
expect(signOutPage.body).toContain(
29+
`action="http://localhost:3000/auth/signout"`
30+
)
31+
})
32+
33+
it("should return correct URL for signout page form action", async () => {
34+
// When visiting `/auth/signout`, for example
35+
const { options } = await init({
36+
authOptions: authOptions,
37+
action: "signout",
38+
providerId: "github",
39+
url: new URL("http://localhost:3000/auth/signout"),
40+
cookies: {},
41+
isPost: true,
42+
csrfDisabled: true,
43+
})
44+
45+
expect(options.url).toBeInstanceOf(URL)
46+
expect(options.url.toString()).toBe("http://localhost:3000/auth/signout")
47+
})
48+
})
49+
describe("sign-in", () => {
50+
beforeEach(() => {
51+
vi.resetAllMocks()
52+
})
53+
54+
it("should attempt to render signin page", async () => {
55+
// Generated when visiting `/auth/signin`
56+
const { options } = await init({
57+
authOptions: authOptions,
58+
action: "signin",
59+
providerId: "github",
60+
url: new URL("http://localhost:3000/auth/signin"),
61+
cookies: {},
62+
isPost: true,
63+
csrfDisabled: true,
64+
})
65+
66+
const render = renderPage({ ...options, query: {}, cookies: [] })
67+
const signInPage = render.signin()
68+
69+
expect(signInPage.body).toContain(`<title>Sign In</title>`)
70+
expect(signInPage.body).toContain(
71+
`action="http://localhost:3000/auth/signin/github"`
72+
)
73+
})
74+
})
75+
})

packages/utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"private": true,
44
"version": "0.0.0",
55
"devDependencies": {
6-
"@auth/core": "workspace:*"
6+
"@auth/core": "workspace:*",
7+
"@preact/preset-vite": "^2.8.1"
78
},
89
"type": "module",
910
"dependencies": {

packages/utils/vitest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { defineConfig } from "vite"
44
import swc from "unplugin-swc"
5+
import preact from "@preact/preset-vite"
56

67
// https://vitejs.dev/config/
78
export default defineConfig({
@@ -16,5 +17,5 @@ export default defineConfig({
1617
},
1718
setupFiles: ["../utils/vitest-setup.ts"],
1819
},
19-
plugins: [swc.vite()],
20+
plugins: [swc.vite(), preact()],
2021
})

0 commit comments

Comments
 (0)