Skip to content

Commit 4537491

Browse files
Exposes explicit entry points for app/pages routers
Introduces explicit subpath imports for Next.js App Router and Pages Router environments, enhancing clarity and enabling future optimizations. All existing imports continue to work unchanged. Adds `createAppRouterSession` and `createPagesRouterSession` helpers for syntactic sugar.
1 parent 60721e5 commit 4537491

4 files changed

Lines changed: 70 additions & 20 deletions

File tree

readme.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,39 @@ You will need to add your Kinde credentials to the generated `.env.local` file i
3434

3535
By contributing to Kinde, you agree that your contributions will be licensed under its MIT License.
3636

37-
## New Explicit Import Entry Points (App vs Pages Router)
37+
## Explicit Import Entry Points (App vs Pages Router)
3838

39-
We have introduced **additive, non-breaking** explicit subpath imports to make it clearer which APIs you are using in a Next.js App Router vs Pages Router environment. All existing imports continue to work unchanged.
39+
To make router intent clear (and enable future optimizations), you can now use router‑specific subpath imports. All existing imports still work unchanged.
40+
41+
### Imports
4042

41-
### Available existing imports
4243
```
44+
# Existing (unchanged)
4345
@kinde-oss/kinde-auth-nextjs
4446
@kinde-oss/kinde-auth-nextjs/server
4547
@kinde-oss/kinde-auth-nextjs/components
4648
@kinde-oss/kinde-auth-nextjs/middleware
47-
```
4849
49-
### New additive imports
50-
```
50+
# New (additive)
5151
@kinde-oss/kinde-auth-nextjs/app
5252
@kinde-oss/kinde-auth-nextjs/app/server
5353
@kinde-oss/kinde-auth-nextjs/pages
5454
@kinde-oss/kinde-auth-nextjs/pages/server
5555
```
5656

57-
### When to use which
58-
- Use `.../app` in client components or RSC-compatible code for the App Router.
59-
- Use `.../app/server` inside App Router server contexts (route handlers, server actions) when you need helpers like `getKindeServerSession`.
60-
- Use `.../pages` and `.../pages/server` analogously when working in the traditional Pages Router.
57+
### Which path to use
58+
59+
| Path | Use in | Typical usage |
60+
|------|--------|---------------|
61+
| `.../app` | App Router client/RSC | Provider, hooks, link components |
62+
| `.../app/server` | App Router server code (route handlers, server actions) | Session + auth helpers |
63+
| `.../pages` | Pages Router client code | Same client exports as root |
64+
| `.../pages/server` | Pages API routes / SSR utilities | Session + auth helpers |
6165

6266
### Behavior parity
63-
Currently these new paths **re-export the same implementations** as the legacy generic paths. This guarantees there is no behavior change. Future minor versions may begin optimizing these new entrypoints (e.g., lighter dependencies) while keeping the old ones stable; any divergence will be documented.
67+
All new subpaths currently re-export the exact same implementations as the legacy ones. No behavior change, no deprecations yet. Future minor versions may optimize these bundles; any divergence will be documented.
6468

65-
### Migration (optional at this stage)
66-
You can start switching to the explicit paths for clarity:
69+
### Migration (optional)
6770
```ts
6871
// Before (still valid)
6972
import { getKindeServerSession } from '@kinde-oss/kinde-auth-nextjs/server';
@@ -72,4 +75,27 @@ import { getKindeServerSession } from '@kinde-oss/kinde-auth-nextjs/server';
7275
import { getKindeServerSession } from '@kinde-oss/kinde-auth-nextjs/app/server';
7376
```
7477

75-
No deprecation warnings are emitted yet (it is in the pipeline); this is a clarity & future-proofing step.
78+
### Helper wrappers (syntactic sugar)
79+
80+
```ts
81+
import { createAppRouterSession } from '@kinde-oss/kinde-auth-nextjs/app/server';
82+
import { createPagesRouterSession } from '@kinde-oss/kinde-auth-nextjs/pages/server';
83+
84+
// App Router
85+
const appSession = createAppRouterSession();
86+
const token = await appSession.getAccessToken?.();
87+
88+
// Pages Router
89+
export default async function handler(req, res) {
90+
const pageSession = createPagesRouterSession(req, res);
91+
const user = await pageSession.getUser?.();
92+
res.status(200).json({ user });
93+
}
94+
```
95+
96+
Wrappers simply call `getKindeServerSession`; they:
97+
- Make router context explicit
98+
- Provide future extension points
99+
- Assist gradual migration from Pages to App Router
100+
101+
You can ignore them and continue using `getKindeServerSession` directly if you prefer.

src/app/server/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
// New App Router server entrypoint (additive, non-breaking)
2-
// For now, simply re-exports existing server APIs.
32

43
export { default as getKindeServerSession } from "../../session";
54
export { withAuth } from "../../authMiddleware/authMiddleware";
65
export { createKindeManagementAPIClient } from "../../api-client";
76
export { default as handleAuth } from "../../handlers/auth";
87
export { protectApi, protectPage } from "../../handlers/protect";
9-
export { LoginLink, CreateOrgLink, LogoutLink, RegisterLink, PortalLink } from "../../components";
10-
11-
// Types
8+
export {
9+
LoginLink,
10+
CreateOrgLink,
11+
LogoutLink,
12+
RegisterLink,
13+
PortalLink,
14+
} from "../../components";
1215
export * from "../../types";
16+
17+
// Wrapper (syntactic sugar)
18+
import getKindeServerSession from "../../session";
19+
20+
export const createAppRouterSession = () => {
21+
return getKindeServerSession();
22+
};

src/pages/server/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
// New Pages Router server entrypoint (additive, non-breaking)
2-
// Re-exports the same server utilities; semantic path highlights Pages Router context.
32

43
export { default as getKindeServerSession } from "../../session";
54
export { withAuth } from "../../authMiddleware/authMiddleware";
65
export { createKindeManagementAPIClient } from "../../api-client";
76
export { default as handleAuth } from "../../handlers/auth";
87
export { protectApi, protectPage } from "../../handlers/protect";
9-
export { LoginLink, CreateOrgLink, LogoutLink, RegisterLink, PortalLink } from "../../components";
8+
export {
9+
LoginLink,
10+
CreateOrgLink,
11+
LogoutLink,
12+
RegisterLink,
13+
PortalLink,
14+
} from "../../components";
1015
export * from "../../types";
16+
17+
// Wrapper (syntactic sugar)
18+
import getKindeServerSession from "../../session";
19+
20+
export const createPagesRouterSession = (req?: any, res?: any) => {
21+
return getKindeServerSession(req, res);
22+
};

tests/entrypoints/app-pages-entrypoints.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('New app/pages entrypoints (non-breaking additive)', () => {
1414
const mod = await import('@kinde-oss/kinde-auth-nextjs/app/server');
1515
expect(mod.getKindeServerSession).toBeTypeOf('function');
1616
expect(mod.withAuth).toBeTypeOf('function');
17+
expect(mod.createAppRouterSession).toBeTypeOf("function");
1718
});
1819

1920
it('pages client exports', async () => {
@@ -26,5 +27,6 @@ describe('New app/pages entrypoints (non-breaking additive)', () => {
2627
const mod = await import('@kinde-oss/kinde-auth-nextjs/pages/server');
2728
expect(mod.getKindeServerSession).toBeTypeOf('function');
2829
expect(mod.withAuth).toBeTypeOf('function');
30+
expect(mod.createPagesRouterSession).toBeTypeOf("function");
2931
});
3032
});

0 commit comments

Comments
 (0)