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

fix(nextjs-sdk): add support for NextJS 15 #885

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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@
"bash -c \"pnpm i\"",
"git add pnpm-lock.yaml"
]
}
},
"packageManager": "[email protected]+sha512.499434c9d8fdd1a2794ebf4552b3b25c0a633abcee5bb15e7b5de90f32f47b513aca98cd5cfd001c31f0db454bc3804edccd578501e4ca293a6816166bbd9f81"
}
4 changes: 2 additions & 2 deletions packages/sdks/nextjs-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Server Component:
import { session } from '@descope/nextjs-sdk/server';

async function Page() {
const sessionRes = session();
const sessionRes = await session();
if (!sessionRes) {
// ...
}
Expand All @@ -221,7 +221,7 @@ Route handler:
```js
// src/pages/api/routes.ts
export async function GET() {
const currSession = session();
const currSession = await session();
if (!currSession.isAuthenticated) {
// ...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const sdk = createSdk({
});

export const GET = async () => {
const currentSession = session();
const currentSession = await session();
if (!currentSession) {
return new Response('Unauthorized', { status: 401 });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdks/nextjs-sdk/examples/app-router/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Link from 'next/link';
import UserDetails from './UserDetails';

const Page = async () => {
const sessionRes = session();
const sessionRes = await session();

return (
<div>
Expand Down
2 changes: 1 addition & 1 deletion packages/sdks/nextjs-sdk/examples/app-router/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
7 changes: 6 additions & 1 deletion packages/sdks/nextjs-sdk/examples/app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
"license": "ISC",
"dependencies": {
"@descope/nextjs-sdk": "file:../..",
"next": "^14.2.10",
"next": "^15.1.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "22.10.5",
"@types/react": "19.0.4",
"typescript": "5.7.3"
}
}
2 changes: 1 addition & 1 deletion packages/sdks/nextjs-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"jest-fetch-mock": "^3.0.3",
"lint-staged": "^13.3.0",
"msw": "^2.1.7",
"next": "^14.2.10",
"next": "^15.1.4",
"rollup": "^2.79.1",
"rollup-plugin-auto-external": "^2.0.0",
"rollup-plugin-browsersync": "^1.0.0",
Expand Down
10 changes: 2 additions & 8 deletions packages/sdks/nextjs-sdk/src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { NextApiRequest } from 'next';
import { headers } from 'next/headers';
import { DESCOPE_SESSION_HEADER } from './constants';

// This type is declared to allow simpler migration to Next.15
// It will be removed in the future
type HeaderTypes = Awaited<ReturnType<typeof headers>>;

const extractSession = (
descopeSession?: string
): AuthenticationInfo | undefined => {
Expand All @@ -24,10 +20,8 @@ const extractSession = (
};
// returns the session token if it exists in the headers
// This function require middleware
export const session = (): AuthenticationInfo | undefined => {
// from Next.js 15, headers() returns a Promise
// It can still be used synchronously to facilitate migration
const reqHeaders = headers() as never as HeaderTypes;
export const session = async (): Promise<AuthenticationInfo | undefined> => {
Copy link
Member

Choose a reason for hiding this comment

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

Think break BC
Also for someone that is not using next 15

Let's think about how to achieve that breaking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems to maintain backwards compatibility and access the session synchronously server-side, we would need to switch off of headers, cookies, or any other API from NextJS that is now asynchronous. So doing the same thing where we set the session into whatever storage mechanism (like how it's headers right now) in middleware and accessing via session function.

Options are:

Thoughts on this? At some point we'll probably have to bite the bullet and make the session call async, but maybe once NextJS fully moves off?

const reqHeaders = await headers();
const sessionHeader = reqHeaders.get(DESCOPE_SESSION_HEADER);
return extractSession(sessionHeader);
};
Expand Down
8,388 changes: 8,388 additions & 0 deletions packages/sdks/nextjs-sdk/yarn.lock

Large diffs are not rendered by default.

Loading
Loading