Skip to content

Commit

Permalink
fix: support Next.js 13 (#5710)
Browse files Browse the repository at this point in the history
* Migrate dev app to Next.js Version 13

* Update core types

* Fix middleware

#5649

* Use new ResponseCookie API

vercel/next.js#41526
  • Loading branch information
HaNdTriX authored and balazsorban44 committed Nov 4, 2022
1 parent 30ad639 commit 060953d
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 189 deletions.
4 changes: 1 addition & 3 deletions apps/dev/components/footer.js
Expand Up @@ -17,9 +17,7 @@ export default function Footer() {
<a href="https://github.com/nextauthjs/next-auth-example">GitHub</a>
</li>
<li className={styles.navItem}>
<Link href="/policy">
<a>Policy</a>
</Link>
<Link href="/policy">Policy</Link>
</li>
<li className={styles.navItem}>
<em>{packageJSON.version}</em>
Expand Down
36 changes: 9 additions & 27 deletions apps/dev/components/header.js
Expand Up @@ -64,49 +64,31 @@ export default function Header() {
<nav>
<ul className={styles.navItems}>
<li className={styles.navItem}>
<Link href="/">
<a>Home</a>
</Link>
<Link href="/">Home</Link>
</li>
<li className={styles.navItem}>
<Link href="/client">
<a>Client</a>
</Link>
<Link href="/client">Client</Link>
</li>
<li className={styles.navItem}>
<Link href="/server">
<a>Server</a>
</Link>
<Link href="/server">Server</Link>
</li>
<li className={styles.navItem}>
<Link href="/protected">
<a>Protected</a>
</Link>
<Link href="/protected">Protected</Link>
</li>
<li className={styles.navItem}>
<Link href="/protected-ssr">
<a>Protected(SSR)</a>
</Link>
<Link href="/protected-ssr">Protected(SSR)</Link>
</li>
<li className={styles.navItem}>
<Link href="/api-example">
<a>API</a>
</Link>
<Link href="/api-example">API</Link>
</li>
<li className={styles.navItem}>
<Link href="/credentials">
<a>Credentials</a>
</Link>
<Link href="/credentials">Credentials</Link>
</li>
<li className={styles.navItem}>
<Link href="/email">
<a>Email</a>
</Link>
<Link href="/email">Email</Link>
</li>
<li className={styles.navItem}>
<Link href="/middleware-protected">
<a>Middleware protected</a>
</Link>
<Link href="/middleware-protected">Middleware protected</Link>
</li>
</ul>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/package.json
Expand Up @@ -19,7 +19,7 @@
"@next-auth/typeorm-legacy-adapter": "workspace:*",
"@prisma/client": "^3",
"faunadb": "^4",
"next": "12.3.1",
"next": "13.0.1",
"next-auth": "workspace:*",
"nodemailer": "^6",
"react": "^18",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-auth/package.json
Expand Up @@ -120,7 +120,7 @@
"jest-environment-jsdom": "^28.1.1",
"jest-watch-typeahead": "^1.1.0",
"msw": "^0.42.3",
"next": "12.3.1",
"next": "13.0.1",
"postcss": "^8.4.14",
"postcss-cli": "^9.1.0",
"postcss-nested": "^5.0.6",
Expand Down
20 changes: 14 additions & 6 deletions packages/next-auth/src/core/lib/cookie.ts
@@ -1,6 +1,7 @@
import type { IncomingHttpHeaders } from "http"
import type { CookiesOptions } from "../.."
import type { CookieOption, LoggerInstance, SessionStrategy } from "../types"
import type { NextRequest } from "next/server"
import type { NextApiRequest } from "next"

// Uncomment to recalculate the estimated size
// of an empty session cookie
Expand Down Expand Up @@ -128,10 +129,10 @@ export class SessionStore {

constructor(
option: CookieOption,
req: {
cookies?: Partial<Record<string, string> | Map<string, string>>
headers?: Headers | IncomingHttpHeaders | Record<string, string>
},
req: Partial<{
cookies: NextRequest["cookies"] | NextApiRequest["cookies"]
headers: NextRequest["headers"] | NextApiRequest["headers"]
}>,
logger: LoggerInstance | Console
) {
this.#logger = logger
Expand All @@ -140,7 +141,14 @@ export class SessionStore {
const { cookies } = req
const { name: cookieName } = option

if (cookies instanceof Map) {
if (typeof cookies?.getAll === "function") {
// Next.js ^v13.0.1 (Edge Env)
for (const { name, value } of cookies.getAll()) {
if (name.startsWith(cookieName)) {
this.#chunks[name] = value
}
}
} else if (cookies instanceof Map) {
for (const name of cookies.keys()) {
if (name.startsWith(cookieName)) this.#chunks[name] = cookies.get(name)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next-auth/src/next/middleware.ts
Expand Up @@ -184,7 +184,7 @@ export type WithAuthArgs =
* [Documentation](https://next-auth.js.org/configuration/nextjs#middleware)
*/
export function withAuth(...args: WithAuthArgs) {
if (!args.length || args[0] instanceof NextRequest) {
if (!args.length || args[0] instanceof Request) {
// @ts-expect-error
return handleMiddleware(...args)
}
Expand Down

0 comments on commit 060953d

Please sign in to comment.