Skip to content

Commit 44f2a47

Browse files
JuneezeeThangHuuVu
andauthoredSep 18, 2022
fix(middleware): use includes() for NextAuth pages (#5104)
* fix(middleware): use `includes()` for NextAuth pages Some users could be setting their `signIn` and `error` pages option to `/` to disable the automatically generated pages, as suggested in [1]. This commit reverts the behaviour for matching `signIn` and `error` pages in `handleMiddleware` to pre-v4.10.3. ``` const signInPage = "/" const errorPage = "/" const publicPaths = [signInPage, errorPage, "/_next", "/favicon.ico"] // pathname = "/" will return true publicPaths.some((p) => pathname.startsWith(p)) ``` Fixes: aedabc8 ("fix: avoid redirect on always public paths") Reference [1]: #2330 (reply in thread) Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * test(middleware): add tests for public paths Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Co-authored-by: Thang Vu <thvu@hey.com>
1 parent a3b92db commit 44f2a47

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
 

‎packages/next-auth/src/next/middleware.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ async function handleMiddleware(
106106
const signInPage = options?.pages?.signIn ?? "/api/auth/signin"
107107
const errorPage = options?.pages?.error ?? "/api/auth/error"
108108
const basePath = parseUrl(process.env.NEXTAUTH_URL).path
109-
const publicPaths = [signInPage, errorPage, "/_next", "/favicon.ico"]
109+
const publicPaths = ["/_next", "/favicon.ico"]
110110

111111
// Avoid infinite redirects/invalid response
112112
// on paths that never require authentication
113113
if (
114114
pathname.startsWith(basePath) ||
115+
[signInPage, errorPage].includes(pathname) ||
115116
publicPaths.some((p) => pathname.startsWith(p))
116117
) {
117118
return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { NextMiddleware } from "next/server"
2+
import { NextAuthMiddlewareOptions, withAuth } from "../next/middleware"
3+
4+
it("should not match pages as public paths", async () => {
5+
const options: NextAuthMiddlewareOptions = {
6+
pages: {
7+
signIn: "/",
8+
error: "/"
9+
},
10+
secret: "secret"
11+
}
12+
13+
const nextUrl: any = {
14+
pathname: "/protected/pathA",
15+
search: "",
16+
origin: "http://127.0.0.1"
17+
}
18+
const req: any = { nextUrl, headers: { authorization: "" } }
19+
20+
const handleMiddleware = withAuth(options) as NextMiddleware
21+
const res = await handleMiddleware(req, null)
22+
expect(res).toBeDefined()
23+
expect(res.status).toBe(307)
24+
})
25+
26+
it("should not redirect on public paths", async () => {
27+
const options: NextAuthMiddlewareOptions = {
28+
secret: "secret"
29+
}
30+
const nextUrl: any = {
31+
pathname: "/_next/foo",
32+
search: "",
33+
origin: "http://127.0.0.1"
34+
}
35+
const req: any = { nextUrl, headers: { authorization: "" } }
36+
37+
const handleMiddleware = withAuth(options) as NextMiddleware
38+
const res = await handleMiddleware(req, null)
39+
expect(res).toBeUndefined()
40+
})

0 commit comments

Comments
 (0)
Please sign in to comment.