Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(svelte): cannot parse action at /session #10219

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 13 additions & 27 deletions packages/frameworks-sveltekit/src/lib/actions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { redirect } from "@sveltejs/kit"
import type { RequestEvent } from "@sveltejs/kit"
import { parse } from "set-cookie-parser"
import { dev } from "$app/environment"
import { env } from "$env/dynamic/private"

import { Auth, raw, skipCSRFCheck } from "@auth/core"
import type { AuthAction } from "@auth/core/types"
import { Auth, createActionURL, raw, skipCSRFCheck } from "@auth/core"

Check warning on line 6 in packages/frameworks-sveltekit/src/lib/actions.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/actions.ts#L6

Added line #L6 was not covered by tests
import type { SvelteKitAuthConfig } from "./types"
import { setEnvDefaults } from "./env"

Expand All @@ -17,7 +15,7 @@
config: SvelteKitAuthConfig,
event: RequestEvent
) {
const { request } = event
const { request, url: { protocol } } = event

Check warning on line 18 in packages/frameworks-sveltekit/src/lib/actions.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/actions.ts#L18

Added line #L18 was not covered by tests
const headers = new Headers(request.headers)
const {
redirect: shouldRedirect = true,
Expand All @@ -26,7 +24,13 @@
} = options instanceof FormData ? Object.fromEntries(options) : options

const callbackUrl = redirectTo?.toString() ?? headers.get("Referer") ?? "/"
const base = createActionURL("signin", headers, config.basePath)
const base = createActionURL(
"signin",
protocol,
headers,
env,
config.basePath
)

Check warning on line 33 in packages/frameworks-sveltekit/src/lib/actions.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/actions.ts#L27-L33

Added lines #L27 - L33 were not covered by tests

if (!provider) {
const url = `${base}?${new URLSearchParams({ callbackUrl })}`
Expand Down Expand Up @@ -78,11 +82,11 @@
config: SvelteKitAuthConfig,
event: RequestEvent
) {
const { request } = event
const { request, url: { protocol } } = event

Check warning on line 85 in packages/frameworks-sveltekit/src/lib/actions.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/actions.ts#L85

Added line #L85 was not covered by tests
const headers = new Headers(request.headers)
headers.set("Content-Type", "application/x-www-form-urlencoded")

const url = createActionURL("signout", headers, config.basePath)
const url = createActionURL("signout", protocol, headers, env, config.basePath)

Check warning on line 89 in packages/frameworks-sveltekit/src/lib/actions.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/actions.ts#L89

Added line #L89 was not covered by tests
const callbackUrl = options?.redirectTo ?? headers.get("Referer") ?? "/"
const body = new URLSearchParams({ callbackUrl })
const req = new Request(url, { method: "POST", headers, body })
Expand All @@ -105,9 +109,9 @@
setEnvDefaults(env, config)
config.trustHost ??= true

const { request: req } = event
const { request: req, url: { protocol } } = event

Check warning on line 112 in packages/frameworks-sveltekit/src/lib/actions.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/actions.ts#L112

Added line #L112 was not covered by tests

const sessionUrl = createActionURL("session", req.headers, config.basePath)
const sessionUrl = createActionURL("session", protocol, req.headers, env, config.basePath)

Check warning on line 114 in packages/frameworks-sveltekit/src/lib/actions.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/actions.ts#L114

Added line #L114 was not covered by tests
const request = new Request(sessionUrl, {
headers: { cookie: req.headers.get("cookie") ?? "" },
})
Expand All @@ -127,21 +131,3 @@
if (status === 200) return data
throw new Error(data.message)
}

/**
* Extract the origin and base path from either `AUTH_URL` or `NEXTAUTH_URL` environment variables,
* or the request's headers and the {@link NextAuthConfig.basePath} option.
*/
export function createActionURL(
action: AuthAction,
headers: Headers,
basePath?: string
) {
let url = env.AUTH_URL
if (!url) {
const host = headers.get("x-forwarded-host") ?? headers.get("host")
const proto = headers.get("x-forwarded-proto")
url = `${proto === "http" || dev ? "http" : "https"}://${host}${basePath}`
}
return new URL(`${url.replace(/\/$/, "")}/${action}`)
}
Loading