@imtbl/auth-nextjs 2.12.4-alpha.5 → 2.12.4-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,104 @@
1
- export { withPageAuthRequired, getImmutableSession, type WithPageAuthRequiredFullOptions, type WithPageAuthRequiredProps, } from './with-page-auth';
2
- export type { WithPageAuthRequiredOptions } from '../types';
1
+ import { type NextRequest, NextResponse } from 'next/server';
2
+ import type { Session } from 'next-auth';
3
+ export { createImmutableAuth, createAuthConfig } from '../index';
4
+ /**
5
+ * Options for createAuthMiddleware
6
+ */
7
+ export interface AuthMiddlewareOptions {
8
+ /**
9
+ * URL to redirect to when not authenticated
10
+ * @default "/login"
11
+ */
12
+ loginUrl?: string;
13
+ /**
14
+ * Paths that should be protected (regex patterns)
15
+ * If not provided, middleware should be configured via Next.js matcher
16
+ */
17
+ protectedPaths?: (string | RegExp)[];
18
+ /**
19
+ * Paths that should be excluded from protection (regex patterns)
20
+ * Takes precedence over protectedPaths
21
+ */
22
+ publicPaths?: (string | RegExp)[];
23
+ }
24
+ /**
25
+ * Type for the auth function returned by createImmutableAuth
26
+ */
27
+ export type AuthFunction = () => Promise<Session | null>;
28
+ /**
29
+ * Create a Next.js middleware for protecting routes with Immutable authentication.
30
+ *
31
+ * This is the App Router replacement for `withPageAuthRequired`.
32
+ *
33
+ * @param auth - The auth function from createImmutableAuth
34
+ * @param options - Middleware options
35
+ * @returns A Next.js middleware function
36
+ *
37
+ * @example Basic usage with Next.js middleware:
38
+ * ```typescript
39
+ * // middleware.ts
40
+ * import { createAuthMiddleware } from "@imtbl/auth-nextjs/server";
41
+ * import { auth } from "@/lib/auth";
42
+ *
43
+ * export default createAuthMiddleware(auth, {
44
+ * loginUrl: "/login",
45
+ * });
46
+ *
47
+ * export const config = {
48
+ * matcher: ["/dashboard/:path*", "/profile/:path*"],
49
+ * };
50
+ * ```
51
+ *
52
+ * @example With path configuration:
53
+ * ```typescript
54
+ * export default createAuthMiddleware(auth, {
55
+ * loginUrl: "/login",
56
+ * protectedPaths: [/^\/dashboard/, /^\/profile/],
57
+ * publicPaths: [/^\/api\/public/],
58
+ * });
59
+ * ```
60
+ */
61
+ export declare function createAuthMiddleware(auth: AuthFunction, options?: AuthMiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
62
+ /**
63
+ * Higher-order function to protect a Server Action or Route Handler.
64
+ *
65
+ * The returned function forwards all arguments from Next.js to your handler,
66
+ * allowing access to the request, context, form data, or any other arguments.
67
+ *
68
+ * @param auth - The auth function from createImmutableAuth
69
+ * @param handler - The handler function to protect. Receives session as first arg,
70
+ * followed by any arguments passed by Next.js (request, context, etc.)
71
+ * @returns A protected handler that checks authentication before executing
72
+ *
73
+ * @example Protecting a Route Handler with request access:
74
+ * ```typescript
75
+ * // app/api/protected/route.ts
76
+ * import { withAuth } from "@imtbl/auth-nextjs/server";
77
+ * import { auth } from "@/lib/auth";
78
+ *
79
+ * export const POST = withAuth(auth, async (session, request: Request) => {
80
+ * const body = await request.json();
81
+ * return Response.json({ user: session.user, data: body });
82
+ * });
83
+ *
84
+ * export const GET = withAuth(auth, async (session, request: Request, context) => {
85
+ * const { params } = context;
86
+ * return Response.json({ user: session.user, params: await params });
87
+ * });
88
+ * ```
89
+ *
90
+ * @example Protecting a Server Action:
91
+ * ```typescript
92
+ * // app/actions.ts
93
+ * "use server";
94
+ * import { withAuth } from "@imtbl/auth-nextjs/server";
95
+ * import { auth } from "@/lib/auth";
96
+ *
97
+ * export const protectedAction = withAuth(auth, async (session, formData: FormData) => {
98
+ * const userId = session.user.sub;
99
+ * const name = formData.get("name");
100
+ * // ... your action logic
101
+ * });
102
+ * ```
103
+ */
104
+ export declare function withAuth<TArgs extends unknown[], TReturn>(auth: AuthFunction, handler: (session: Session, ...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
@@ -1,7 +1,7 @@
1
- import type { DefaultSession, DefaultUser, Session } from 'next-auth';
2
- import type { DefaultJWT } from 'next-auth/jwt';
1
+ import type { DefaultSession, Session } from 'next-auth';
2
+ import type { JWT } from 'next-auth/jwt';
3
3
  /**
4
- * Configuration for ImmutableAuthProvider and createAuthOptions
4
+ * Configuration for ImmutableAuthProvider and createImmutableAuth
5
5
  */
6
6
  export interface ImmutableAuthConfig {
7
7
  /**
@@ -45,11 +45,11 @@ export interface ImmutableUser {
45
45
  nickname?: string;
46
46
  }
47
47
  /**
48
- * NextAuth module augmentation to add Immutable-specific fields
48
+ * Auth.js v5 module augmentation to add Immutable-specific fields
49
49
  */
50
50
  declare module 'next-auth' {
51
51
  interface Session extends DefaultSession {
52
- user: ImmutableUser;
52
+ user: ImmutableUser & DefaultSession['user'];
53
53
  accessToken: string;
54
54
  refreshToken?: string;
55
55
  idToken?: string;
@@ -57,9 +57,10 @@ declare module 'next-auth' {
57
57
  zkEvm?: ZkEvmInfo;
58
58
  error?: string;
59
59
  }
60
- interface User extends DefaultUser {
60
+ interface User {
61
+ id: string;
61
62
  sub: string;
62
- email?: string;
63
+ email?: string | null;
63
64
  nickname?: string;
64
65
  accessToken: string;
65
66
  refreshToken?: string;
@@ -69,20 +70,20 @@ declare module 'next-auth' {
69
70
  }
70
71
  }
71
72
  declare module 'next-auth/jwt' {
72
- interface JWT extends DefaultJWT {
73
- sub: string;
74
- email?: string;
73
+ interface JWT {
74
+ sub?: string;
75
+ email?: string | null;
75
76
  nickname?: string;
76
- accessToken: string;
77
+ accessToken?: string;
77
78
  refreshToken?: string;
78
79
  idToken?: string;
79
- accessTokenExpires: number;
80
+ accessTokenExpires?: number;
80
81
  zkEvm?: ZkEvmInfo;
81
82
  error?: string;
82
83
  }
83
84
  }
84
85
  /**
85
- * Token data passed from client to NextAuth credentials provider
86
+ * Token data passed from client to Auth.js credentials provider
86
87
  */
87
88
  export interface ImmutableTokenData {
88
89
  accessToken: string;
@@ -127,11 +128,11 @@ export interface ImmutableAuthProviderProps {
127
128
  config: ImmutableAuthConfig;
128
129
  /**
129
130
  * Initial session from server (for SSR hydration)
130
- * Can be Session from getServerSession or any compatible session object
131
+ * Can be Session from auth() or any compatible session object
131
132
  */
132
133
  session?: Session | DefaultSession | null;
133
134
  /**
134
- * Custom base path for NextAuth API routes
135
+ * Custom base path for Auth.js API routes
135
136
  * Use this when you have multiple auth endpoints (e.g., per environment)
136
137
  * @default "/api/auth"
137
138
  */
@@ -146,7 +147,7 @@ export interface UseImmutableAuthReturn {
146
147
  */
147
148
  user: ImmutableUser | null;
148
149
  /**
149
- * Full NextAuth session with tokens
150
+ * Full Auth.js session with tokens
150
151
  */
151
152
  session: Session | null;
152
153
  /**
@@ -163,7 +164,7 @@ export interface UseImmutableAuthReturn {
163
164
  */
164
165
  signIn: (options?: import('@imtbl/auth').LoginOptions) => Promise<void>;
165
166
  /**
166
- * Sign out from both NextAuth and Immutable
167
+ * Sign out from both Auth.js and Immutable
167
168
  */
168
169
  signOut: () => Promise<void>;
169
170
  /**
@@ -175,18 +176,4 @@ export interface UseImmutableAuthReturn {
175
176
  */
176
177
  auth: import('@imtbl/auth').Auth | null;
177
178
  }
178
- /**
179
- * Options for withPageAuthRequired
180
- */
181
- export interface WithPageAuthRequiredOptions {
182
- /**
183
- * URL to redirect to when not authenticated
184
- * @default "/login"
185
- */
186
- loginUrl?: string;
187
- /**
188
- * URL to redirect to after login
189
- * @default current page
190
- */
191
- returnTo?: string | false;
192
- }
179
+ export type { JWT };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@imtbl/auth-nextjs",
3
- "version": "2.12.4-alpha.5",
4
- "description": "Next.js authentication integration for Immutable SDK using NextAuth",
3
+ "version": "2.12.4-alpha.7",
4
+ "description": "Next.js App Router authentication integration for Immutable SDK using Auth.js v5",
5
5
  "author": "Immutable",
6
6
  "bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
7
7
  "homepage": "https://github.com/immutable/ts-immutable-sdk#readme",
@@ -51,11 +51,11 @@
51
51
  "dist"
52
52
  ],
53
53
  "dependencies": {
54
- "@imtbl/auth": "2.12.4-alpha.5"
54
+ "@imtbl/auth": "2.12.4-alpha.7"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "next": "14.2.25",
58
- "next-auth": "^4.24.0",
58
+ "next-auth": "^5.0.0-beta.30",
59
59
  "react": "^18.2.0"
60
60
  },
61
61
  "devDependencies": {
@@ -67,7 +67,7 @@
67
67
  "jest": "^29.4.3",
68
68
  "jest-environment-jsdom": "^29.4.3",
69
69
  "next": "14.2.25",
70
- "next-auth": "^4.24.0",
70
+ "next-auth": "^5.0.0-beta.30",
71
71
  "react": "^18.2.0",
72
72
  "ts-node": "^10.9.1",
73
73
  "tsup": "^8.3.0",
@@ -1,94 +0,0 @@
1
- import type { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
2
- import type { IncomingMessage, ServerResponse } from 'http';
3
- import { type Session } from 'next-auth';
4
- import type { ImmutableAuthConfig, WithPageAuthRequiredOptions } from '../types';
5
- /**
6
- * Extended options for withPageAuthRequired
7
- */
8
- export interface WithPageAuthRequiredFullOptions<P extends Record<string, unknown> = Record<string, unknown>> extends WithPageAuthRequiredOptions {
9
- /**
10
- * Custom getServerSideProps that runs after auth check.
11
- * Session is guaranteed to exist when this runs.
12
- */
13
- getServerSideProps?: (ctx: GetServerSidePropsContext, session: Session) => Promise<GetServerSidePropsResult<P>>;
14
- }
15
- /**
16
- * Props added by withPageAuthRequired
17
- */
18
- export interface WithPageAuthRequiredProps {
19
- session: Session;
20
- }
21
- /**
22
- * Get the Immutable session on the server side.
23
- *
24
- * @example
25
- * ```typescript
26
- * // pages/api/user.ts
27
- * import { getImmutableSession } from "@imtbl/auth-nextjs/server";
28
- *
29
- * const config = { clientId: "...", redirectUri: "..." };
30
- *
31
- * export default async function handler(req, res) {
32
- * const session = await getImmutableSession(req, res, config);
33
- * if (!session) {
34
- * return res.status(401).json({ error: "Not authenticated" });
35
- * }
36
- * res.json({ user: session.user });
37
- * }
38
- * ```
39
- *
40
- * @example In getServerSideProps
41
- * ```typescript
42
- * export const getServerSideProps = async (ctx) => {
43
- * const session = await getImmutableSession(ctx.req, ctx.res, config);
44
- * return { props: { user: session?.user ?? null } };
45
- * };
46
- * ```
47
- */
48
- export declare function getImmutableSession(req: IncomingMessage & {
49
- cookies: Partial<Record<string, string>>;
50
- }, res: ServerResponse, config: ImmutableAuthConfig): Promise<Session | null>;
51
- /**
52
- * Higher-order function that protects a page with authentication.
53
- *
54
- * When a signed-out user visits the page:
55
- * 1. Server checks session via getServerSession() → returns null
56
- * 2. Returns HTTP redirect to login page with returnTo parameter
57
- * 3. After login, user is redirected back to original page
58
- *
59
- * @example Basic usage:
60
- * ```typescript
61
- * // pages/dashboard.tsx
62
- * import { withPageAuthRequired } from "@imtbl/auth-nextjs/server";
63
- *
64
- * const config = { clientId: "...", redirectUri: "..." };
65
- *
66
- * function DashboardPage() {
67
- * // Page only renders if user is authenticated
68
- * return <h1>Dashboard</h1>;
69
- * }
70
- *
71
- * export default DashboardPage;
72
- * export const getServerSideProps = withPageAuthRequired(config);
73
- * ```
74
- *
75
- * @example With additional data fetching:
76
- * ```typescript
77
- * export const getServerSideProps = withPageAuthRequired(config, {
78
- * async getServerSideProps(ctx, session) {
79
- * // session is guaranteed to exist here
80
- * const data = await fetchData(session.accessToken);
81
- * return { props: { data } };
82
- * },
83
- * });
84
- * ```
85
- *
86
- * @example With custom options:
87
- * ```typescript
88
- * export const getServerSideProps = withPageAuthRequired(config, {
89
- * loginUrl: "/auth/signin",
90
- * returnTo: "/dashboard",
91
- * });
92
- * ```
93
- */
94
- export declare function withPageAuthRequired<P extends Record<string, unknown> = Record<string, unknown>>(config: ImmutableAuthConfig, options?: WithPageAuthRequiredFullOptions<P>): GetServerSideProps<WithPageAuthRequiredProps & P>;