Skip to content
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

[auth-nextjs] Async Request APIs, compat with Next.js 15 #1137

Merged
merged 7 commits into from
Dec 2, 2024

Conversation

MartinCura
Copy link
Contributor

@MartinCura MartinCura commented Nov 29, 2024

This PR adapts the @edgedb/auth-nextjs package for the [kinda*] breaking change of Next.js 15, where Request APIs like cookies and params are now async.

To solve this, i simply added await to all corresponding places. Awaiting something sync is not strictly a noop, but for these cases it shouldn't change behavior afaict.

Tested in my own project with both Next.js before their breaking change and after it.

Breaking change: As NextAppAuth.getSession() has to await cookies(), it is now an async function, which means those upgrading have to add await when calling it.

const auth = createAuth(client, {
  baseUrl: `http://localhost:${process.env.PORT || 3000}`,
  authCookieName: "edgedb-session",
  pkceVerifierCookieName: "edgedb-pkce-verifier",
})

export const getCurrentUser = async () => {
- const session = auth.getSession()
+ const session = await auth.getSession()
  const userQuery = e.select(e.User, () => ({
    ...e.User["*"],
    filter_single: { id: e.global.current_user.id },
  }))
  return await userQuery.run(session.client)
}

This necessitates that getSession is now async, which is a breaking change, because it accesses the cookies.

*kinda because as a compatibility layer it is still possible to not await it, but it's not what you want to do.

@edgedb-cla
Copy link

edgedb-cla bot commented Nov 29, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

package.json Show resolved Hide resolved
packages/auth-nextjs/src/app/index.ts Show resolved Hide resolved
packages/auth-nextjs/src/shared.ts Outdated Show resolved Hide resolved
@1st1
Copy link
Member

1st1 commented Nov 29, 2024

Thank you, we'll take a look soon!

Copy link
Collaborator

@scotttrinh scotttrinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for taking on this mechanical work! It's been on our "next up" list for a few weeks now.

@@ -153,8 +159,9 @@ export abstract class NextAuth extends NextAuthHelpers {
return {
GET: async (
req: NextRequest,
{ params }: { params: { auth: string[] } },
ctx: { params: Promise<{ auth: string[] }> },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work in <15 or will those users need to do a type-cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. In runtime it shouldn't be a problem because it would just be awaiting a non-Promise; and i didn't notice any type error when i tried it in <15, but i can check again jic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not worried too much about runtime, and even if it breaks backwards compatibility, I'm fine with shipping it, I just want to understand everything that might break here so we can update the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the GET is exported in a route.ts to act as a Next.js Route Handler but it's not type checked against anything. I can run and build the project running Next.js before their breaking change with this version, no errors. And i don't know of any way that someone could be type-checking this and get a warning/error after this change.

At most we could add a small note to the docs referencing the change introduced by Next.js 15 and how this small library targets the new behaviour but supports both.

packages/auth-nextjs/src/shared.ts Outdated Show resolved Hide resolved
@MartinCura
Copy link
Contributor Author

MartinCura commented Dec 2, 2024

Thanks so much for taking on this mechanical work! It's been on our "next up" list for a few weeks now.

No worries! Glad to help and love what you guys are doing. I'm working on a sizable project by myself, so these kinds of helper libraries that help me ship faster are very welcome.

And i'm sure you're quite busy with EdgeDB Gel 6!

@scotttrinh scotttrinh merged commit 36da97d into edgedb:master Dec 2, 2024
10 checks passed
@MartinCura MartinCura deleted the nextjs_15 branch December 2, 2024 15:59
@MartinCura
Copy link
Contributor Author

MartinCura commented Dec 6, 2024

👋🏼 Small update: React 19 stable finally dropped, and with it a stable release of @types/react, so the overrides i added in this PR should not be needed anymore if you just point to the new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants