Skip to content

Fix: 13142 - Replace only the pathname from signin to callback instead of the whole url #13162

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions apps/dev/nextjs/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { signIn, auth } from "auth"
import { SessionProvider } from "next-auth/react"
import Client from "../client"
export default async function Page() {
const session = await auth()

return (
<div className="container">
<h1>Login Form Sample</h1>
<p>This sample shows how to login using `auth` module with custom form</p>
<div className="card">
<div className="card-header">
<h3> Login </h3>
</div>
<div className="card-body">
{session ? (
<pre>{JSON.stringify(session, null, 2)}</pre>
) : (
<form
action={async (formData) => {
"use server"
await signIn("credentials", formData)
}}
>
<input
id="password"
type="password"
name="password"
placeholder="Enter password"
required
minLength={6}
/>
<div className="btn-wrapper">
<button>Sign In</button>
</div>
</form>
)}
</div>
</div>
</div>
)
}
23 changes: 18 additions & 5 deletions apps/dev/nextjs/app/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ main {
}

.card-body {
padding-bottom: 1.1rem;

&:has(button) {
padding: 1.1rem;
}
padding: 1.1rem;
}

.card-footer {
Expand Down Expand Up @@ -129,4 +125,21 @@ main {
button:hover {
background-color: #ccc;
}

input {
padding: 0.7rem 0.8rem;
border: 1px solid #ccc;
border-radius: 0.5rem;
font-size: 1rem;
line-height: 1.4rem;
background-color: #fff;
color: black;
margin-bottom: 1rem;
}

input:focus {
outline: none;
border-color: #999;
background-color: #fafafa;
}
}
3 changes: 3 additions & 0 deletions apps/dev/nextjs/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export function Header({
<Link className={styles.navItem} href="/dashboard">
Dashboard (app)
</Link>
<Link className={styles.navItem} href="/login">
Login (app)
</Link>
<Link className={styles.navItem} href="/policy">
Policy (pages)
</Link>
Expand Down
6 changes: 3 additions & 3 deletions apps/dev/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
},
"license": "ISC",
"dependencies": {
"next": "15.3.1",
"next": "15.4.5",
"next-auth": "workspace:*",
"react": "19.0.0-rc-4c58fce7-20240904",
"react-dom": "19.0.0-rc-4c58fce7-20240904"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.23",
Expand Down
4 changes: 3 additions & 1 deletion packages/next-auth/src/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export async function signIn(
}

if (foundProvider.type === "credentials") {
url = url.replace("signin", "callback")
const urlObj = new URL(url)
urlObj.pathname = urlObj.pathname.replace("signin", "callback")
url = urlObj.href
}

headers.set("Content-Type", "application/x-www-form-urlencoded")
Expand Down
40 changes: 39 additions & 1 deletion packages/next-auth/test/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
import {
describe,
it,
expect,
vi,
beforeEach,
afterEach,
beforeAll,
} from "vitest"
import NextAuth, { NextAuthConfig } from "../src"
// TODO: Move the MemoryAdapter to utils package
import { MemoryAdapter } from "../../core/test/memory-adapter"
import Nodemailer from "@auth/core/providers/nodemailer"
import Credentials from "@auth/core/providers/credentials"

let mockedHeaders = vi.hoisted(() => {
return new globalThis.Headers()
Expand Down Expand Up @@ -106,4 +115,33 @@ describe("signIn action", () => {
)
})
})
describe("with credentials provider", () => {
beforeEach(() => {
nextAuth = NextAuth({
...config,
providers: [
Credentials({
credentials: { password: { label: "Password", type: "password" } },
authorize(c) {
if (c.password !== "password") return null
return {
id: "test",
name: "Test User",
email: "[email protected]",
}
},
}),
],
})
})
it("follow redirects and callback signin", async () => {
await nextAuth?.signIn("credentials", { password: "password" })
expect(mockRedirect).toHaveBeenCalledWith("http://localhost/")
})
it("only replaces signin on path name", async () => {
process.env.AUTH_URL = "http://signin"
await nextAuth?.signIn("credentials", { password: "password" })
expect(mockRedirect).toHaveBeenCalledWith("http://signin/")
})
})
})
Loading