Skip to content

Commit 0f83056

Browse files
authored
fix(core): revert #11050 (#11469)
* fix(core): revert #11050 Added a test to avoid regression * Update callback.test.ts * Update index.ts
1 parent c188390 commit 0f83056

File tree

4 files changed

+105
-21
lines changed

4 files changed

+105
-21
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"dev": "pnpm css && pnpm providers && tsc -w",
9999
"test:e2e": "playwright test -c ../utils/playwright.config.ts",
100100
"test": "vitest run -c ../utils/vitest.config.ts",
101+
"test:watch": "vitest -c ../utils/vitest.config.ts",
101102
"providers": "node scripts/generate-providers"
102103
},
103104
"devDependencies": {

packages/core/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export class InvalidCallbackUrl extends AuthError {
193193
* 1. The user is redirected to the signin page, with `error=CredentialsSignin&code=credentials` in the URL. `code` is configurable.
194194
* 2. If you throw this error in a framework that handles form actions server-side, this error is thrown, instead of redirecting the user, so you'll need to handle.
195195
*/
196-
export class CredentialsSignin extends Error {
196+
export class CredentialsSignin extends SignInError {
197197
static type = "CredentialsSignin"
198198
/**
199199
* The error code that is set in the `code` query parameter of the redirect URL.

packages/core/src/lib/actions/callback/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,8 @@ export async function callback(
324324
)
325325
const user = userFromAuthorize
326326

327-
if (!user) {
328-
console.error(
329-
"Read more at https://errors.authjs.dev/#credentialssignin"
330-
)
331-
throw new CredentialsSignin()
332-
} else user.id = user.id?.toString() ?? crypto.randomUUID()
327+
if (!user) throw new CredentialsSignin()
328+
else user.id = user.id?.toString() ?? crypto.randomUUID()
333329

334330
const account = {
335331
providerAccountId: user.id,

packages/core/test/actions/callback.test.ts

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import GitHub from "../../src/providers/github.js"
66
import Credentials from "../../src/providers/credentials.js"
77

88
import { makeAuthRequest } from "../utils.js"
9+
import { skipCSRFCheck } from "../../src/index.js"
10+
import { CredentialsSignin } from "../../src/errors.js"
911

1012
describe("assert GET callback action", () => {
1113
beforeEach(() => {
@@ -66,22 +68,107 @@ describe("assert GET callback action", () => {
6668
`https://login.example.com?state=${encodedState}`
6769
)
6870
})
69-
})
7071

71-
it("should redirect to the custom error page is custom error page is defined", async () => {
72-
const { response } = await makeAuthRequest({
73-
action: "callback",
74-
path: "/credentials",
75-
config: {
76-
pages: {
77-
error: "/custom/error",
72+
it("should redirect to the custom error page is custom error page is defined", async () => {
73+
const { response } = await makeAuthRequest({
74+
action: "callback",
75+
path: "/credentials",
76+
config: {
77+
pages: {
78+
error: "/custom/error",
79+
},
80+
providers: [Credentials],
7881
},
79-
providers: [Credentials],
80-
},
82+
})
83+
84+
expect(response.status).toEqual(302)
85+
expect(response.headers.get("location")).toEqual(
86+
`https://authjs.test/custom/error?error=Configuration`
87+
)
8188
})
89+
})
90+
91+
describe("assert POST callback action", () => {
92+
describe("Credentials provider", () => {
93+
it("should return error=CredentialSignin and code=credentials if authorize returns null", async () => {
94+
const { response } = await makeAuthRequest({
95+
action: "callback",
96+
path: "/credentials",
97+
body: {
98+
username: "foo",
99+
password: "bar",
100+
},
101+
config: {
102+
skipCSRFCheck,
103+
providers: [
104+
Credentials({
105+
authorize() {
106+
return null
107+
},
108+
}),
109+
],
110+
},
111+
})
112+
113+
expect(response.status).toEqual(302)
114+
expect(response.headers.get("location")).toEqual(
115+
`https://authjs.test/auth/signin?error=CredentialsSignin&code=credentials`
116+
)
117+
})
82118

83-
expect(response.status).toEqual(302)
84-
expect(response.headers.get("location")).toEqual(
85-
`https://authjs.test/custom/error?error=Configuration`
86-
)
119+
it("should return error=Configuration if authorize throws an Error that is not extending from CredentialSignin", async () => {
120+
const { response } = await makeAuthRequest({
121+
action: "callback",
122+
path: "/credentials",
123+
body: {
124+
username: "foo",
125+
password: "bar",
126+
},
127+
config: {
128+
skipCSRFCheck,
129+
providers: [
130+
Credentials({
131+
authorize() {
132+
throw new Error()
133+
},
134+
}),
135+
],
136+
},
137+
})
138+
139+
expect(response.status).toEqual(302)
140+
expect(response.headers.get("location")).toEqual(
141+
`https://authjs.test/auth/error?error=Configuration`
142+
)
143+
})
144+
145+
it("should return error=CredentialsSignin and code=custom if authorize throws an custom Error that is extending from CredentialSignin", async () => {
146+
class CustomSigninError extends CredentialsSignin {
147+
code = "custom"
148+
}
149+
const { response } = await makeAuthRequest({
150+
action: "callback",
151+
path: "/credentials",
152+
body: {
153+
username: "foo",
154+
password: "bar",
155+
},
156+
config: {
157+
skipCSRFCheck,
158+
providers: [
159+
Credentials({
160+
authorize() {
161+
throw new CustomSigninError()
162+
},
163+
}),
164+
],
165+
},
166+
})
167+
168+
expect(response.status).toEqual(302)
169+
expect(response.headers.get("location")).toEqual(
170+
`https://authjs.test/auth/signin?error=CredentialsSignin&code=custom`
171+
)
172+
})
173+
})
87174
})

0 commit comments

Comments
 (0)