@imtbl/auth-nextjs 0.0.1-alpha.0 → 2.12.4-alpha.4

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.
@@ -0,0 +1,94 @@
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>;
@@ -0,0 +1,191 @@
1
+ import type { DefaultSession, DefaultUser, Session } from 'next-auth';
2
+ import type { DefaultJWT } from 'next-auth/jwt';
3
+ /**
4
+ * Configuration for ImmutableAuthProvider and createAuthOptions
5
+ */
6
+ export interface ImmutableAuthConfig {
7
+ /**
8
+ * Immutable OAuth client ID
9
+ */
10
+ clientId: string;
11
+ /**
12
+ * OAuth callback redirect URI
13
+ */
14
+ redirectUri: string;
15
+ /**
16
+ * Where to redirect after logout
17
+ */
18
+ logoutRedirectUri?: string;
19
+ /**
20
+ * OAuth audience (default: "platform_api")
21
+ */
22
+ audience?: string;
23
+ /**
24
+ * OAuth scopes (default: "openid profile email offline_access transact")
25
+ */
26
+ scope?: string;
27
+ /**
28
+ * Authentication domain (default: "https://auth.immutable.com")
29
+ */
30
+ authenticationDomain?: string;
31
+ }
32
+ /**
33
+ * zkEVM wallet information
34
+ */
35
+ export interface ZkEvmInfo {
36
+ ethAddress: string;
37
+ userAdminAddress: string;
38
+ }
39
+ /**
40
+ * User profile from Immutable
41
+ */
42
+ export interface ImmutableUser {
43
+ sub: string;
44
+ email?: string;
45
+ nickname?: string;
46
+ }
47
+ /**
48
+ * NextAuth module augmentation to add Immutable-specific fields
49
+ */
50
+ declare module 'next-auth' {
51
+ interface Session extends DefaultSession {
52
+ user: ImmutableUser;
53
+ accessToken: string;
54
+ refreshToken?: string;
55
+ idToken?: string;
56
+ accessTokenExpires: number;
57
+ zkEvm?: ZkEvmInfo;
58
+ error?: string;
59
+ }
60
+ interface User extends DefaultUser {
61
+ sub: string;
62
+ email?: string;
63
+ nickname?: string;
64
+ accessToken: string;
65
+ refreshToken?: string;
66
+ idToken?: string;
67
+ accessTokenExpires: number;
68
+ zkEvm?: ZkEvmInfo;
69
+ }
70
+ }
71
+ declare module 'next-auth/jwt' {
72
+ interface JWT extends DefaultJWT {
73
+ sub: string;
74
+ email?: string;
75
+ nickname?: string;
76
+ accessToken: string;
77
+ refreshToken?: string;
78
+ idToken?: string;
79
+ accessTokenExpires: number;
80
+ zkEvm?: ZkEvmInfo;
81
+ error?: string;
82
+ }
83
+ }
84
+ /**
85
+ * Token data passed from client to NextAuth credentials provider
86
+ */
87
+ export interface ImmutableTokenData {
88
+ accessToken: string;
89
+ refreshToken?: string;
90
+ idToken?: string;
91
+ accessTokenExpires: number;
92
+ profile: {
93
+ sub: string;
94
+ email?: string;
95
+ nickname?: string;
96
+ };
97
+ zkEvm?: ZkEvmInfo;
98
+ }
99
+ /**
100
+ * Response from the userinfo endpoint
101
+ * Used for server-side token validation
102
+ */
103
+ export interface UserInfoResponse {
104
+ /** Subject - unique user identifier */
105
+ sub: string;
106
+ /** User's email address */
107
+ email?: string;
108
+ /** User's nickname/username */
109
+ nickname?: string;
110
+ /** User's full name */
111
+ name?: string;
112
+ /** User's profile picture URL */
113
+ picture?: string;
114
+ /** When the user profile was last updated */
115
+ updated_at?: string;
116
+ /** Whether the email has been verified */
117
+ email_verified?: boolean;
118
+ }
119
+ /**
120
+ * Props for ImmutableAuthProvider
121
+ */
122
+ export interface ImmutableAuthProviderProps {
123
+ children: React.ReactNode;
124
+ /**
125
+ * Immutable auth configuration
126
+ */
127
+ config: ImmutableAuthConfig;
128
+ /**
129
+ * Initial session from server (for SSR hydration)
130
+ * Can be Session from getServerSession or any compatible session object
131
+ */
132
+ session?: Session | DefaultSession | null;
133
+ /**
134
+ * Custom base path for NextAuth API routes
135
+ * Use this when you have multiple auth endpoints (e.g., per environment)
136
+ * @default "/api/auth"
137
+ */
138
+ basePath?: string;
139
+ }
140
+ /**
141
+ * Return type of useImmutableAuth hook
142
+ */
143
+ export interface UseImmutableAuthReturn {
144
+ /**
145
+ * Current user profile (null if not authenticated)
146
+ */
147
+ user: ImmutableUser | null;
148
+ /**
149
+ * Full NextAuth session with tokens
150
+ */
151
+ session: Session | null;
152
+ /**
153
+ * Whether authentication state is loading
154
+ */
155
+ isLoading: boolean;
156
+ /**
157
+ * Whether user is authenticated
158
+ */
159
+ isAuthenticated: boolean;
160
+ /**
161
+ * Sign in with Immutable (opens popup)
162
+ */
163
+ signIn: () => Promise<void>;
164
+ /**
165
+ * Sign out from both NextAuth and Immutable
166
+ */
167
+ signOut: () => Promise<void>;
168
+ /**
169
+ * Get a valid access token (refreshes if needed)
170
+ */
171
+ getAccessToken: () => Promise<string>;
172
+ /**
173
+ * The underlying Auth instance (for advanced use)
174
+ */
175
+ auth: import('@imtbl/auth').Auth | null;
176
+ }
177
+ /**
178
+ * Options for withPageAuthRequired
179
+ */
180
+ export interface WithPageAuthRequiredOptions {
181
+ /**
182
+ * URL to redirect to when not authenticated
183
+ * @default "/login"
184
+ */
185
+ loginUrl?: string;
186
+ /**
187
+ * URL to redirect to after login
188
+ * @default current page
189
+ */
190
+ returnTo?: string | false;
191
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Extract the expiry timestamp from a JWT access token.
3
+ * Returns the expiry as a Unix timestamp in milliseconds.
4
+ *
5
+ * @param accessToken - JWT access token
6
+ * @returns Expiry timestamp in milliseconds, or a default 15-minute expiry if extraction fails
7
+ */
8
+ export declare function getTokenExpiry(accessToken: string | undefined): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imtbl/auth-nextjs",
3
- "version": "0.0.1-alpha.0",
3
+ "version": "2.12.4-alpha.4",
4
4
  "description": "Next.js authentication integration for Immutable SDK using NextAuth",
5
5
  "author": "Immutable",
6
6
  "bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
@@ -51,7 +51,7 @@
51
51
  "dist"
52
52
  ],
53
53
  "dependencies": {
54
- "@imtbl/auth": "2.12.4-alpha.3"
54
+ "@imtbl/auth": "2.12.4-alpha.4"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "next": "14.2.25",