@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.
- package/README.md +199 -121
- package/dist/node/{chunk-OPPMGNFZ.js → chunk-BRDI4KXS.js} +75 -14
- package/dist/node/client/index.cjs +38 -21
- package/dist/node/client/index.js +37 -20
- package/dist/node/index.cjs +39 -21
- package/dist/node/index.js +15 -46
- package/dist/node/server/index.cjs +107 -59
- package/dist/node/server/index.js +48 -47
- package/dist/types/client/callback.d.ts +42 -8
- package/dist/types/client/provider.d.ts +20 -6
- package/dist/types/config.d.ts +9 -11
- package/dist/types/index.d.ts +28 -14
- package/dist/types/refresh.d.ts +1 -1
- package/dist/types/server/index.d.ts +104 -2
- package/dist/types/types.d.ts +19 -32
- package/package.json +5 -5
- package/dist/types/server/with-page-auth.d.ts +0 -94
|
@@ -30,13 +30,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/server/index.ts
|
|
31
31
|
var server_exports = {};
|
|
32
32
|
__export(server_exports, {
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
createAuthConfig: () => createAuthConfig,
|
|
34
|
+
createAuthMiddleware: () => createAuthMiddleware,
|
|
35
|
+
createImmutableAuth: () => createImmutableAuth,
|
|
36
|
+
withAuth: () => withAuth
|
|
35
37
|
});
|
|
36
38
|
module.exports = __toCommonJS(server_exports);
|
|
39
|
+
var import_server = require("next/server");
|
|
37
40
|
|
|
38
|
-
// src/
|
|
39
|
-
var import_next_auth = require("next-auth");
|
|
41
|
+
// src/index.ts
|
|
42
|
+
var import_next_auth = __toESM(require("next-auth"), 1);
|
|
40
43
|
|
|
41
44
|
// src/config.ts
|
|
42
45
|
var import_credentials = __toESM(require("next-auth/providers/credentials"), 1);
|
|
@@ -111,7 +114,7 @@ function isTokenExpired(accessTokenExpires, bufferSeconds = TOKEN_EXPIRY_BUFFER_
|
|
|
111
114
|
}
|
|
112
115
|
|
|
113
116
|
// src/config.ts
|
|
114
|
-
var
|
|
117
|
+
var Credentials = import_credentials.default.default || import_credentials.default;
|
|
115
118
|
async function validateTokens(accessToken, authDomain) {
|
|
116
119
|
try {
|
|
117
120
|
const response = await fetch(`${authDomain}/userinfo`, {
|
|
@@ -130,18 +133,18 @@ async function validateTokens(accessToken, authDomain) {
|
|
|
130
133
|
return null;
|
|
131
134
|
}
|
|
132
135
|
}
|
|
133
|
-
function
|
|
136
|
+
function createAuthConfig(config) {
|
|
134
137
|
const authDomain = config.authenticationDomain || DEFAULT_AUTH_DOMAIN;
|
|
135
138
|
return {
|
|
136
139
|
providers: [
|
|
137
|
-
|
|
140
|
+
Credentials({
|
|
138
141
|
id: IMMUTABLE_PROVIDER_ID,
|
|
139
142
|
name: "Immutable",
|
|
140
143
|
credentials: {
|
|
141
144
|
tokens: { label: "Tokens", type: "text" }
|
|
142
145
|
},
|
|
143
146
|
async authorize(credentials) {
|
|
144
|
-
if (!credentials?.tokens) {
|
|
147
|
+
if (!credentials?.tokens || typeof credentials.tokens !== "string") {
|
|
145
148
|
return null;
|
|
146
149
|
}
|
|
147
150
|
let tokenData;
|
|
@@ -184,6 +187,7 @@ function createAuthOptions(config) {
|
|
|
184
187
|
})
|
|
185
188
|
],
|
|
186
189
|
callbacks: {
|
|
190
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
187
191
|
async jwt({
|
|
188
192
|
token,
|
|
189
193
|
user,
|
|
@@ -204,13 +208,14 @@ function createAuthOptions(config) {
|
|
|
204
208
|
};
|
|
205
209
|
}
|
|
206
210
|
if (trigger === "update" && sessionUpdate) {
|
|
211
|
+
const update = sessionUpdate;
|
|
207
212
|
return {
|
|
208
213
|
...token,
|
|
209
|
-
...
|
|
210
|
-
...
|
|
211
|
-
...
|
|
212
|
-
...
|
|
213
|
-
...
|
|
214
|
+
...update.accessToken ? { accessToken: update.accessToken } : {},
|
|
215
|
+
...update.refreshToken ? { refreshToken: update.refreshToken } : {},
|
|
216
|
+
...update.idToken ? { idToken: update.idToken } : {},
|
|
217
|
+
...update.accessTokenExpires ? { accessTokenExpires: update.accessTokenExpires } : {},
|
|
218
|
+
...update.zkEvm ? { zkEvm: update.zkEvm } : {}
|
|
214
219
|
};
|
|
215
220
|
}
|
|
216
221
|
if (!isTokenExpired(token.accessTokenExpires)) {
|
|
@@ -218,10 +223,12 @@ function createAuthOptions(config) {
|
|
|
218
223
|
}
|
|
219
224
|
return refreshAccessToken(token, config);
|
|
220
225
|
},
|
|
226
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
221
227
|
async session({ session, token }) {
|
|
222
228
|
return {
|
|
223
229
|
...session,
|
|
224
230
|
user: {
|
|
231
|
+
...session.user,
|
|
225
232
|
sub: token.sub,
|
|
226
233
|
email: token.email,
|
|
227
234
|
nickname: token.nickname
|
|
@@ -239,62 +246,103 @@ function createAuthOptions(config) {
|
|
|
239
246
|
strategy: "jwt",
|
|
240
247
|
// Session max age in seconds (30 days default)
|
|
241
248
|
maxAge: DEFAULT_SESSION_MAX_AGE_SECONDS
|
|
242
|
-
}
|
|
243
|
-
// Use NEXTAUTH_SECRET from environment
|
|
244
|
-
secret: process.env.NEXTAUTH_SECRET
|
|
249
|
+
}
|
|
245
250
|
};
|
|
246
251
|
}
|
|
247
252
|
|
|
248
|
-
// src/
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const separator = loginUrl.includes("?") ? "&" : "?";
|
|
267
|
-
destination = `${loginUrl}${separator}returnTo=${encodeURIComponent(returnPath)}`;
|
|
268
|
-
}
|
|
269
|
-
return {
|
|
270
|
-
redirect: {
|
|
271
|
-
destination,
|
|
272
|
-
permanent: false
|
|
273
|
-
}
|
|
253
|
+
// src/index.ts
|
|
254
|
+
var import_auth = require("@imtbl/auth");
|
|
255
|
+
var NextAuth = import_next_auth.default.default || import_next_auth.default;
|
|
256
|
+
function createImmutableAuth(config, overrides) {
|
|
257
|
+
const authConfig = createAuthConfig(config);
|
|
258
|
+
if (!overrides) {
|
|
259
|
+
return NextAuth(authConfig);
|
|
260
|
+
}
|
|
261
|
+
const composedCallbacks = {
|
|
262
|
+
...authConfig.callbacks
|
|
263
|
+
};
|
|
264
|
+
if (overrides.callbacks) {
|
|
265
|
+
if (overrides.callbacks.jwt) {
|
|
266
|
+
const internalJwt = authConfig.callbacks?.jwt;
|
|
267
|
+
const userJwt = overrides.callbacks.jwt;
|
|
268
|
+
composedCallbacks.jwt = async (params) => {
|
|
269
|
+
const token = internalJwt ? await internalJwt(params) : params.token;
|
|
270
|
+
return userJwt({ ...params, token });
|
|
274
271
|
};
|
|
275
272
|
}
|
|
276
|
-
if (
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return {
|
|
283
|
-
props: {
|
|
284
|
-
...userProps,
|
|
285
|
-
session
|
|
286
|
-
}
|
|
273
|
+
if (overrides.callbacks.session) {
|
|
274
|
+
const internalSession = authConfig.callbacks?.session;
|
|
275
|
+
const userSession = overrides.callbacks.session;
|
|
276
|
+
composedCallbacks.session = async (params) => {
|
|
277
|
+
const session = internalSession ? await internalSession(params) : params.session;
|
|
278
|
+
return userSession({ ...params, session });
|
|
287
279
|
};
|
|
288
280
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
281
|
+
if (overrides.callbacks.signIn) {
|
|
282
|
+
composedCallbacks.signIn = overrides.callbacks.signIn;
|
|
283
|
+
}
|
|
284
|
+
if (overrides.callbacks.redirect) {
|
|
285
|
+
composedCallbacks.redirect = overrides.callbacks.redirect;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const mergedConfig = {
|
|
289
|
+
...authConfig,
|
|
290
|
+
...overrides,
|
|
291
|
+
callbacks: composedCallbacks
|
|
292
|
+
};
|
|
293
|
+
return NextAuth(mergedConfig);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// src/server/index.ts
|
|
297
|
+
function createAuthMiddleware(auth, options = {}) {
|
|
298
|
+
const { loginUrl = "/login", protectedPaths, publicPaths } = options;
|
|
299
|
+
return async function middleware(request) {
|
|
300
|
+
const { pathname } = request.nextUrl;
|
|
301
|
+
if (publicPaths) {
|
|
302
|
+
const isPublic = publicPaths.some((pattern) => {
|
|
303
|
+
if (typeof pattern === "string") {
|
|
304
|
+
return pathname === pattern || pathname.startsWith(pattern);
|
|
305
|
+
}
|
|
306
|
+
return pattern.test(pathname);
|
|
307
|
+
});
|
|
308
|
+
if (isPublic) {
|
|
309
|
+
return import_server.NextResponse.next();
|
|
292
310
|
}
|
|
293
|
-
}
|
|
311
|
+
}
|
|
312
|
+
if (protectedPaths) {
|
|
313
|
+
const isProtected = protectedPaths.some((pattern) => {
|
|
314
|
+
if (typeof pattern === "string") {
|
|
315
|
+
return pathname === pattern || pathname.startsWith(pattern);
|
|
316
|
+
}
|
|
317
|
+
return pattern.test(pathname);
|
|
318
|
+
});
|
|
319
|
+
if (!isProtected) {
|
|
320
|
+
return import_server.NextResponse.next();
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const session = await auth();
|
|
324
|
+
if (!session) {
|
|
325
|
+
const url = new URL(loginUrl, request.url);
|
|
326
|
+
const returnTo = request.nextUrl.search ? `${pathname}${request.nextUrl.search}` : pathname;
|
|
327
|
+
url.searchParams.set("returnTo", returnTo);
|
|
328
|
+
return import_server.NextResponse.redirect(url);
|
|
329
|
+
}
|
|
330
|
+
return import_server.NextResponse.next();
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function withAuth(auth, handler) {
|
|
334
|
+
return async (...args) => {
|
|
335
|
+
const session = await auth();
|
|
336
|
+
if (!session) {
|
|
337
|
+
throw new Error("Unauthorized: No active session");
|
|
338
|
+
}
|
|
339
|
+
return handler(session, ...args);
|
|
294
340
|
};
|
|
295
341
|
}
|
|
296
342
|
// Annotate the CommonJS export names for ESM import in node:
|
|
297
343
|
0 && (module.exports = {
|
|
298
|
-
|
|
299
|
-
|
|
344
|
+
createAuthConfig,
|
|
345
|
+
createAuthMiddleware,
|
|
346
|
+
createImmutableAuth,
|
|
347
|
+
withAuth
|
|
300
348
|
});
|
|
@@ -1,57 +1,58 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
createAuthConfig,
|
|
3
|
+
createImmutableAuth
|
|
4
|
+
} from "../chunk-BRDI4KXS.js";
|
|
4
5
|
|
|
5
|
-
// src/server/
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
return
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
getServerSideProps: customGetServerSideProps
|
|
16
|
-
} = options;
|
|
17
|
-
const authOptions = createAuthOptions(config);
|
|
18
|
-
return async (ctx) => {
|
|
19
|
-
const session = await nextAuthGetServerSession(ctx.req, ctx.res, authOptions);
|
|
20
|
-
if (!session) {
|
|
21
|
-
let destination = loginUrl;
|
|
22
|
-
if (returnTo !== false) {
|
|
23
|
-
const returnPath = returnTo || ctx.resolvedUrl;
|
|
24
|
-
const separator = loginUrl.includes("?") ? "&" : "?";
|
|
25
|
-
destination = `${loginUrl}${separator}returnTo=${encodeURIComponent(returnPath)}`;
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
redirect: {
|
|
29
|
-
destination,
|
|
30
|
-
permanent: false
|
|
6
|
+
// src/server/index.ts
|
|
7
|
+
import { NextResponse } from "next/server";
|
|
8
|
+
function createAuthMiddleware(auth, options = {}) {
|
|
9
|
+
const { loginUrl = "/login", protectedPaths, publicPaths } = options;
|
|
10
|
+
return async function middleware(request) {
|
|
11
|
+
const { pathname } = request.nextUrl;
|
|
12
|
+
if (publicPaths) {
|
|
13
|
+
const isPublic = publicPaths.some((pattern) => {
|
|
14
|
+
if (typeof pattern === "string") {
|
|
15
|
+
return pathname === pattern || pathname.startsWith(pattern);
|
|
31
16
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if ("redirect" in result || "notFound" in result) {
|
|
37
|
-
return result;
|
|
17
|
+
return pattern.test(pathname);
|
|
18
|
+
});
|
|
19
|
+
if (isPublic) {
|
|
20
|
+
return NextResponse.next();
|
|
38
21
|
}
|
|
39
|
-
const userProps = await result.props;
|
|
40
|
-
return {
|
|
41
|
-
props: {
|
|
42
|
-
...userProps,
|
|
43
|
-
session
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
22
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
23
|
+
if (protectedPaths) {
|
|
24
|
+
const isProtected = protectedPaths.some((pattern) => {
|
|
25
|
+
if (typeof pattern === "string") {
|
|
26
|
+
return pathname === pattern || pathname.startsWith(pattern);
|
|
27
|
+
}
|
|
28
|
+
return pattern.test(pathname);
|
|
29
|
+
});
|
|
30
|
+
if (!isProtected) {
|
|
31
|
+
return NextResponse.next();
|
|
50
32
|
}
|
|
51
|
-
}
|
|
33
|
+
}
|
|
34
|
+
const session = await auth();
|
|
35
|
+
if (!session) {
|
|
36
|
+
const url = new URL(loginUrl, request.url);
|
|
37
|
+
const returnTo = request.nextUrl.search ? `${pathname}${request.nextUrl.search}` : pathname;
|
|
38
|
+
url.searchParams.set("returnTo", returnTo);
|
|
39
|
+
return NextResponse.redirect(url);
|
|
40
|
+
}
|
|
41
|
+
return NextResponse.next();
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function withAuth(auth, handler) {
|
|
45
|
+
return async (...args) => {
|
|
46
|
+
const session = await auth();
|
|
47
|
+
if (!session) {
|
|
48
|
+
throw new Error("Unauthorized: No active session");
|
|
49
|
+
}
|
|
50
|
+
return handler(session, ...args);
|
|
52
51
|
};
|
|
53
52
|
}
|
|
54
53
|
export {
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
createAuthConfig,
|
|
55
|
+
createAuthMiddleware,
|
|
56
|
+
createImmutableAuth,
|
|
57
|
+
withAuth
|
|
57
58
|
};
|
|
@@ -1,14 +1,30 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import type { ImmutableAuthConfig } from '../types';
|
|
2
|
+
import type { ImmutableAuthConfig, ImmutableUser } from '../types';
|
|
3
3
|
export interface CallbackPageProps {
|
|
4
4
|
/**
|
|
5
5
|
* Immutable auth configuration
|
|
6
6
|
*/
|
|
7
7
|
config: ImmutableAuthConfig;
|
|
8
8
|
/**
|
|
9
|
-
* URL to redirect to after successful authentication (when not in popup)
|
|
9
|
+
* URL to redirect to after successful authentication (when not in popup).
|
|
10
|
+
* Can be a string or a function that receives the authenticated user.
|
|
11
|
+
* If a function returns void/undefined, defaults to "/".
|
|
12
|
+
* @default "/"
|
|
13
|
+
*
|
|
14
|
+
* @example Static redirect
|
|
15
|
+
* ```tsx
|
|
16
|
+
* <CallbackPage config={config} redirectTo="/dashboard" />
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example Dynamic redirect based on user
|
|
20
|
+
* ```tsx
|
|
21
|
+
* <CallbackPage
|
|
22
|
+
* config={config}
|
|
23
|
+
* redirectTo={(user) => user.email?.endsWith('@admin.com') ? '/admin' : '/dashboard'}
|
|
24
|
+
* />
|
|
25
|
+
* ```
|
|
10
26
|
*/
|
|
11
|
-
redirectTo?: string;
|
|
27
|
+
redirectTo?: string | ((user: ImmutableUser) => string | void);
|
|
12
28
|
/**
|
|
13
29
|
* Custom loading component
|
|
14
30
|
*/
|
|
@@ -17,21 +33,39 @@ export interface CallbackPageProps {
|
|
|
17
33
|
* Custom error component
|
|
18
34
|
*/
|
|
19
35
|
errorComponent?: (error: string) => React.ReactElement | null;
|
|
36
|
+
/**
|
|
37
|
+
* Callback fired after successful authentication.
|
|
38
|
+
* Receives the authenticated user as a parameter.
|
|
39
|
+
* Called before redirect (non-popup) or before window.close (popup).
|
|
40
|
+
* If this callback returns a Promise, it will be awaited before proceeding.
|
|
41
|
+
*/
|
|
42
|
+
onSuccess?: (user: ImmutableUser) => void | Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Callback fired when authentication fails.
|
|
45
|
+
* Receives the error message as a parameter.
|
|
46
|
+
* Called before the error UI is displayed.
|
|
47
|
+
*/
|
|
48
|
+
onError?: (error: string) => void;
|
|
20
49
|
}
|
|
21
50
|
/**
|
|
22
|
-
* Callback page component for handling OAuth redirects.
|
|
51
|
+
* Callback page component for handling OAuth redirects (App Router version).
|
|
23
52
|
*
|
|
24
53
|
* Use this in your callback page to process authentication responses.
|
|
25
54
|
*
|
|
26
55
|
* @example
|
|
27
56
|
* ```tsx
|
|
28
|
-
* //
|
|
57
|
+
* // app/callback/page.tsx
|
|
58
|
+
* "use client";
|
|
29
59
|
* import { CallbackPage } from "@imtbl/auth-nextjs/client";
|
|
30
|
-
*
|
|
60
|
+
*
|
|
61
|
+
* const config = {
|
|
62
|
+
* clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
63
|
+
* redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
|
|
64
|
+
* };
|
|
31
65
|
*
|
|
32
66
|
* export default function Callback() {
|
|
33
|
-
* return <CallbackPage config={
|
|
67
|
+
* return <CallbackPage config={config} />;
|
|
34
68
|
* }
|
|
35
69
|
* ```
|
|
36
70
|
*/
|
|
37
|
-
export declare function CallbackPage({ config, redirectTo, loadingComponent, errorComponent, }: CallbackPageProps): import("react/jsx-runtime").JSX.Element | null;
|
|
71
|
+
export declare function CallbackPage({ config, redirectTo, loadingComponent, errorComponent, onSuccess, onError, }: CallbackPageProps): import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { ImmutableAuthProviderProps, UseImmutableAuthReturn } from '../types';
|
|
2
2
|
/**
|
|
3
|
-
* Provider component for Immutable authentication with
|
|
3
|
+
* Provider component for Immutable authentication with Auth.js v5
|
|
4
4
|
*
|
|
5
5
|
* Wraps your app to provide authentication state via useImmutableAuth hook.
|
|
6
6
|
*
|
|
7
|
-
* @example
|
|
7
|
+
* @example App Router (recommended)
|
|
8
8
|
* ```tsx
|
|
9
|
-
* //
|
|
9
|
+
* // app/providers.tsx
|
|
10
|
+
* "use client";
|
|
10
11
|
* import { ImmutableAuthProvider } from "@imtbl/auth-nextjs/client";
|
|
11
12
|
*
|
|
12
13
|
* const config = {
|
|
@@ -14,13 +15,26 @@ import type { ImmutableAuthProviderProps, UseImmutableAuthReturn } from '../type
|
|
|
14
15
|
* redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
|
|
15
16
|
* };
|
|
16
17
|
*
|
|
17
|
-
* export
|
|
18
|
+
* export function Providers({ children }: { children: React.ReactNode }) {
|
|
18
19
|
* return (
|
|
19
|
-
* <ImmutableAuthProvider config={config}
|
|
20
|
-
*
|
|
20
|
+
* <ImmutableAuthProvider config={config}>
|
|
21
|
+
* {children}
|
|
21
22
|
* </ImmutableAuthProvider>
|
|
22
23
|
* );
|
|
23
24
|
* }
|
|
25
|
+
*
|
|
26
|
+
* // app/layout.tsx
|
|
27
|
+
* import { Providers } from "./providers";
|
|
28
|
+
*
|
|
29
|
+
* export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
30
|
+
* return (
|
|
31
|
+
* <html>
|
|
32
|
+
* <body>
|
|
33
|
+
* <Providers>{children}</Providers>
|
|
34
|
+
* </body>
|
|
35
|
+
* </html>
|
|
36
|
+
* );
|
|
37
|
+
* }
|
|
24
38
|
* ```
|
|
25
39
|
*/
|
|
26
40
|
export declare function ImmutableAuthProvider({ children, config, session, basePath, }: ImmutableAuthProviderProps): import("react/jsx-runtime").JSX.Element;
|
package/dist/types/config.d.ts
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NextAuthConfig } from 'next-auth';
|
|
2
2
|
import type { ImmutableAuthConfig } from './types';
|
|
3
3
|
/**
|
|
4
|
-
* Create
|
|
4
|
+
* Create Auth.js v5 configuration for Immutable authentication
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```typescript
|
|
8
8
|
* // lib/auth.ts
|
|
9
|
-
* import
|
|
9
|
+
* import NextAuth from "next-auth";
|
|
10
|
+
* import { createAuthConfig } from "@imtbl/auth-nextjs";
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
+
* const config = {
|
|
12
13
|
* clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
13
14
|
* redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
|
|
14
|
-
* }
|
|
15
|
-
*
|
|
16
|
-
* // pages/api/auth/[...nextauth].ts
|
|
17
|
-
* import NextAuth from "next-auth";
|
|
18
|
-
* import { authOptions } from "@/lib/auth";
|
|
15
|
+
* };
|
|
19
16
|
*
|
|
20
|
-
* export
|
|
17
|
+
* export const { handlers, auth, signIn, signOut } = NextAuth(createAuthConfig(config));
|
|
21
18
|
* ```
|
|
22
19
|
*/
|
|
23
|
-
export declare function
|
|
20
|
+
export declare function createAuthConfig(config: ImmutableAuthConfig): NextAuthConfig;
|
|
21
|
+
export declare const createAuthOptions: typeof createAuthConfig;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import NextAuthImport from 'next-auth';
|
|
2
|
+
import type { NextAuthConfig } from 'next-auth';
|
|
2
3
|
import type { ImmutableAuthConfig } from './types';
|
|
4
|
+
declare const NextAuth: typeof NextAuthImport;
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
6
|
+
* Auth.js v5 config options that can be overridden.
|
|
5
7
|
* Excludes 'providers' as that's managed internally.
|
|
6
8
|
*/
|
|
7
|
-
export type ImmutableAuthOverrides = Omit<
|
|
9
|
+
export type ImmutableAuthOverrides = Omit<NextAuthConfig, 'providers'>;
|
|
8
10
|
/**
|
|
9
|
-
*
|
|
11
|
+
* Return type of createImmutableAuth - the NextAuth instance with handlers
|
|
12
|
+
*/
|
|
13
|
+
export type ImmutableAuthResult = ReturnType<typeof NextAuth>;
|
|
14
|
+
/**
|
|
15
|
+
* Create an Auth.js v5 instance with Immutable authentication
|
|
10
16
|
*
|
|
11
17
|
* @param config - Immutable auth configuration
|
|
12
|
-
* @param overrides - Optional
|
|
18
|
+
* @param overrides - Optional Auth.js options to override defaults
|
|
19
|
+
* @returns NextAuth instance with { handlers, auth, signIn, signOut }
|
|
13
20
|
*
|
|
14
21
|
* @remarks
|
|
15
22
|
* Callback composition: The `jwt` and `session` callbacks are composed rather than
|
|
@@ -17,20 +24,24 @@ export type ImmutableAuthOverrides = Omit<NextAuthOptions, 'providers'>;
|
|
|
17
24
|
* your custom callbacks receive the result. Other callbacks (`signIn`, `redirect`)
|
|
18
25
|
* are replaced entirely if provided.
|
|
19
26
|
*
|
|
20
|
-
* @example Basic usage
|
|
27
|
+
* @example Basic usage (App Router)
|
|
21
28
|
* ```typescript
|
|
22
|
-
* //
|
|
23
|
-
* import {
|
|
29
|
+
* // lib/auth.ts
|
|
30
|
+
* import { createImmutableAuth } from "@imtbl/auth-nextjs";
|
|
24
31
|
*
|
|
25
|
-
* export
|
|
32
|
+
* export const { handlers, auth, signIn, signOut } = createImmutableAuth({
|
|
26
33
|
* clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
27
34
|
* redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
|
|
28
35
|
* });
|
|
36
|
+
*
|
|
37
|
+
* // app/api/auth/[...nextauth]/route.ts
|
|
38
|
+
* import { handlers } from "@/lib/auth";
|
|
39
|
+
* export const { GET, POST } = handlers;
|
|
29
40
|
* ```
|
|
30
41
|
*
|
|
31
|
-
* @example With
|
|
42
|
+
* @example With Auth.js overrides
|
|
32
43
|
* ```typescript
|
|
33
|
-
* export
|
|
44
|
+
* export const { handlers, auth } = createImmutableAuth(
|
|
34
45
|
* { clientId: "...", redirectUri: "..." },
|
|
35
46
|
* {
|
|
36
47
|
* pages: { signIn: "/custom-login", error: "/auth-error" },
|
|
@@ -41,7 +52,7 @@ export type ImmutableAuthOverrides = Omit<NextAuthOptions, 'providers'>;
|
|
|
41
52
|
*
|
|
42
53
|
* @example With custom jwt callback (composed with internal callback)
|
|
43
54
|
* ```typescript
|
|
44
|
-
* export
|
|
55
|
+
* export const { handlers, auth } = createImmutableAuth(
|
|
45
56
|
* { clientId: "...", redirectUri: "..." },
|
|
46
57
|
* {
|
|
47
58
|
* callbacks: {
|
|
@@ -56,8 +67,11 @@ export type ImmutableAuthOverrides = Omit<NextAuthOptions, 'providers'>;
|
|
|
56
67
|
* );
|
|
57
68
|
* ```
|
|
58
69
|
*/
|
|
59
|
-
export declare function
|
|
60
|
-
export
|
|
70
|
+
export declare function createImmutableAuth(config: ImmutableAuthConfig, overrides?: ImmutableAuthOverrides): ImmutableAuthResult;
|
|
71
|
+
export declare const ImmutableAuth: typeof createImmutableAuth;
|
|
72
|
+
export { createAuthConfig } from './config';
|
|
73
|
+
export type { ImmutableAuthConfig, ImmutableTokenData, ImmutableUser, ZkEvmInfo, } from './types';
|
|
61
74
|
export type { LoginOptions, DirectLoginOptions } from '@imtbl/auth';
|
|
62
75
|
export { MarketingConsentStatus } from '@imtbl/auth';
|
|
63
76
|
export { refreshAccessToken, isTokenExpired } from './refresh';
|
|
77
|
+
export { DEFAULT_AUTH_DOMAIN, DEFAULT_AUDIENCE, DEFAULT_SCOPE, DEFAULT_NEXTAUTH_BASE_PATH, } from './constants';
|
package/dist/types/refresh.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { JWT } from 'next-auth/jwt';
|
|
|
2
2
|
import type { ImmutableAuthConfig } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* Refresh the access token using the refresh token
|
|
5
|
-
* Called by
|
|
5
|
+
* Called by Auth.js JWT callback when token is expired
|
|
6
6
|
*/
|
|
7
7
|
export declare function refreshAccessToken(token: JWT, config: ImmutableAuthConfig): Promise<JWT>;
|
|
8
8
|
/**
|