@imtbl/auth-nextjs 2.12.4-alpha.5 → 2.12.4-alpha.6
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 +133 -120
- package/dist/node/{chunk-OPPMGNFZ.js → chunk-BRDI4KXS.js} +75 -14
- package/dist/node/client/index.cjs +9 -18
- package/dist/node/client/index.js +8 -17
- 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 +9 -4
- 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
package/dist/node/index.cjs
CHANGED
|
@@ -30,8 +30,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
+
DEFAULT_AUDIENCE: () => DEFAULT_AUDIENCE,
|
|
34
|
+
DEFAULT_AUTH_DOMAIN: () => DEFAULT_AUTH_DOMAIN,
|
|
35
|
+
DEFAULT_NEXTAUTH_BASE_PATH: () => DEFAULT_NEXTAUTH_BASE_PATH,
|
|
36
|
+
DEFAULT_SCOPE: () => DEFAULT_SCOPE,
|
|
33
37
|
ImmutableAuth: () => ImmutableAuth,
|
|
34
38
|
MarketingConsentStatus: () => import_auth.MarketingConsentStatus,
|
|
39
|
+
createAuthConfig: () => createAuthConfig,
|
|
40
|
+
createImmutableAuth: () => createImmutableAuth,
|
|
35
41
|
isTokenExpired: () => isTokenExpired,
|
|
36
42
|
refreshAccessToken: () => refreshAccessToken
|
|
37
43
|
});
|
|
@@ -43,7 +49,10 @@ var import_credentials = __toESM(require("next-auth/providers/credentials"), 1);
|
|
|
43
49
|
|
|
44
50
|
// src/constants.ts
|
|
45
51
|
var DEFAULT_AUTH_DOMAIN = "https://auth.immutable.com";
|
|
52
|
+
var DEFAULT_AUDIENCE = "platform_api";
|
|
53
|
+
var DEFAULT_SCOPE = "openid profile email offline_access transact";
|
|
46
54
|
var IMMUTABLE_PROVIDER_ID = "immutable";
|
|
55
|
+
var DEFAULT_NEXTAUTH_BASE_PATH = "/api/auth";
|
|
47
56
|
var DEFAULT_TOKEN_EXPIRY_SECONDS = 900;
|
|
48
57
|
var DEFAULT_TOKEN_EXPIRY_MS = DEFAULT_TOKEN_EXPIRY_SECONDS * 1e3;
|
|
49
58
|
var TOKEN_EXPIRY_BUFFER_SECONDS = 60;
|
|
@@ -111,7 +120,7 @@ function isTokenExpired(accessTokenExpires, bufferSeconds = TOKEN_EXPIRY_BUFFER_
|
|
|
111
120
|
}
|
|
112
121
|
|
|
113
122
|
// src/config.ts
|
|
114
|
-
var
|
|
123
|
+
var Credentials = import_credentials.default.default || import_credentials.default;
|
|
115
124
|
async function validateTokens(accessToken, authDomain) {
|
|
116
125
|
try {
|
|
117
126
|
const response = await fetch(`${authDomain}/userinfo`, {
|
|
@@ -130,18 +139,18 @@ async function validateTokens(accessToken, authDomain) {
|
|
|
130
139
|
return null;
|
|
131
140
|
}
|
|
132
141
|
}
|
|
133
|
-
function
|
|
142
|
+
function createAuthConfig(config) {
|
|
134
143
|
const authDomain = config.authenticationDomain || DEFAULT_AUTH_DOMAIN;
|
|
135
144
|
return {
|
|
136
145
|
providers: [
|
|
137
|
-
|
|
146
|
+
Credentials({
|
|
138
147
|
id: IMMUTABLE_PROVIDER_ID,
|
|
139
148
|
name: "Immutable",
|
|
140
149
|
credentials: {
|
|
141
150
|
tokens: { label: "Tokens", type: "text" }
|
|
142
151
|
},
|
|
143
152
|
async authorize(credentials) {
|
|
144
|
-
if (!credentials?.tokens) {
|
|
153
|
+
if (!credentials?.tokens || typeof credentials.tokens !== "string") {
|
|
145
154
|
return null;
|
|
146
155
|
}
|
|
147
156
|
let tokenData;
|
|
@@ -184,6 +193,7 @@ function createAuthOptions(config) {
|
|
|
184
193
|
})
|
|
185
194
|
],
|
|
186
195
|
callbacks: {
|
|
196
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
187
197
|
async jwt({
|
|
188
198
|
token,
|
|
189
199
|
user,
|
|
@@ -204,13 +214,14 @@ function createAuthOptions(config) {
|
|
|
204
214
|
};
|
|
205
215
|
}
|
|
206
216
|
if (trigger === "update" && sessionUpdate) {
|
|
217
|
+
const update = sessionUpdate;
|
|
207
218
|
return {
|
|
208
219
|
...token,
|
|
209
|
-
...
|
|
210
|
-
...
|
|
211
|
-
...
|
|
212
|
-
...
|
|
213
|
-
...
|
|
220
|
+
...update.accessToken ? { accessToken: update.accessToken } : {},
|
|
221
|
+
...update.refreshToken ? { refreshToken: update.refreshToken } : {},
|
|
222
|
+
...update.idToken ? { idToken: update.idToken } : {},
|
|
223
|
+
...update.accessTokenExpires ? { accessTokenExpires: update.accessTokenExpires } : {},
|
|
224
|
+
...update.zkEvm ? { zkEvm: update.zkEvm } : {}
|
|
214
225
|
};
|
|
215
226
|
}
|
|
216
227
|
if (!isTokenExpired(token.accessTokenExpires)) {
|
|
@@ -218,10 +229,12 @@ function createAuthOptions(config) {
|
|
|
218
229
|
}
|
|
219
230
|
return refreshAccessToken(token, config);
|
|
220
231
|
},
|
|
232
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
221
233
|
async session({ session, token }) {
|
|
222
234
|
return {
|
|
223
235
|
...session,
|
|
224
236
|
user: {
|
|
237
|
+
...session.user,
|
|
225
238
|
sub: token.sub,
|
|
226
239
|
email: token.email,
|
|
227
240
|
nickname: token.nickname
|
|
@@ -239,26 +252,24 @@ function createAuthOptions(config) {
|
|
|
239
252
|
strategy: "jwt",
|
|
240
253
|
// Session max age in seconds (30 days default)
|
|
241
254
|
maxAge: DEFAULT_SESSION_MAX_AGE_SECONDS
|
|
242
|
-
}
|
|
243
|
-
// Use NEXTAUTH_SECRET from environment
|
|
244
|
-
secret: process.env.NEXTAUTH_SECRET
|
|
255
|
+
}
|
|
245
256
|
};
|
|
246
257
|
}
|
|
247
258
|
|
|
248
259
|
// src/index.ts
|
|
249
260
|
var import_auth = require("@imtbl/auth");
|
|
250
261
|
var NextAuth = import_next_auth.default.default || import_next_auth.default;
|
|
251
|
-
function
|
|
252
|
-
const
|
|
262
|
+
function createImmutableAuth(config, overrides) {
|
|
263
|
+
const authConfig = createAuthConfig(config);
|
|
253
264
|
if (!overrides) {
|
|
254
|
-
return NextAuth(
|
|
265
|
+
return NextAuth(authConfig);
|
|
255
266
|
}
|
|
256
267
|
const composedCallbacks = {
|
|
257
|
-
...
|
|
268
|
+
...authConfig.callbacks
|
|
258
269
|
};
|
|
259
270
|
if (overrides.callbacks) {
|
|
260
271
|
if (overrides.callbacks.jwt) {
|
|
261
|
-
const internalJwt =
|
|
272
|
+
const internalJwt = authConfig.callbacks?.jwt;
|
|
262
273
|
const userJwt = overrides.callbacks.jwt;
|
|
263
274
|
composedCallbacks.jwt = async (params) => {
|
|
264
275
|
const token = internalJwt ? await internalJwt(params) : params.token;
|
|
@@ -266,7 +277,7 @@ function ImmutableAuth(config, overrides) {
|
|
|
266
277
|
};
|
|
267
278
|
}
|
|
268
279
|
if (overrides.callbacks.session) {
|
|
269
|
-
const internalSession =
|
|
280
|
+
const internalSession = authConfig.callbacks?.session;
|
|
270
281
|
const userSession = overrides.callbacks.session;
|
|
271
282
|
composedCallbacks.session = async (params) => {
|
|
272
283
|
const session = internalSession ? await internalSession(params) : params.session;
|
|
@@ -280,17 +291,24 @@ function ImmutableAuth(config, overrides) {
|
|
|
280
291
|
composedCallbacks.redirect = overrides.callbacks.redirect;
|
|
281
292
|
}
|
|
282
293
|
}
|
|
283
|
-
const
|
|
284
|
-
...
|
|
294
|
+
const mergedConfig = {
|
|
295
|
+
...authConfig,
|
|
285
296
|
...overrides,
|
|
286
297
|
callbacks: composedCallbacks
|
|
287
298
|
};
|
|
288
|
-
return NextAuth(
|
|
299
|
+
return NextAuth(mergedConfig);
|
|
289
300
|
}
|
|
301
|
+
var ImmutableAuth = createImmutableAuth;
|
|
290
302
|
// Annotate the CommonJS export names for ESM import in node:
|
|
291
303
|
0 && (module.exports = {
|
|
304
|
+
DEFAULT_AUDIENCE,
|
|
305
|
+
DEFAULT_AUTH_DOMAIN,
|
|
306
|
+
DEFAULT_NEXTAUTH_BASE_PATH,
|
|
307
|
+
DEFAULT_SCOPE,
|
|
292
308
|
ImmutableAuth,
|
|
293
309
|
MarketingConsentStatus,
|
|
310
|
+
createAuthConfig,
|
|
311
|
+
createImmutableAuth,
|
|
294
312
|
isTokenExpired,
|
|
295
313
|
refreshAccessToken
|
|
296
314
|
});
|
package/dist/node/index.js
CHANGED
|
@@ -1,55 +1,24 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
DEFAULT_AUDIENCE,
|
|
3
|
+
DEFAULT_AUTH_DOMAIN,
|
|
4
|
+
DEFAULT_NEXTAUTH_BASE_PATH,
|
|
5
|
+
DEFAULT_SCOPE,
|
|
6
|
+
ImmutableAuth,
|
|
7
|
+
MarketingConsentStatus,
|
|
8
|
+
createAuthConfig,
|
|
9
|
+
createImmutableAuth,
|
|
3
10
|
isTokenExpired,
|
|
4
11
|
refreshAccessToken
|
|
5
|
-
} from "./chunk-
|
|
6
|
-
|
|
7
|
-
// src/index.ts
|
|
8
|
-
import NextAuthDefault from "next-auth";
|
|
9
|
-
import { MarketingConsentStatus } from "@imtbl/auth";
|
|
10
|
-
var NextAuth = NextAuthDefault.default || NextAuthDefault;
|
|
11
|
-
function ImmutableAuth(config, overrides) {
|
|
12
|
-
const authOptions = createAuthOptions(config);
|
|
13
|
-
if (!overrides) {
|
|
14
|
-
return NextAuth(authOptions);
|
|
15
|
-
}
|
|
16
|
-
const composedCallbacks = {
|
|
17
|
-
...authOptions.callbacks
|
|
18
|
-
};
|
|
19
|
-
if (overrides.callbacks) {
|
|
20
|
-
if (overrides.callbacks.jwt) {
|
|
21
|
-
const internalJwt = authOptions.callbacks?.jwt;
|
|
22
|
-
const userJwt = overrides.callbacks.jwt;
|
|
23
|
-
composedCallbacks.jwt = async (params) => {
|
|
24
|
-
const token = internalJwt ? await internalJwt(params) : params.token;
|
|
25
|
-
return userJwt({ ...params, token });
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
if (overrides.callbacks.session) {
|
|
29
|
-
const internalSession = authOptions.callbacks?.session;
|
|
30
|
-
const userSession = overrides.callbacks.session;
|
|
31
|
-
composedCallbacks.session = async (params) => {
|
|
32
|
-
const session = internalSession ? await internalSession(params) : params.session;
|
|
33
|
-
return userSession({ ...params, session });
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
if (overrides.callbacks.signIn) {
|
|
37
|
-
composedCallbacks.signIn = overrides.callbacks.signIn;
|
|
38
|
-
}
|
|
39
|
-
if (overrides.callbacks.redirect) {
|
|
40
|
-
composedCallbacks.redirect = overrides.callbacks.redirect;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
const mergedOptions = {
|
|
44
|
-
...authOptions,
|
|
45
|
-
...overrides,
|
|
46
|
-
callbacks: composedCallbacks
|
|
47
|
-
};
|
|
48
|
-
return NextAuth(mergedOptions);
|
|
49
|
-
}
|
|
12
|
+
} from "./chunk-BRDI4KXS.js";
|
|
50
13
|
export {
|
|
14
|
+
DEFAULT_AUDIENCE,
|
|
15
|
+
DEFAULT_AUTH_DOMAIN,
|
|
16
|
+
DEFAULT_NEXTAUTH_BASE_PATH,
|
|
17
|
+
DEFAULT_SCOPE,
|
|
51
18
|
ImmutableAuth,
|
|
52
19
|
MarketingConsentStatus,
|
|
20
|
+
createAuthConfig,
|
|
21
|
+
createImmutableAuth,
|
|
53
22
|
isTokenExpired,
|
|
54
23
|
refreshAccessToken
|
|
55
24
|
};
|
|
@@ -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
|
};
|
|
@@ -19,18 +19,23 @@ export interface CallbackPageProps {
|
|
|
19
19
|
errorComponent?: (error: string) => React.ReactElement | null;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
|
-
* Callback page component for handling OAuth redirects.
|
|
22
|
+
* Callback page component for handling OAuth redirects (App Router version).
|
|
23
23
|
*
|
|
24
24
|
* Use this in your callback page to process authentication responses.
|
|
25
25
|
*
|
|
26
26
|
* @example
|
|
27
27
|
* ```tsx
|
|
28
|
-
* //
|
|
28
|
+
* // app/callback/page.tsx
|
|
29
|
+
* "use client";
|
|
29
30
|
* import { CallbackPage } from "@imtbl/auth-nextjs/client";
|
|
30
|
-
*
|
|
31
|
+
*
|
|
32
|
+
* const config = {
|
|
33
|
+
* clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
34
|
+
* redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
|
|
35
|
+
* };
|
|
31
36
|
*
|
|
32
37
|
* export default function Callback() {
|
|
33
|
-
* return <CallbackPage config={
|
|
38
|
+
* return <CallbackPage config={config} />;
|
|
34
39
|
* }
|
|
35
40
|
* ```
|
|
36
41
|
*/
|
|
@@ -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;
|