@monocloud/auth-nextjs 0.1.6 → 0.1.8

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,409 +0,0 @@
1
- import { Authenticators, AuthorizationParams, DisplayOptions, MonoCloudUser, Prompt } from "@monocloud/auth-node-core";
2
- import { JSX } from "react";
3
- import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
4
- import { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult, NextApiRequest, NextApiResponse } from "next/types";
5
- import { ParsedUrlQuery } from "node:querystring";
6
-
7
- //#region src/types.d.ts
8
- /**
9
- * Context object passed to App Router API route handlers.
10
- * Contains the dynamic route parameters.
11
- */
12
- interface AppRouterContext {
13
- params: Record<string, string | string[]>;
14
- }
15
- /**
16
- * Return type of `monoCloudAuth()` handler.
17
- */
18
- type MonoCloudAuthHandler = (req: Request | NextRequest | NextApiRequest, resOrCtx?: Response | NextResponse<any> | NextApiResponse<any> | AppRouterContext) => Promise<Response | NextResponse | void | any>;
19
- /** Return type of Next.js middleware/proxy */
20
- type NextMiddlewareResult = NextResponse | Response | null | undefined | void;
21
- /**
22
- * Handler function triggered when a user is denied access in Next.js Middleware.
23
- *
24
- * @param request - The incoming Next.js request.
25
- * @param event - The Next.js fetch event.
26
- * @returns A `NextMiddlewareResult` (e.g., a redirect or rewrite) or a Promise resolving to one.
27
- */
28
- type NextMiddlewareOnAccessDenied = (request: NextRequest, event: NextFetchEvent) => NextMiddlewareResult | Promise<NextMiddlewareResult>;
29
- /**
30
- * Handler function triggered when a user is denied access in Next.js Middleware due to group restrictions.
31
- *
32
- * @param request - The incoming Next.js request.
33
- * @param event - The Next.js fetch event.
34
- * @param user - The authenticated user object.
35
- * @returns A `NextMiddlewareResult` (e.g., a redirect or rewrite) or a Promise resolving to one.
36
- */
37
- type NextMiddlewareOnGroupAccessDenied = (request: NextRequest, event: NextFetchEvent, user: MonoCloudUser) => NextMiddlewareResult | Promise<NextMiddlewareResult>;
38
- /**
39
- *
40
- * ProtectedRouteMatcher can be a combination of the following types.
41
- *
42
- * - `string` : Relative route that should be protected.
43
- * - `RegExp` : A regular expression used to match relative routes that should be protected.
44
- * - `{ routes: string[]; groups: string[] }` : Users belonging to any of the group names or IDs listed in groups are granted access to the route paths specified in routes.
45
- */
46
- type ProtectedRouteMatcher = string | RegExp | {
47
- /**
48
- * Routes accessible by the users of specified groups
49
- */
50
- routes: (string | RegExp)[];
51
- /**
52
- * A list of group IDs or names specifying the groups the user must belong for accessing the routes.
53
- */
54
- groups: string[];
55
- };
56
- /**
57
- * A function to be executed that determines whether the route is protected.
58
- */
59
- type CustomProtectedRouteMatcher = (req: NextRequest) => Promise<boolean> | boolean;
60
- /**
61
- * @param req - The Next.js request object.
62
- * @param ctx - App Router context that contains dynamic route values.
63
- * @param error - Error occured during execution of the endpoint.
64
- * @returns A promise of the response
65
- */
66
- type AppOnError<T = any> = (req: NextRequest, ctx: T, error: Error) => Promise<NextResponse | void> | NextResponse | void;
67
- /**
68
- * @param req - The Next.js API request object.
69
- * @param res - The Next.js API response object.
70
- * @param error - Error occured during execution of the endpoint.
71
- * @returns A promise of void
72
- */
73
- type PageOnError = (req: NextApiRequest, res: NextApiResponse, error: Error) => Promise<void> | void;
74
- /**
75
- * A route handler function used to handle errors that occur during the signin, callback, signout and userinfo endpoint execution.
76
- *
77
- * `Note` - In the app router error handler, failing to return a `NextResponse` or throw an error will cause the request to hang. Same happens in the page router if you don't call `res.send()` or `res.json()` after you handle the error.
78
- */
79
- type OnError = AppOnError | PageOnError;
80
- /**
81
- * Options for `monoCloudAuth()`.
82
- */
83
- interface MonoCloudAuthOptions {
84
- /**
85
- * Error handler for signin, callback, signout and userinfo endpoints.
86
- */
87
- onError?: OnError;
88
- }
89
- /**
90
- * Configuration for protected routes.
91
- */
92
- type ProtectedRoutes = ProtectedRouteMatcher[] | CustomProtectedRouteMatcher;
93
- /**
94
- * Options for configuring MonoCloud authentication middleware.
95
- */
96
- interface MonoCloudMiddlewareOptions {
97
- /**
98
- * Error handler for signin, callback, signout and userinfo endpoints.
99
- *
100
- * @param req - The Next.js request object.
101
- * @param evt - The Next.js FetchEvent.
102
- * @param error - Error occured during execution of the endpoint.
103
- * @returns A promise of the response or void
104
- */
105
- onError?: (req: NextRequest, evt: NextFetchEvent, error: Error) => Promise<NextResponse | void> | NextResponse | void;
106
- /**
107
- * Specifies the routes that require authentication. @see ProtectedRoutes
108
- *
109
- * If an empty array is passed as the value for the protected routes configuration, no routes will be protected.
110
- */
111
- protectedRoutes?: ProtectedRoutes;
112
- /**
113
- * The name of the groups claim in the user profile. Default: `groups`.
114
- */
115
- groupsClaim?: string;
116
- /**
117
- * If true, user must be a member of all groups. Default: false.
118
- */
119
- matchAll?: boolean;
120
- /**
121
- * A middleware function called when the user is not authenticated.
122
- */
123
- onAccessDenied?: NextMiddlewareOnAccessDenied;
124
- /**
125
- * A middleware function called when the user is authenticated but is not a member of the specified groups.
126
- */
127
- onGroupAccessDenied?: NextMiddlewareOnGroupAccessDenied;
128
- }
129
- /**
130
- * A subset of authorization parameters used on client side functions
131
- */
132
- type ExtraAuthParams = Pick<AuthorizationParams, 'scopes' | 'resource' | 'prompt' | 'display' | 'uiLocales' | 'acrValues' | 'authenticatorHint' | 'maxAge' | 'loginHint'>;
133
- /**
134
- * Represents a Next.js App Router React Server Component.
135
- *
136
- * @param props - The props object containing `params` and `searchParams`.
137
- *
138
- * @returns A JSX Element or a Promise resolving to one.
139
- */
140
- type AppRouterPageHandler = (props: {
141
- params?: Record<string, string | string[]>;
142
- searchParams?: Record<string, string | string[] | undefined>;
143
- }) => Promise<JSX.Element> | JSX.Element;
144
- /** App Router API Route Handler */
145
- type AppRouterApiHandlerFn = (req: NextRequest | Request, ctx: AppRouterContext) => Promise<Response | NextResponse> | Response | NextResponse;
146
- /**
147
- * Options for configuring `protectPage()` in the App Router.
148
- */
149
- type ProtectAppPageOptions = {
150
- /**
151
- * The URL to return to after authentication.
152
- */
153
- returnUrl?: string;
154
- /**
155
- * Alternate page handler called when the user is not authenticated.
156
- */
157
- onAccessDenied?: (props: {
158
- params?: Record<string, string | string[]>;
159
- searchParams?: Record<string, string | string[] | undefined>;
160
- }) => Promise<JSX.Element> | JSX.Element;
161
- /**
162
- * Alternate page handler called when the user is authenticated but is not a member of the specified groups.
163
- */
164
- onGroupAccessDenied?: (props: {
165
- user: MonoCloudUser;
166
- params?: Record<string, string | string[]>;
167
- searchParams?: Record<string, string | string[] | undefined>;
168
- }) => Promise<JSX.Element> | JSX.Element;
169
- /**
170
- * Authorization parameters to be used during authentication.
171
- */
172
- authParams?: ExtraAuthParams;
173
- } & GroupOptions;
174
- /**
175
- * Options for configuring `protectPage()` in the Pages Router.
176
- *
177
- * @typeParam P - The type of the props returned by `getServerSideProps`.
178
- * @typeParam Q - The type of the parsed query object.
179
- */
180
- type ProtectPagePageOptions<P extends Record<string, any> = Record<string, any>, Q extends ParsedUrlQuery = ParsedUrlQuery> = {
181
- /**
182
- * Function to fetch server-side props for the protected page handler.
183
- * If provided, this function will be called before rendering the protected page.
184
- *
185
- * @param context - The Next.js context object, including the request and response objects.
186
- * @returns Server-side props for the protected page.
187
- */
188
- getServerSideProps?: GetServerSideProps<P, Q>;
189
- /**
190
- * Specifies the URL to redirect to after authentication.
191
- */
192
- returnUrl?: string;
193
- /**
194
- * Alternate `getServerSideProps` function called when the user is not authenticated.
195
- */
196
- onAccessDenied?: ProtectPagePageOnAccessDeniedType<P, Q>;
197
- /**
198
- * Alternate `getServerSideProps` function called when the user IS authenticated but is not a member of the specified groups.
199
- */
200
- onGroupAccessDenied?: ProtectPagePageOnGroupAccessDeniedType<P, Q>;
201
- /**
202
- * Authorization parameters to be used during authentication.
203
- */
204
- authParams?: ExtraAuthParams;
205
- } & GroupOptions;
206
- /**
207
- * Handler function triggered when a user is not authenticated in a Pages Router `getServerSideProps` flow.
208
- *
209
- * @typeParam P - The type of the props.
210
- * @typeParam Q - The type of the parsed query object.
211
- *
212
- * @param context - The server-side props context
213
- *
214
- * @returns The result for `getServerSideProps`.
215
- */
216
- type ProtectPagePageOnAccessDeniedType<P, Q extends ParsedUrlQuery = ParsedUrlQuery> = (context: GetServerSidePropsContext<Q>) => Promise<GetServerSidePropsResult<P>> | GetServerSidePropsResult<P>;
217
- /**
218
- * Handler function triggered when a user is denied access in a Pages Router `getServerSideProps` flow due to group restrictions.
219
- *
220
- * @typeParam P - The type of the props.
221
- * @typeParam Q - The type of the parsed query object.
222
- *
223
- * @param context - The server-side props context with the user object.
224
- *
225
- * @returns The result for `getServerSideProps`.
226
- */
227
- type ProtectPagePageOnGroupAccessDeniedType<P, Q extends ParsedUrlQuery = ParsedUrlQuery> = (context: GetServerSidePropsContext<Q> & {
228
- user: MonoCloudUser;
229
- }) => Promise<GetServerSidePropsResult<P>> | GetServerSidePropsResult<P>;
230
- /**
231
- * The return type of the `protectPage()` wrapper for Pages Router.
232
- * It returns a function compatible with `getServerSideProps` that injects the user into props.
233
- *
234
- * @typeParam P - The type of the props.
235
- * @typeParam Q - The type of the parsed query object.
236
- *
237
- * @returns `GetServerSidePropsResult` with user injected
238
- */
239
- type ProtectPagePageReturnType<P, Q extends ParsedUrlQuery = ParsedUrlQuery> = (context: GetServerSidePropsContext<Q>) => Promise<GetServerSidePropsResult<P & {
240
- user: MonoCloudUser;
241
- accessDenied?: boolean;
242
- }>>;
243
- /**
244
- * The App Router server component that protectPage wraps and secures
245
- */
246
- type ProtectedAppServerComponent = (props: {
247
- user: MonoCloudUser;
248
- params?: Record<string, string | string[]>;
249
- searchParams?: Record<string, string | string[] | undefined>;
250
- }) => Promise<JSX.Element> | JSX.Element;
251
- /**
252
- * Handler function triggered when a user is not authenticated in an App Router API route.
253
- *
254
- * @param req - The incoming Next.js request.
255
- * @param ctx - The App Router context.
256
- *
257
- * @returns A Response/NextResponse or Promise resolving to one.
258
- */
259
- type AppRouterApiOnAccessDeniedHandler = (req: NextRequest, ctx: AppRouterContext) => Promise<Response> | Response;
260
- /**
261
- * Handler function triggered when a user is denied access in an App Router API route due to group restrictions.
262
- *
263
- * @param req - The incoming Next.js request.
264
- * @param ctx - The App Router context.
265
- * @param user - The authenticated user object.
266
- *
267
- * @returns A Response/NextResponse or Promise resolving to one.
268
- */
269
- type AppRouterApiOnGroupAccessDeniedHandler = (req: NextRequest, ctx: AppRouterContext, user: MonoCloudUser) => Promise<Response> | Response;
270
- /** Options for App Router `protectApi()` */
271
- type ProtectApiAppOptions = {
272
- /**
273
- * Alternate app router api handler called when the user is not authenticated.
274
- */
275
- onAccessDenied?: AppRouterApiOnAccessDeniedHandler;
276
- /**
277
- * Alternate app router api handler called when the user is authenticated but is not a member of the specified groups.
278
- */
279
- onGroupAccessDenied?: AppRouterApiOnGroupAccessDeniedHandler;
280
- } & GroupOptions;
281
- /**
282
- * Handler function triggered when a user is not authenticated in a Pages Router API route.
283
- *
284
- * @param req - The incoming Next.js API request.
285
- * @param res - The Next.js API response.
286
- *
287
- * @returns
288
- */
289
- type PageRouterApiOnAccessDeniedHandler = (req: NextApiRequest, res: NextApiResponse<any>) => Promise<unknown> | unknown;
290
- /**
291
- * Handler function triggered when a user is denied access in a Pages Router API route due to group restrictions.
292
- *
293
- * @param req - The incoming Next.js API request.
294
- * @param res - The Next.js API response.
295
- * @param user - The authenticated user object.
296
- *
297
- * @returns
298
- */
299
- type PageRouterApiOnGroupAccessDeniedHandler = (req: NextApiRequest, res: NextApiResponse<any>, user: MonoCloudUser) => Promise<unknown> | unknown;
300
- /** Options for Page Router `protectApi()` */
301
- type ProtectApiPageOptions = {
302
- /**
303
- * Alternate page router api handler called when the user is not authenticated.
304
- */
305
- onAccessDenied?: PageRouterApiOnAccessDeniedHandler;
306
- /**
307
- * Alternate page router api handler called when the user is authenticated but is not a member of the specified groups.
308
- */
309
- onGroupAccessDenied?: PageRouterApiOnGroupAccessDeniedHandler;
310
- } & GroupOptions;
311
- /**
312
- * Options for the `protect()` helper function.
313
- */
314
- type ProtectOptions = {
315
- /**
316
- * The url where the user will be redirected to after sign in.
317
- */
318
- returnUrl?: string;
319
- /**
320
- * Authorization parameters to be used during authentication.
321
- */
322
- authParams?: ExtraAuthParams;
323
- } & GroupOptions;
324
- /**
325
- * Configuration options for checking if a user belongs to specific groups.
326
- */
327
- interface IsUserInGroupOptions {
328
- /**
329
- * The name of the groups claim in the user profile. Default: `groups`.
330
- */
331
- groupsClaim?: string;
332
- /**
333
- * If true, user must be a member of all groups. Default: false.
334
- */
335
- matchAll?: boolean;
336
- }
337
- /**
338
- * Extended configuration options that include a list of required groups.
339
- */
340
- interface GroupOptions extends IsUserInGroupOptions {
341
- /**
342
- * A list of group IDs or names specifying the groups the user must belong to.
343
- */
344
- groups?: string[];
345
- }
346
- /**
347
- * Options for `redirectToSignIn()`
348
- */
349
- interface RedirectToSignInOptions {
350
- /**
351
- * The URL to which the user should be redirected after successful sign-in.
352
- */
353
- returnUrl?: string;
354
- /**
355
- * Maximum allowed time in seconds since the last End-User authentication.
356
- */
357
- maxAge?: number;
358
- /**
359
- * A hint to the authorization server about the desired authenticator the client wishes to authenticate the user with
360
- */
361
- authenticatorHint?: Authenticators;
362
- /**
363
- * An array of scopes requested from the authorization server
364
- */
365
- scopes?: string[];
366
- /**
367
- * List of resources the access token should be scoped to
368
- */
369
- resource?: string[];
370
- /**
371
- * User's preferred languages and scripts for the user interface
372
- */
373
- uiLocales?: string;
374
- /**
375
- * The desired user interface mode
376
- */
377
- display?: DisplayOptions;
378
- /**
379
- * An array of authentication context class references (ACRs).
380
- */
381
- acrValues?: string[];
382
- /**
383
- * A hint to the authorization server about the user's identifier
384
- */
385
- loginHint?: string;
386
- /**
387
- * The desired authentication behaviour.
388
- * - `none`: User is not prompted to sign in.
389
- * - `login`: Prompt the user to log in even if the user is already authenticated.
390
- * - `consent`: Prompt the user for consent.
391
- * - `select_account`: Prompt the user to sign in.
392
- * - `create`: Prompt the user to sign up.
393
- */
394
- prompt?: Prompt;
395
- }
396
- /**
397
- * Options for `redirectToSignOut()`
398
- */
399
- interface RedirectToSignOutOptions {
400
- /**
401
- * The url authorization server should redirect the user to after a successful sign out. This url has to be registered in the client's sign out url section.
402
- */
403
- postLogoutRedirectUri?: string;
404
- /** Whether to also sign out the user from MonoCloud */
405
- federated?: boolean;
406
- }
407
- //#endregion
408
- export { ProtectPagePageOptions as _, GroupOptions as a, RedirectToSignInOptions as b, MonoCloudAuthOptions as c, PageRouterApiOnAccessDeniedHandler as d, ProtectApiAppOptions as f, ProtectPagePageOnAccessDeniedType as g, ProtectOptions as h, ExtraAuthParams as i, MonoCloudMiddlewareOptions as l, ProtectAppPageOptions as m, AppRouterApiOnAccessDeniedHandler as n, IsUserInGroupOptions as o, ProtectApiPageOptions as p, AppRouterPageHandler as r, MonoCloudAuthHandler as s, AppRouterApiHandlerFn as t, NextMiddlewareResult as u, ProtectPagePageReturnType as v, RedirectToSignOutOptions as x, ProtectedAppServerComponent as y };
409
- //# sourceMappingURL=types-Cx32VRoI.d.mts.map