Skip to content

Commit

Permalink
fix(core): ignore basePath if pages configuration (#10288)
Browse files Browse the repository at this point in the history
* fix: pages configuration

* fix: typo

* chore: test custom pages config

---------

Co-authored-by: Thang Vu <hi@thvu.dev>
  • Loading branch information
chungweileong94 and ThangHuuVu committed Mar 25, 2024
1 parent 86c8822 commit 9c56e69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/index.ts
Expand Up @@ -183,8 +183,9 @@ export async function Auth(
if (error instanceof CredentialsSignin) params.set("code", error.code)

const pageKind = (isAuthError && error.kind) || "error"
const pagePath = config.pages?.[pageKind] ?? `/${pageKind.toLowerCase()}`
const url = `${internalRequest.url.origin}${config.basePath}${pagePath}?${params}`
const pagePath =
config.pages?.[pageKind] ?? `${config.basePath}/${pageKind.toLowerCase()}`
const url = `${internalRequest.url.origin}${pagePath}?${params}`

if (isRedirect) return Response.json({ url })
return Response.redirect(url)
Expand Down
19 changes: 19 additions & 0 deletions packages/core/test/actions/callback.test.ts
Expand Up @@ -3,6 +3,7 @@ import * as jose from "jose"
import * as o from "oauth4webapi"

import GitHub from "../../src/providers/github.js"
import Credentials from "../../src/providers/credentials.js"

import { makeAuthRequest } from "../utils.js"

Expand Down Expand Up @@ -66,3 +67,21 @@ describe("assert GET callback action", () => {
)
})
})

it("should redirect to the custom error page is custom error page is defined", async () => {
const { response } = await makeAuthRequest({
action: "callback",
path: "/credentials",
config: {
pages: {
error:'/custom/error'
},
providers: [Credentials],
},
})

expect(response.status).toEqual(302)
expect(response.headers.get("location")).toEqual(
`https://authjs.test/custom/error?error=Configuration`
)
})

0 comments on commit 9c56e69

Please sign in to comment.