@monocloud/auth-nextjs 0.1.9 → 0.1.11
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 +1 -1
- package/dist/{chunk-C0xms8kb.cjs → _virtual/_rolldown/runtime.cjs} +1 -6
- package/dist/client/index.cjs +3 -2
- package/dist/client/index.d.mts +3 -203
- package/dist/client/index.mjs +2 -1
- package/dist/{protect-client-page-BdsnH8gs.cjs → client/protect-client-page.cjs} +9 -112
- package/dist/client/protect-client-page.cjs.map +1 -0
- package/dist/client/protect-client-page.d.mts +123 -0
- package/dist/{protect-client-page-BFVskb3X.mjs → client/protect-client-page.mjs} +5 -91
- package/dist/client/protect-client-page.mjs.map +1 -0
- package/dist/client/use-auth.cjs +96 -0
- package/dist/client/use-auth.cjs.map +1 -0
- package/dist/client/use-auth.d.mts +84 -0
- package/dist/client/use-auth.mjs +94 -0
- package/dist/client/use-auth.mjs.map +1 -0
- package/dist/components/client/index.cjs +4 -156
- package/dist/components/client/index.d.mts +3 -189
- package/dist/components/client/index.mjs +3 -153
- package/dist/components/client/protected.cjs +89 -0
- package/dist/components/client/protected.cjs.map +1 -0
- package/dist/components/client/protected.d.mts +114 -0
- package/dist/components/client/protected.mjs +87 -0
- package/dist/components/client/protected.mjs.map +1 -0
- package/dist/components/client/redirect-to-signin.cjs +77 -0
- package/dist/components/client/redirect-to-signin.cjs.map +1 -0
- package/dist/components/client/redirect-to-signin.d.mts +78 -0
- package/dist/components/client/redirect-to-signin.mjs +76 -0
- package/dist/components/client/redirect-to-signin.mjs.map +1 -0
- package/dist/components/index.cjs +6 -168
- package/dist/components/index.d.mts +4 -188
- package/dist/components/index.mjs +4 -164
- package/dist/components/signin.cjs +63 -0
- package/dist/components/signin.cjs.map +1 -0
- package/dist/components/signin.d.mts +72 -0
- package/dist/components/signin.mjs +61 -0
- package/dist/components/signin.mjs.map +1 -0
- package/dist/components/signout.cjs +49 -0
- package/dist/components/signout.cjs.map +1 -0
- package/dist/components/signout.d.mts +55 -0
- package/dist/components/signout.mjs +47 -0
- package/dist/components/signout.mjs.map +1 -0
- package/dist/components/signup.cjs +67 -0
- package/dist/components/signup.cjs.map +1 -0
- package/dist/components/signup.d.mts +70 -0
- package/dist/components/signup.mjs +65 -0
- package/dist/components/signup.mjs.map +1 -0
- package/dist/index.cjs +15 -1136
- package/dist/index.d.mts +5 -1681
- package/dist/index.mjs +4 -1125
- package/dist/initialize.cjs +284 -0
- package/dist/initialize.cjs.map +1 -0
- package/dist/initialize.d.mts +1383 -0
- package/dist/initialize.mjs +274 -0
- package/dist/initialize.mjs.map +1 -0
- package/dist/monocloud-next-client.cjs +600 -0
- package/dist/monocloud-next-client.cjs.map +1 -0
- package/dist/monocloud-next-client.d.mts +330 -0
- package/dist/monocloud-next-client.mjs +599 -0
- package/dist/monocloud-next-client.mjs.map +1 -0
- package/dist/requests/monocloud-app-router-request.cjs +32 -0
- package/dist/requests/monocloud-app-router-request.cjs.map +1 -0
- package/dist/requests/monocloud-app-router-request.mjs +31 -0
- package/dist/requests/monocloud-app-router-request.mjs.map +1 -0
- package/dist/requests/monocloud-cookie-request.cjs +22 -0
- package/dist/requests/monocloud-cookie-request.cjs.map +1 -0
- package/dist/requests/monocloud-cookie-request.mjs +21 -0
- package/dist/requests/monocloud-cookie-request.mjs.map +1 -0
- package/dist/requests/monocloud-page-router-request.cjs +37 -0
- package/dist/requests/monocloud-page-router-request.cjs.map +1 -0
- package/dist/requests/monocloud-page-router-request.mjs +36 -0
- package/dist/requests/monocloud-page-router-request.mjs.map +1 -0
- package/dist/responses/monocloud-app-router-response.cjs +67 -0
- package/dist/responses/monocloud-app-router-response.cjs.map +1 -0
- package/dist/responses/monocloud-app-router-response.mjs +66 -0
- package/dist/responses/monocloud-app-router-response.mjs.map +1 -0
- package/dist/responses/monocloud-cookie-response.cjs +20 -0
- package/dist/responses/monocloud-cookie-response.cjs.map +1 -0
- package/dist/responses/monocloud-cookie-response.mjs +19 -0
- package/dist/responses/monocloud-cookie-response.mjs.map +1 -0
- package/dist/responses/monocloud-page-router-response.cjs +54 -0
- package/dist/responses/monocloud-page-router-response.cjs.map +1 -0
- package/dist/responses/monocloud-page-router-response.mjs +53 -0
- package/dist/responses/monocloud-page-router-response.mjs.map +1 -0
- package/dist/{types-ClljFIvK.d.mts → types.d.mts} +2 -2
- package/dist/utils.cjs +89 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.mjs +80 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/components/client/index.cjs.map +0 -1
- package/dist/components/client/index.mjs.map +0 -1
- package/dist/components/index.cjs.map +0 -1
- package/dist/components/index.mjs.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/protect-client-page-BFVskb3X.mjs.map +0 -1
- package/dist/protect-client-page-BdsnH8gs.cjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,1681 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import { ParsedUrlQuery } from "node:querystring";
|
|
7
|
-
import { MonoCloudOidcClient, MonoCloudSession as MonoCloudSession$2 } from "@monocloud/auth-core";
|
|
8
|
-
import { IncomingMessage, ServerResponse } from "node:http";
|
|
9
|
-
import { NextApiHandler as NextApiHandler$1, NextApiRequest as NextApiRequest$1, NextApiResponse as NextApiResponse$1 } from "next";
|
|
10
|
-
|
|
11
|
-
//#region src/monocloud-next-client.d.ts
|
|
12
|
-
/**
|
|
13
|
-
* `MonoCloudNextClient` is the core SDK entry point for integrating MonoCloud authentication into a Next.js application.
|
|
14
|
-
*
|
|
15
|
-
* It provides:
|
|
16
|
-
* - Authentication middleware
|
|
17
|
-
* - Route protection helpers
|
|
18
|
-
* - Session and token access
|
|
19
|
-
* - Redirect utilities
|
|
20
|
-
* - Server-side enforcement helpers
|
|
21
|
-
*
|
|
22
|
-
* ## 1. Add environment variables
|
|
23
|
-
*
|
|
24
|
-
* ```bash:.env.local
|
|
25
|
-
* MONOCLOUD_AUTH_TENANT_DOMAIN=<tenant-domain>
|
|
26
|
-
* MONOCLOUD_AUTH_CLIENT_ID=<client-id>
|
|
27
|
-
* MONOCLOUD_AUTH_CLIENT_SECRET=<client-secret>
|
|
28
|
-
* MONOCLOUD_AUTH_SCOPES=openid profile email
|
|
29
|
-
* MONOCLOUD_AUTH_APP_URL=http://localhost:3000
|
|
30
|
-
* MONOCLOUD_AUTH_COOKIE_SECRET=<cookie-secret>
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* ## 2. Register middleware
|
|
34
|
-
*
|
|
35
|
-
* ```typescript:src/proxy.ts
|
|
36
|
-
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
37
|
-
*
|
|
38
|
-
* export default authMiddleware();
|
|
39
|
-
*
|
|
40
|
-
* export const config = {
|
|
41
|
-
* matcher: [
|
|
42
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
43
|
-
* ],
|
|
44
|
-
* };
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* ## Advanced usage
|
|
48
|
-
*
|
|
49
|
-
* ### Create a shared client instance
|
|
50
|
-
*
|
|
51
|
-
* By default, the SDK exposes function exports (for example, `authMiddleware()`, `getSession()`, `getTokens()`) that internally use a shared singleton `MonoCloudNextClient`.
|
|
52
|
-
*
|
|
53
|
-
* Create your own `MonoCloudNextClient` instance when you need multiple configurations, dependency injection, or explicit control over initialization.
|
|
54
|
-
*
|
|
55
|
-
* ```ts:src/monocloud.ts
|
|
56
|
-
* import { MonoCloudNextClient } from "@monocloud/auth-nextjs";
|
|
57
|
-
*
|
|
58
|
-
* export const monoCloud = new MonoCloudNextClient();
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* ### Using instance methods
|
|
62
|
-
*
|
|
63
|
-
* Once you create a client instance, call methods directly on it instead of using the default function exports.
|
|
64
|
-
*
|
|
65
|
-
* ```ts:src/app/page.tsx
|
|
66
|
-
* import { monoCloud } from "@/monocloud";
|
|
67
|
-
*
|
|
68
|
-
* export default async function Page() {
|
|
69
|
-
* const session = await monoCloud.getSession();
|
|
70
|
-
*
|
|
71
|
-
* if (!session) {
|
|
72
|
-
* return <>Not signed in</>;
|
|
73
|
-
* }
|
|
74
|
-
*
|
|
75
|
-
* return <>Hello {session.user.name}</>;
|
|
76
|
-
* }
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* #### Using constructor options
|
|
80
|
-
*
|
|
81
|
-
* When configuration is provided through both constructor options and environment variables, the values passed to the constructor take precedence. Environment variables are used only for options that are not explicitly supplied.
|
|
82
|
-
*
|
|
83
|
-
* ```ts:src/monocloud.ts
|
|
84
|
-
* import { MonoCloudNextClient } from "@monocloud/auth-nextjs";
|
|
85
|
-
*
|
|
86
|
-
* export const monoCloud = new MonoCloudNextClient({
|
|
87
|
-
* tenantDomain: "<tenant-domain>",
|
|
88
|
-
* clientId: "<client-id>",
|
|
89
|
-
* clientSecret: "<client-secret>",
|
|
90
|
-
* appUrl: "http://localhost:3000",
|
|
91
|
-
* cookieSecret: "<cookie-secret>",
|
|
92
|
-
* defaultAuthParams: {
|
|
93
|
-
* scopes: "openid profile email",
|
|
94
|
-
* },
|
|
95
|
-
* });
|
|
96
|
-
* ```
|
|
97
|
-
*
|
|
98
|
-
* ### Modifying default routes
|
|
99
|
-
*
|
|
100
|
-
* If you customize any of the default auth route paths:
|
|
101
|
-
*
|
|
102
|
-
* - Also set the corresponding `NEXT_PUBLIC_` environment variables so client-side helpers
|
|
103
|
-
* (for example `<SignIn />`, `<SignOut />`, and `useAuth()`) can discover the correct URLs.
|
|
104
|
-
* - Update the **Application URLs** in your MonoCloud Dashboard to match the new paths.
|
|
105
|
-
*
|
|
106
|
-
* Example:
|
|
107
|
-
*
|
|
108
|
-
* ```bash:.env.local
|
|
109
|
-
* MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
110
|
-
* NEXT_PUBLIC_MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
111
|
-
* ```
|
|
112
|
-
*
|
|
113
|
-
* When routes are overridden, the Redirect URI configured in the dashboard
|
|
114
|
-
* must reflect the new path. For example, during local development:
|
|
115
|
-
*
|
|
116
|
-
* `http://localhost:3000/api/custom_callback`
|
|
117
|
-
*
|
|
118
|
-
* @category Classes
|
|
119
|
-
*/
|
|
120
|
-
declare class MonoCloudNextClient {
|
|
121
|
-
private readonly _coreClient;
|
|
122
|
-
/**
|
|
123
|
-
* This exposes the framework-agnostic MonoCloud client used internally by the Next.js SDK.
|
|
124
|
-
* Use it if you need access to lower-level functionality not directly exposed by MonoCloudNextClient.
|
|
125
|
-
*
|
|
126
|
-
* @returns Returns the underlying **Node client** instance.
|
|
127
|
-
*/
|
|
128
|
-
get coreClient(): MonoCloudCoreClient;
|
|
129
|
-
/**
|
|
130
|
-
* This is intended for advanced scenarios requiring direct control over the authorization or token flow.
|
|
131
|
-
*
|
|
132
|
-
* @returns Returns the underlying **OIDC client** used for OpenID Connect operations.
|
|
133
|
-
*/
|
|
134
|
-
get oidcClient(): MonoCloudOidcClient;
|
|
135
|
-
/**
|
|
136
|
-
* Creates a new client instance.
|
|
137
|
-
*
|
|
138
|
-
* @param options Optional configuration for initializing the MonoCloud client. If not provided, settings are automatically resolved from environment variables.
|
|
139
|
-
*/
|
|
140
|
-
constructor(options?: MonoCloudOptions$1);
|
|
141
|
-
/**
|
|
142
|
-
* @see {@link monoCloudAuth} for full docs and examples.
|
|
143
|
-
* @param options Optional configuration for the auth handler.
|
|
144
|
-
* @returns Returns a Next.js-compatible handler for App Router route handlers or Pages Router API routes.
|
|
145
|
-
*/
|
|
146
|
-
monoCloudAuth(options?: MonoCloudAuthOptions): MonoCloudAuthHandler;
|
|
147
|
-
/**
|
|
148
|
-
* @see {@link protectPage} for full docs and examples.
|
|
149
|
-
* @param component The App Router server component to protect.
|
|
150
|
-
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
151
|
-
* @returns A wrapped page component that enforces authentication before rendering.
|
|
152
|
-
*/
|
|
153
|
-
protectPage(component: ProtectedAppServerComponent, options?: ProtectAppPageOptions): AppRouterPageHandler;
|
|
154
|
-
/**
|
|
155
|
-
* @see {@link protectPage} for full docs and examples.
|
|
156
|
-
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
157
|
-
* @typeParam P - Props returned from `getServerSideProps`.
|
|
158
|
-
* @typeParam Q - Query parameters parsed from the URL.
|
|
159
|
-
* @returns A getServerSideProps wrapper that enforces authentication before executing the page logic.
|
|
160
|
-
*/
|
|
161
|
-
protectPage<P extends Record<string, any> = Record<string, any>, Q extends ParsedUrlQuery = ParsedUrlQuery>(options?: ProtectPagePageOptions<P, Q>): ProtectPagePageReturnType<P, Q>;
|
|
162
|
-
private protectAppPage;
|
|
163
|
-
private protectPagePage;
|
|
164
|
-
/**
|
|
165
|
-
* @see {@link protectApi} for full docs and examples.
|
|
166
|
-
* @param handler The route handler to protect.
|
|
167
|
-
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
168
|
-
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
169
|
-
*/
|
|
170
|
-
protectApi(handler: AppRouterApiHandlerFn, options?: ProtectApiAppOptions): AppRouterApiHandlerFn;
|
|
171
|
-
/**
|
|
172
|
-
* @see {@link protectApi} for full docs and examples.
|
|
173
|
-
* @param handler - The route handler to protect.
|
|
174
|
-
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
175
|
-
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
176
|
-
*/
|
|
177
|
-
protectApi(handler: NextApiHandler, options?: ProtectApiPageOptions): NextApiHandler;
|
|
178
|
-
private protectAppApi;
|
|
179
|
-
private protectPageApi;
|
|
180
|
-
/**
|
|
181
|
-
* @see {@link authMiddleware} for full docs and examples.
|
|
182
|
-
* @param options Optional configuration that controls how authentication is enforced (for example, redirect behavior, route matching, or custom handling of unauthenticated requests).
|
|
183
|
-
* @returns Returns a Next.js middleware result (`NextResponse`, redirect, or `undefined` to continue processing).
|
|
184
|
-
*/
|
|
185
|
-
authMiddleware(options?: MonoCloudMiddlewareOptions): NextMiddleware | NextProxy;
|
|
186
|
-
/**
|
|
187
|
-
* @see {@link authMiddleware} for full docs and examples.
|
|
188
|
-
* @param request Incoming Next.js middleware request used to resolve authentication state.
|
|
189
|
-
* @param event Next.js middleware event providing lifecycle hooks such as `waitUntil`.
|
|
190
|
-
* @returns Returns a Next.js middleware result (`NextResponse`, redirect, or `undefined` to continue processing).
|
|
191
|
-
*/
|
|
192
|
-
authMiddleware(request: NextRequest, event: NextFetchEvent): Promise<NextMiddlewareResult> | NextMiddlewareResult;
|
|
193
|
-
private authMiddlewareHandler;
|
|
194
|
-
private handleAuthRoutes;
|
|
195
|
-
/**
|
|
196
|
-
* @see {@link getSession} for full docs and examples.
|
|
197
|
-
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
198
|
-
*/
|
|
199
|
-
getSession(): Promise<MonoCloudSession$1 | undefined>;
|
|
200
|
-
/**
|
|
201
|
-
* @see {@link getSession} for full docs and examples.
|
|
202
|
-
* @param req Incoming request used to read authentication cookies and headers to resolve the current user's session.
|
|
203
|
-
* @param res Optional response to update if session resolution requires refreshed authentication cookies or headers.
|
|
204
|
-
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
205
|
-
*/
|
|
206
|
-
getSession(req: NextRequest | Request, res?: NextResponse | Response): Promise<MonoCloudSession$1 | undefined>;
|
|
207
|
-
/**
|
|
208
|
-
* @see {@link getSession} for full docs and examples.
|
|
209
|
-
* @param req Incoming Node.js request used to read authentication cookies and resolve the current user's session.
|
|
210
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
211
|
-
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
212
|
-
*/
|
|
213
|
-
getSession(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>): Promise<MonoCloudSession$1 | undefined>;
|
|
214
|
-
/**
|
|
215
|
-
* @see {@link getTokens} for full docs and examples.
|
|
216
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
217
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
218
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
219
|
-
*/
|
|
220
|
-
getTokens(options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
221
|
-
/**
|
|
222
|
-
* @see {@link getTokens} for full docs and examples.
|
|
223
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
224
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
225
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
226
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
227
|
-
*/
|
|
228
|
-
getTokens(req: NextRequest | Request, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
229
|
-
/**
|
|
230
|
-
* @see {@link getTokens} for full docs and examples.
|
|
231
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
232
|
-
* @param res Existing response to update with refreshed authentication cookies or headers.
|
|
233
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
234
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
235
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
236
|
-
*/
|
|
237
|
-
getTokens(req: NextRequest | Request, res: NextResponse | Response, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
238
|
-
/**
|
|
239
|
-
* @see {@link getTokens} for full docs and examples.
|
|
240
|
-
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
241
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
242
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
243
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
244
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
245
|
-
*/
|
|
246
|
-
getTokens(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
247
|
-
/**
|
|
248
|
-
* @see {@link isAuthenticated} for full docs and examples.
|
|
249
|
-
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
250
|
-
*/
|
|
251
|
-
isAuthenticated(): Promise<boolean>;
|
|
252
|
-
/**
|
|
253
|
-
* @see {@link isAuthenticated} for full docs and examples.
|
|
254
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
255
|
-
* @param res Optional response to update if refreshed authentication cookies or headers are required.
|
|
256
|
-
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
257
|
-
*/
|
|
258
|
-
isAuthenticated(req: NextRequest | Request, res?: NextResponse | Response): Promise<boolean>;
|
|
259
|
-
/**
|
|
260
|
-
* @see {@link isAuthenticated} for full docs and examples.
|
|
261
|
-
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
262
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
263
|
-
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
264
|
-
*/
|
|
265
|
-
isAuthenticated(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>): Promise<boolean>;
|
|
266
|
-
/**
|
|
267
|
-
* @see {@link protect} for full docs and examples.
|
|
268
|
-
* @param options Optional configuration for redirect behavior (for example, return URL or sign-in parameters).
|
|
269
|
-
* @returns Resolves if the user is authenticated; otherwise triggers a redirect.
|
|
270
|
-
*/
|
|
271
|
-
protect(options?: ProtectOptions): Promise<void>;
|
|
272
|
-
/**
|
|
273
|
-
* @see {@link isUserInGroup} for full docs and examples.
|
|
274
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
275
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
276
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
277
|
-
*/
|
|
278
|
-
isUserInGroup(groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
279
|
-
/**
|
|
280
|
-
* @see {@link isUserInGroup} for full docs and examples.
|
|
281
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
282
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
283
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
284
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
285
|
-
*/
|
|
286
|
-
isUserInGroup(req: NextRequest | Request, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
287
|
-
/**
|
|
288
|
-
* @see {@link isUserInGroup} for full docs and examples.
|
|
289
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
290
|
-
* @param res Existing response to update with refreshed authentication cookies or headers when required.
|
|
291
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
292
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
293
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
294
|
-
*/
|
|
295
|
-
isUserInGroup(req: NextRequest | Request, res: NextResponse | Response, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
296
|
-
/**
|
|
297
|
-
* @see {@link isUserInGroup} for full docs and examples.
|
|
298
|
-
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
299
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
300
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
301
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
302
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
303
|
-
*/
|
|
304
|
-
isUserInGroup(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
305
|
-
/**
|
|
306
|
-
* @see {@link redirectToSignIn} for full docs and examples.
|
|
307
|
-
* @param options Optional configuration for the redirect, such as `returnUrl` or additional sign-in parameters.
|
|
308
|
-
* @returns Never resolves. Triggers a redirect to the sign-in flow.
|
|
309
|
-
*/
|
|
310
|
-
redirectToSignIn(options?: RedirectToSignInOptions): Promise<void>;
|
|
311
|
-
/**
|
|
312
|
-
* @see {@link redirectToSignOut} for full docs and examples.
|
|
313
|
-
* @param options Optional configuration for the redirect, such as `postLogoutRedirectUri` or additional sign-out parameters.
|
|
314
|
-
* @returns Never resolves. Triggers a redirect to the sign-out flow.
|
|
315
|
-
*/
|
|
316
|
-
redirectToSignOut(options?: RedirectToSignOutOptions): Promise<void>;
|
|
317
|
-
private getOptions;
|
|
318
|
-
private registerPublicEnvVariables;
|
|
319
|
-
}
|
|
320
|
-
//#endregion
|
|
321
|
-
//#region src/initialize.d.ts
|
|
322
|
-
/**
|
|
323
|
-
* Creates a Next.js catch-all auth route handler (Pages Router and App Router) for the built-in routes (`/signin`, `/callback`, `/userinfo`, `/signout`).
|
|
324
|
-
*
|
|
325
|
-
* Mount this handler on a catch-all route (e.g. `/api/auth/[...monocloud]`).
|
|
326
|
-
*
|
|
327
|
-
* > If you already use `authMiddleware()`, you typically don’t need this handler. Use `monoCloudAuth()` when middleware cannot be used or when auth routes need customization.
|
|
328
|
-
*
|
|
329
|
-
* @example App Router
|
|
330
|
-
* ```tsx:src/app/api/auth/[...monocloud]/route.ts tab="App Router" tab-group="monoCloudAuth"
|
|
331
|
-
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
332
|
-
*
|
|
333
|
-
* export const GET = monoCloudAuth();
|
|
334
|
-
*```
|
|
335
|
-
*
|
|
336
|
-
* @example App Router (Response)
|
|
337
|
-
* ```tsx:src/app/api/auth/[...monocloud]/route.ts tab="App Router (Response)" tab-group="monoCloudAuth"
|
|
338
|
-
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
339
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
340
|
-
*
|
|
341
|
-
* export const GET = (req: NextRequest) => {
|
|
342
|
-
* const authHandler = monoCloudAuth();
|
|
343
|
-
*
|
|
344
|
-
* const res = new NextResponse();
|
|
345
|
-
*
|
|
346
|
-
* res.cookies.set("last_auth_requested", `${Date.now()}`);
|
|
347
|
-
*
|
|
348
|
-
* return authHandler(req, res);
|
|
349
|
-
* };
|
|
350
|
-
* ```
|
|
351
|
-
*
|
|
352
|
-
* @example Pages Router
|
|
353
|
-
* ```tsx:src/pages/api/auth/[...monocloud].ts tab="Pages Router" tab-group="monoCloudAuth"
|
|
354
|
-
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
355
|
-
*
|
|
356
|
-
* export default monoCloudAuth();
|
|
357
|
-
*```
|
|
358
|
-
*
|
|
359
|
-
* @example Pages Router (Response)
|
|
360
|
-
* ```tsx:src/pages/api/auth/[...monocloud].ts tab="Pages Router (Response)" tab-group="monoCloudAuth"
|
|
361
|
-
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
362
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
363
|
-
*
|
|
364
|
-
* export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
365
|
-
* const authHandler = monoCloudAuth();
|
|
366
|
-
*
|
|
367
|
-
* res.setHeader("last_auth_requested", `${Date.now()}`);
|
|
368
|
-
*
|
|
369
|
-
* return authHandler(req, res);
|
|
370
|
-
* }
|
|
371
|
-
* ```
|
|
372
|
-
*
|
|
373
|
-
* @param options Optional configuration for the auth handler.
|
|
374
|
-
* @returns Returns a Next.js-compatible handler for App Router route handlers or Pages Router API routes.
|
|
375
|
-
*
|
|
376
|
-
* @category Functions
|
|
377
|
-
*/
|
|
378
|
-
declare function monoCloudAuth(options?: MonoCloudAuthOptions): MonoCloudAuthHandler;
|
|
379
|
-
/**
|
|
380
|
-
* Creates a Next.js authentication middleware that protects routes.
|
|
381
|
-
*
|
|
382
|
-
* By default, all routes matched by `config.matcher` are protected unless configured otherwise.
|
|
383
|
-
*
|
|
384
|
-
* @example Protect All Routes
|
|
385
|
-
* ```tsx:src/proxy.ts tab="Protect All Routes" tab-group="auth-middleware"
|
|
386
|
-
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
387
|
-
*
|
|
388
|
-
* export default authMiddleware();
|
|
389
|
-
*
|
|
390
|
-
* export const config = {
|
|
391
|
-
* matcher: [
|
|
392
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
393
|
-
* ],
|
|
394
|
-
* };
|
|
395
|
-
* ```
|
|
396
|
-
*
|
|
397
|
-
* @example Protect Selected Routes
|
|
398
|
-
* ```tsx:src/proxy.ts tab="Protect Selected Routes" tab-group="auth-middleware"
|
|
399
|
-
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
400
|
-
*
|
|
401
|
-
* export default authMiddleware({
|
|
402
|
-
* protectedRoutes: ["/api/admin", "^/api/protected(/.*)?$"],
|
|
403
|
-
* });
|
|
404
|
-
*
|
|
405
|
-
* export const config = {
|
|
406
|
-
* matcher: [
|
|
407
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
408
|
-
* ],
|
|
409
|
-
* };
|
|
410
|
-
*```
|
|
411
|
-
*
|
|
412
|
-
* @example No Protected Routes
|
|
413
|
-
* ```tsx:src/proxy.ts tab="No Protected Routes" tab-group="auth-middleware"
|
|
414
|
-
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
415
|
-
*
|
|
416
|
-
* export default authMiddleware({
|
|
417
|
-
* protectedRoutes: [],
|
|
418
|
-
* });
|
|
419
|
-
*
|
|
420
|
-
* export const config = {
|
|
421
|
-
* matcher: [
|
|
422
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
423
|
-
* ],
|
|
424
|
-
* };
|
|
425
|
-
* ```
|
|
426
|
-
*
|
|
427
|
-
* @example Dynamic
|
|
428
|
-
* ```tsx:src/proxy.ts tab="Dynamic" tab-group="auth-middleware"
|
|
429
|
-
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
430
|
-
*
|
|
431
|
-
* export default authMiddleware({
|
|
432
|
-
* protectedRoutes: (req) => {
|
|
433
|
-
* return req.nextUrl.pathname.startsWith("/api/protected");
|
|
434
|
-
* },
|
|
435
|
-
* });
|
|
436
|
-
*
|
|
437
|
-
* export const config = {
|
|
438
|
-
* matcher: [
|
|
439
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
440
|
-
* ],
|
|
441
|
-
* };
|
|
442
|
-
* ```
|
|
443
|
-
*
|
|
444
|
-
* @example Group Protection
|
|
445
|
-
* ```tsx:src/proxy.ts tab="Group Protection" tab-group="auth-middleware"
|
|
446
|
-
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
447
|
-
*
|
|
448
|
-
* export default authMiddleware({
|
|
449
|
-
* protectedRoutes: [
|
|
450
|
-
* {
|
|
451
|
-
* groups: ["admin", "editor", "537e7c3d-a442-4b5b-b308-30837aa045a4"],
|
|
452
|
-
* routes: ["/internal", "/api/internal(.*)"],
|
|
453
|
-
* },
|
|
454
|
-
* ],
|
|
455
|
-
* });
|
|
456
|
-
*
|
|
457
|
-
* export const config = {
|
|
458
|
-
* matcher: [
|
|
459
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
460
|
-
* ],
|
|
461
|
-
* };
|
|
462
|
-
* ```
|
|
463
|
-
*
|
|
464
|
-
* @param options Optional configuration that controls how authentication is enforced (for example, redirect behavior, route matching, or custom handling of unauthenticated requests).
|
|
465
|
-
* @returns Returns a Next.js middleware result, such as a `NextResponse`, `redirect`, or `undefined` to continue processing.
|
|
466
|
-
*
|
|
467
|
-
* @category Functions
|
|
468
|
-
*/
|
|
469
|
-
declare function authMiddleware(options?: MonoCloudMiddlewareOptions): NextMiddleware$1 | NextProxy$1;
|
|
470
|
-
/**
|
|
471
|
-
* Executes the authentication middleware manually.
|
|
472
|
-
*
|
|
473
|
-
* Intended for advanced scenarios where the middleware is composed within custom logic.
|
|
474
|
-
*
|
|
475
|
-
* @example Composing with custom middleware
|
|
476
|
-
*
|
|
477
|
-
* ```tsx:src/proxy.ts title="Composing with custom middleware"
|
|
478
|
-
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
479
|
-
* import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
|
|
480
|
-
*
|
|
481
|
-
* export default function customMiddleware(req: NextRequest, evt: NextFetchEvent) {
|
|
482
|
-
* if (req.nextUrl.pathname.startsWith("/api/protected")) {
|
|
483
|
-
* return authMiddleware(req, evt);
|
|
484
|
-
* }
|
|
485
|
-
*
|
|
486
|
-
* return NextResponse.next();
|
|
487
|
-
* }
|
|
488
|
-
*
|
|
489
|
-
* export const config = {
|
|
490
|
-
* matcher: [
|
|
491
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
492
|
-
* ],
|
|
493
|
-
* };
|
|
494
|
-
* ```
|
|
495
|
-
*
|
|
496
|
-
* @param request Incoming Next.js middleware request used to resolve authentication state.
|
|
497
|
-
* @param event Next.js middleware event providing lifecycle hooks such as `waitUntil`.
|
|
498
|
-
* @returns Returns a Next.js middleware result (`NextResponse`, redirect, or `undefined` to continue processing).
|
|
499
|
-
*
|
|
500
|
-
* @category Functions
|
|
501
|
-
*/
|
|
502
|
-
declare function authMiddleware(request: NextRequest$1, event: NextFetchEvent$1): Promise<NextMiddlewareResult> | NextMiddlewareResult;
|
|
503
|
-
/**
|
|
504
|
-
* Retrieves the current user's session using the active server request context.
|
|
505
|
-
*
|
|
506
|
-
* Intended for Server Components, Server Actions, Route Handlers, and Middleware where the request is implicitly available.
|
|
507
|
-
*
|
|
508
|
-
* @example Server Component
|
|
509
|
-
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="session-ssr"
|
|
510
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
511
|
-
*
|
|
512
|
-
* export default async function Home() {
|
|
513
|
-
* const session = await getSession();
|
|
514
|
-
*
|
|
515
|
-
* return <div>{session?.user.name}</div>;
|
|
516
|
-
* }
|
|
517
|
-
* ```
|
|
518
|
-
*
|
|
519
|
-
* @example Server Action
|
|
520
|
-
* ```tsx:src/action.ts tab="Server Action" tab-group="session-ssr"
|
|
521
|
-
* "use server";
|
|
522
|
-
*
|
|
523
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
524
|
-
*
|
|
525
|
-
* export async function getUserAction() {
|
|
526
|
-
* const session = await getSession();
|
|
527
|
-
*
|
|
528
|
-
* return { name: session?.user.name };
|
|
529
|
-
* }
|
|
530
|
-
* ```
|
|
531
|
-
*
|
|
532
|
-
* @example API Handler
|
|
533
|
-
* ```tsx:src/app/api/user/route.ts tab="API Handler" tab-group="session-ssr"
|
|
534
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
535
|
-
* import { NextResponse } from "next/server";
|
|
536
|
-
*
|
|
537
|
-
* export const GET = async () => {
|
|
538
|
-
* const session = await getSession();
|
|
539
|
-
*
|
|
540
|
-
* return NextResponse.json({ name: session?.user.name });
|
|
541
|
-
* };
|
|
542
|
-
* ```
|
|
543
|
-
*
|
|
544
|
-
* @example Middleware
|
|
545
|
-
* ```tsx:src/proxy.ts tab="Middleware" tab-group="session-ssr"
|
|
546
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
547
|
-
* import { NextResponse } from "next/server";
|
|
548
|
-
*
|
|
549
|
-
* export default async function proxy() {
|
|
550
|
-
* const session = await getSession();
|
|
551
|
-
*
|
|
552
|
-
* if (!session) {
|
|
553
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
554
|
-
* }
|
|
555
|
-
*
|
|
556
|
-
* return NextResponse.next();
|
|
557
|
-
* }
|
|
558
|
-
* ```
|
|
559
|
-
*
|
|
560
|
-
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
561
|
-
*
|
|
562
|
-
* @category Functions
|
|
563
|
-
*/
|
|
564
|
-
declare function getSession(): Promise<MonoCloudSession$2 | undefined>;
|
|
565
|
-
/**
|
|
566
|
-
* Retrieves the current user's session using an explicit Web or Next.js request.
|
|
567
|
-
*
|
|
568
|
-
* Use this overload when you already have access to a `Request` or `NextRequest` (for example in Middleware or Route Handlers).
|
|
569
|
-
*
|
|
570
|
-
* @example Middleware (Request)
|
|
571
|
-
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="session-route-handler"
|
|
572
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
573
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
574
|
-
*
|
|
575
|
-
* export default async function proxy(req: NextRequest) {
|
|
576
|
-
* const session = await getSession(req);
|
|
577
|
-
*
|
|
578
|
-
* if (!session) {
|
|
579
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
580
|
-
* }
|
|
581
|
-
*
|
|
582
|
-
* return NextResponse.next();
|
|
583
|
-
* }
|
|
584
|
-
* ```
|
|
585
|
-
*
|
|
586
|
-
* @example Middleware (Response)
|
|
587
|
-
* ```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="session-route-handler"
|
|
588
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
589
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
590
|
-
*
|
|
591
|
-
* export default async function proxy(req: NextRequest) {
|
|
592
|
-
* const res = NextResponse.next();
|
|
593
|
-
*
|
|
594
|
-
* const session = await getSession(req, res);
|
|
595
|
-
*
|
|
596
|
-
* if (!session) {
|
|
597
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
598
|
-
* }
|
|
599
|
-
*
|
|
600
|
-
* res.headers.set("x-auth-status", "active");
|
|
601
|
-
*
|
|
602
|
-
* return res;
|
|
603
|
-
* }
|
|
604
|
-
* ```
|
|
605
|
-
*
|
|
606
|
-
* @example API Handler (Request)
|
|
607
|
-
* ```tsx:src/app/api/user/route.ts tab="API Handler (Request)" tab-group="session-route-handler"
|
|
608
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
609
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
610
|
-
*
|
|
611
|
-
* export const GET = async (req: NextRequest) => {
|
|
612
|
-
* const session = await getSession(req);
|
|
613
|
-
*
|
|
614
|
-
* return NextResponse.json({ name: session?.user.name });
|
|
615
|
-
* };
|
|
616
|
-
* ```
|
|
617
|
-
*
|
|
618
|
-
* @example API Handler (Response)
|
|
619
|
-
* ```tsx:src/app/api/user/route.ts tab="API Handler (Response)" tab-group="session-route-handler"
|
|
620
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
621
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
622
|
-
*
|
|
623
|
-
* export const GET = async (req: NextRequest) => {
|
|
624
|
-
* const res = new NextResponse("YOUR CUSTOM RESPONSE");
|
|
625
|
-
*
|
|
626
|
-
* const session = await getSession(req, res);
|
|
627
|
-
*
|
|
628
|
-
* if (session?.user) {
|
|
629
|
-
* res.cookies.set("something", "important");
|
|
630
|
-
* }
|
|
631
|
-
*
|
|
632
|
-
* return res;
|
|
633
|
-
* };
|
|
634
|
-
* ```
|
|
635
|
-
*
|
|
636
|
-
* @param req Incoming request used to read authentication cookies and headers to resolve the current user's session.
|
|
637
|
-
* @param res Optional response to update if session resolution requires refreshed authentication cookies or headers.
|
|
638
|
-
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
639
|
-
*
|
|
640
|
-
* @category Functions
|
|
641
|
-
*/
|
|
642
|
-
declare function getSession(req: NextRequest$1 | Request, res?: NextResponse$1 | Response): Promise<MonoCloudSession$2 | undefined>;
|
|
643
|
-
/**
|
|
644
|
-
* Retrieves the current user's session in the Pages Router or Node.js runtime.
|
|
645
|
-
*
|
|
646
|
-
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
647
|
-
*
|
|
648
|
-
* @example Pages Router (Pages)
|
|
649
|
-
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="session-pages"
|
|
650
|
-
* import { getSession, MonoCloudSession } from "@monocloud/auth-nextjs";
|
|
651
|
-
* import { GetServerSideProps } from "next";
|
|
652
|
-
*
|
|
653
|
-
* type Props = {
|
|
654
|
-
* session?: MonoCloudSession;
|
|
655
|
-
* };
|
|
656
|
-
*
|
|
657
|
-
* export default function Home({ session }: Props) {
|
|
658
|
-
* return <pre>Session: {JSON.stringify(session, null, 2)}</pre>;
|
|
659
|
-
* }
|
|
660
|
-
*
|
|
661
|
-
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
662
|
-
* const session = await getSession(ctx.req, ctx.res);
|
|
663
|
-
*
|
|
664
|
-
* return {
|
|
665
|
-
* props: {
|
|
666
|
-
* session
|
|
667
|
-
* }
|
|
668
|
-
* };
|
|
669
|
-
* };
|
|
670
|
-
* ```
|
|
671
|
-
* @example Pages Router (API)
|
|
672
|
-
* ```tsx:src/pages/api/user.ts tab="Pages Router (API)" tab-group="session-pages"
|
|
673
|
-
* import { getSession } from "@monocloud/auth-nextjs";
|
|
674
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
675
|
-
*
|
|
676
|
-
* export default async function handler(
|
|
677
|
-
* req: NextApiRequest,
|
|
678
|
-
* res: NextApiResponse
|
|
679
|
-
* ) {
|
|
680
|
-
* const session = await getSession(req, res);
|
|
681
|
-
*
|
|
682
|
-
* res.status(200).json({ name: session?.user.name });
|
|
683
|
-
* }
|
|
684
|
-
* ```
|
|
685
|
-
*
|
|
686
|
-
* @param req Incoming Node.js request used to read authentication cookies and resolve the current user's session.
|
|
687
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
688
|
-
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
689
|
-
*
|
|
690
|
-
* @category Functions
|
|
691
|
-
*/
|
|
692
|
-
declare function getSession(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>): Promise<MonoCloudSession$2 | undefined>;
|
|
693
|
-
/**
|
|
694
|
-
* Retrieves the current user's tokens using the active server request context.
|
|
695
|
-
*
|
|
696
|
-
* @example Server Component
|
|
697
|
-
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="tokens-ssr"
|
|
698
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
699
|
-
*
|
|
700
|
-
* export default async function Home() {
|
|
701
|
-
* const tokens = await getTokens();
|
|
702
|
-
*
|
|
703
|
-
* return <div>Expired: {tokens.isExpired.toString()}</div>;
|
|
704
|
-
* }
|
|
705
|
-
* ```
|
|
706
|
-
*
|
|
707
|
-
* @example Server Action
|
|
708
|
-
* ```tsx:src/action.ts tab="Server Action" tab-group="tokens-ssr"
|
|
709
|
-
* "use server";
|
|
710
|
-
*
|
|
711
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
712
|
-
*
|
|
713
|
-
* export async function getExpiredAction() {
|
|
714
|
-
* const tokens = await getTokens();
|
|
715
|
-
*
|
|
716
|
-
* return { expired: tokens.isExpired };
|
|
717
|
-
* }
|
|
718
|
-
* ```
|
|
719
|
-
*
|
|
720
|
-
* @example API Handler
|
|
721
|
-
* ```tsx:src/app/api/tokens/route.ts tab="API Handler" tab-group="tokens-ssr"
|
|
722
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
723
|
-
* import { NextResponse } from "next/server";
|
|
724
|
-
*
|
|
725
|
-
* export const GET = async () => {
|
|
726
|
-
* const tokens = await getTokens();
|
|
727
|
-
*
|
|
728
|
-
* return NextResponse.json({ expired: tokens.isExpired });
|
|
729
|
-
* };
|
|
730
|
-
* ```
|
|
731
|
-
*
|
|
732
|
-
* @example Middleware
|
|
733
|
-
* ```tsx:src/proxy.ts tab="Middleware" tab-group="tokens-ssr"
|
|
734
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
735
|
-
* import { NextResponse } from "next/server";
|
|
736
|
-
*
|
|
737
|
-
* export default async function proxy() {
|
|
738
|
-
* const tokens = await getTokens();
|
|
739
|
-
*
|
|
740
|
-
* if (tokens.isExpired) {
|
|
741
|
-
* return new NextResponse("Tokens expired", { status: 401 });
|
|
742
|
-
* }
|
|
743
|
-
*
|
|
744
|
-
* return NextResponse.next();
|
|
745
|
-
* }
|
|
746
|
-
* ```
|
|
747
|
-
*
|
|
748
|
-
* @example Refresh the default token
|
|
749
|
-
*
|
|
750
|
-
* The **default token** is the access token associated with your default authorization parameters:
|
|
751
|
-
* - Scopes: `MONOCLOUD_AUTH_SCOPES` or `options.defaultAuthParams.scopes`
|
|
752
|
-
* - Resource: `MONOCLOUD_AUTH_RESOURCE` or `options.defaultAuthParams.resource`
|
|
753
|
-
*
|
|
754
|
-
* Calling `getTokens()` returns the current token set and **refreshes the default token automatically when needed** (for example, if it has expired). To force a refresh even when it isn’t expired, use `forceRefresh: true`.
|
|
755
|
-
*
|
|
756
|
-
* ```tsx:src/app/page.tsx title="Refresh default token"
|
|
757
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
758
|
-
* import { NextResponse } from "next/server";
|
|
759
|
-
*
|
|
760
|
-
* export const GET = async () => {
|
|
761
|
-
* // Forces a refresh of the default token
|
|
762
|
-
* const tokens = await getTokens({ forceRefresh: true });
|
|
763
|
-
*
|
|
764
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
765
|
-
* };
|
|
766
|
-
* ```
|
|
767
|
-
*
|
|
768
|
-
* @example Request an access token for resource(s)
|
|
769
|
-
*
|
|
770
|
-
* Use `resource` and `scopes` to request an access token for one or more resources.
|
|
771
|
-
*
|
|
772
|
-
* > The requested resource and scopes must be included in the initial authorization flow (so the user has consented / the session is eligible to mint that token).
|
|
773
|
-
*
|
|
774
|
-
* ```tsx:src/app/page.tsx title="Request a new access token for resource(s)"
|
|
775
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
776
|
-
* import { NextResponse } from "next/server";
|
|
777
|
-
*
|
|
778
|
-
* export const GET = async () => {
|
|
779
|
-
* const tokens = await getTokens({
|
|
780
|
-
* resource: "https://first.example.com https://second.example.com",
|
|
781
|
-
* scopes: "read:first read:second shared",
|
|
782
|
-
* });
|
|
783
|
-
*
|
|
784
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
785
|
-
* };
|
|
786
|
-
* ```
|
|
787
|
-
*
|
|
788
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
789
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
790
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
791
|
-
*
|
|
792
|
-
* @category Functions
|
|
793
|
-
*/
|
|
794
|
-
declare function getTokens(options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
795
|
-
/**
|
|
796
|
-
* Retrieves the current user's tokens using an explicit Web or Next.js request.
|
|
797
|
-
*
|
|
798
|
-
* Use this overload when you already have access to a `Request` or `NextRequest` (for example, in Middleware or Route Handlers).
|
|
799
|
-
*
|
|
800
|
-
* @example Middleware (Request)
|
|
801
|
-
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="tokens-route-handler-request"
|
|
802
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
803
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
804
|
-
*
|
|
805
|
-
* export default async function proxy(req: NextRequest) {
|
|
806
|
-
* const tokens = await getTokens(req);
|
|
807
|
-
*
|
|
808
|
-
* if (tokens.isExpired) {
|
|
809
|
-
* return new NextResponse("Tokens expired", { status: 401 });
|
|
810
|
-
* }
|
|
811
|
-
*
|
|
812
|
-
* return NextResponse.next();
|
|
813
|
-
* }
|
|
814
|
-
* ```
|
|
815
|
-
*
|
|
816
|
-
* @example API Handler (Request)
|
|
817
|
-
* ```tsx:src/app/api/tokens/route.ts tab="API Handler (Request)" tab-group="tokens-route-handler-request"
|
|
818
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
819
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
820
|
-
*
|
|
821
|
-
* export const GET = async (req: NextRequest) => {
|
|
822
|
-
* const tokens = await getTokens(req);
|
|
823
|
-
*
|
|
824
|
-
* return NextResponse.json({ expired: tokens?.isExpired });
|
|
825
|
-
* };
|
|
826
|
-
* ```
|
|
827
|
-
*
|
|
828
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
829
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
830
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
831
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
832
|
-
*
|
|
833
|
-
* @category Functions
|
|
834
|
-
*/
|
|
835
|
-
declare function getTokens(req: NextRequest$1 | Request, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
836
|
-
/**
|
|
837
|
-
* Retrieves the current user's tokens using an explicit request and response.
|
|
838
|
-
*
|
|
839
|
-
* Use this overload when you have already created a response and want refreshed authentication cookies or headers applied to it.
|
|
840
|
-
*
|
|
841
|
-
* @example Middleware (Response)
|
|
842
|
-
*```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="tokens-route-handler-response"
|
|
843
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
844
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
845
|
-
*
|
|
846
|
-
* export default async function proxy(req: NextRequest) {
|
|
847
|
-
* const res = NextResponse.next();
|
|
848
|
-
*
|
|
849
|
-
* const tokens = await getTokens(req, res);
|
|
850
|
-
*
|
|
851
|
-
* res.headers.set("x-tokens-expired", tokens.isExpired.toString());
|
|
852
|
-
*
|
|
853
|
-
* return res;
|
|
854
|
-
* }
|
|
855
|
-
* ```
|
|
856
|
-
*
|
|
857
|
-
* @example API Handler (Response)
|
|
858
|
-
* ```tsx:src/app/api/tokens/route.ts tab="API Handler (Response)" tab-group="tokens-route-handler-response"
|
|
859
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
860
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
861
|
-
*
|
|
862
|
-
* export const GET = async (req: NextRequest) => {
|
|
863
|
-
* const res = new NextResponse("Custom Body");
|
|
864
|
-
*
|
|
865
|
-
* const tokens = await getTokens(req, res);
|
|
866
|
-
*
|
|
867
|
-
* if (!tokens.isExpired) {
|
|
868
|
-
* res.headers.set("x-auth-status", "active");
|
|
869
|
-
* }
|
|
870
|
-
*
|
|
871
|
-
* return res;
|
|
872
|
-
* };
|
|
873
|
-
* ```
|
|
874
|
-
*
|
|
875
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
876
|
-
* @param res Existing response to update with refreshed authentication cookies or headers.
|
|
877
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
878
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
879
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
880
|
-
*
|
|
881
|
-
* @category Functions
|
|
882
|
-
*/
|
|
883
|
-
declare function getTokens(req: NextRequest$1 | Request, res: NextResponse$1 | Response, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
884
|
-
/**
|
|
885
|
-
* Retrieves the current user's tokens in the Pages Router or Node.js runtime.
|
|
886
|
-
*
|
|
887
|
-
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
888
|
-
*
|
|
889
|
-
* @example Pages Router (Pages)
|
|
890
|
-
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="tokens-pages"
|
|
891
|
-
* import { getTokens, MonoCloudTokens } from "@monocloud/auth-nextjs";
|
|
892
|
-
* import { GetServerSideProps } from "next";
|
|
893
|
-
*
|
|
894
|
-
* type Props = {
|
|
895
|
-
* tokens: MonoCloudTokens;
|
|
896
|
-
* };
|
|
897
|
-
*
|
|
898
|
-
* export default function Home({ tokens }: Props) {
|
|
899
|
-
* return <pre>Tokens: {JSON.stringify(tokens, null, 2)}</pre>;
|
|
900
|
-
* }
|
|
901
|
-
*
|
|
902
|
-
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
903
|
-
* const tokens = await getTokens(ctx.req, ctx.res);
|
|
904
|
-
*
|
|
905
|
-
* return {
|
|
906
|
-
* props: {
|
|
907
|
-
* tokens: tokens
|
|
908
|
-
* }
|
|
909
|
-
* };
|
|
910
|
-
* };
|
|
911
|
-
* ```
|
|
912
|
-
*
|
|
913
|
-
* @example Pages Router (API)
|
|
914
|
-
* ```tsx:src/pages/api/tokens.ts tab="Pages Router (API)" tab-group="tokens-pages"
|
|
915
|
-
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
916
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
917
|
-
*
|
|
918
|
-
* export default async function handler(
|
|
919
|
-
* req: NextApiRequest,
|
|
920
|
-
* res: NextApiResponse
|
|
921
|
-
* ) {
|
|
922
|
-
* const tokens = await getTokens(req, res);
|
|
923
|
-
*
|
|
924
|
-
* res.status(200).json({ accessToken: tokens?.accessToken });
|
|
925
|
-
* }
|
|
926
|
-
* ```
|
|
927
|
-
*
|
|
928
|
-
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
929
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
930
|
-
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
931
|
-
* @returns The current user's tokens, refreshed if necessary.
|
|
932
|
-
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
933
|
-
*
|
|
934
|
-
* @category Functions
|
|
935
|
-
*/
|
|
936
|
-
declare function getTokens(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
937
|
-
/**
|
|
938
|
-
* Checks whether the current user is authenticated using the active server request context.
|
|
939
|
-
*
|
|
940
|
-
* Intended for Server Components, Server Actions, Route Handlers, and Middleware where the request is implicitly available.
|
|
941
|
-
*
|
|
942
|
-
* @example Server Component
|
|
943
|
-
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="is-authenticated-ssr"
|
|
944
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
945
|
-
*
|
|
946
|
-
* export default async function Home() {
|
|
947
|
-
* const authenticated = await isAuthenticated();
|
|
948
|
-
*
|
|
949
|
-
* return <div>Authenticated: {authenticated.toString()}</div>;
|
|
950
|
-
* }
|
|
951
|
-
* ```
|
|
952
|
-
*
|
|
953
|
-
* @example Server Action
|
|
954
|
-
* ```tsx:src/action.ts tab="Server Action" tab-group="is-authenticated-ssr"
|
|
955
|
-
* "use server";
|
|
956
|
-
*
|
|
957
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
958
|
-
*
|
|
959
|
-
* export async function checkAuthAction() {
|
|
960
|
-
* const authenticated = await isAuthenticated();
|
|
961
|
-
*
|
|
962
|
-
* return { authenticated };
|
|
963
|
-
* }
|
|
964
|
-
* ```
|
|
965
|
-
*
|
|
966
|
-
* @example API Handler
|
|
967
|
-
* ```tsx:src/app/api/authenticated/route.ts tab="API Handler" tab-group="is-authenticated-ssr"
|
|
968
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
969
|
-
* import { NextResponse } from "next/server";
|
|
970
|
-
*
|
|
971
|
-
* export const GET = async () => {
|
|
972
|
-
* const authenticated = await isAuthenticated();
|
|
973
|
-
*
|
|
974
|
-
* return NextResponse.json({ authenticated });
|
|
975
|
-
* };
|
|
976
|
-
* ```
|
|
977
|
-
*
|
|
978
|
-
* @example Middleware
|
|
979
|
-
* ```tsx:src/proxy.ts tab="Middleware" tab-group="is-authenticated-ssr"
|
|
980
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
981
|
-
* import { NextResponse } from "next/server";
|
|
982
|
-
*
|
|
983
|
-
* export default async function proxy() {
|
|
984
|
-
* const authenticated = await isAuthenticated();
|
|
985
|
-
*
|
|
986
|
-
* if (!authenticated) {
|
|
987
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
988
|
-
* }
|
|
989
|
-
*
|
|
990
|
-
* return NextResponse.next();
|
|
991
|
-
* }
|
|
992
|
-
* ```
|
|
993
|
-
*
|
|
994
|
-
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
995
|
-
*
|
|
996
|
-
* @category Functions
|
|
997
|
-
*/
|
|
998
|
-
declare function isAuthenticated(): Promise<boolean>;
|
|
999
|
-
/**
|
|
1000
|
-
* Checks whether the current user is authenticated using an explicit Web or Next.js request.
|
|
1001
|
-
*
|
|
1002
|
-
* Use this overload when you already have access to a `Request` or `NextRequest` (for example, in Middleware or Route Handlers).
|
|
1003
|
-
*
|
|
1004
|
-
* @example Middleware (Request)
|
|
1005
|
-
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="is-authenticated-route-handler"
|
|
1006
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1007
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1008
|
-
*
|
|
1009
|
-
* export default async function proxy(req: NextRequest) {
|
|
1010
|
-
* const authenticated = await isAuthenticated(req);
|
|
1011
|
-
*
|
|
1012
|
-
* if (!authenticated) {
|
|
1013
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
1014
|
-
* }
|
|
1015
|
-
*
|
|
1016
|
-
* return NextResponse.next();
|
|
1017
|
-
* }
|
|
1018
|
-
* ```
|
|
1019
|
-
*
|
|
1020
|
-
* @example Middleware (Response)
|
|
1021
|
-
* ```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="is-authenticated-route-handler"
|
|
1022
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1023
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1024
|
-
*
|
|
1025
|
-
* export default async function proxy(req: NextRequest) {
|
|
1026
|
-
* const res = NextResponse.next();
|
|
1027
|
-
*
|
|
1028
|
-
* const authenticated = await isAuthenticated(req, res);
|
|
1029
|
-
*
|
|
1030
|
-
* if (!authenticated) {
|
|
1031
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
1032
|
-
* }
|
|
1033
|
-
*
|
|
1034
|
-
* res.headers.set("x-authenticated", "true");
|
|
1035
|
-
*
|
|
1036
|
-
* return res;
|
|
1037
|
-
* }
|
|
1038
|
-
* ```
|
|
1039
|
-
*
|
|
1040
|
-
* @example API Handler (Request)
|
|
1041
|
-
* ```tsx:src/app/api/authenticated/route.ts tab="API Handler (Request)" tab-group="is-authenticated-route-handler"
|
|
1042
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1043
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1044
|
-
*
|
|
1045
|
-
* export const GET = async (req: NextRequest) => {
|
|
1046
|
-
* const authenticated = await isAuthenticated(req);
|
|
1047
|
-
*
|
|
1048
|
-
* return NextResponse.json({ authenticated });
|
|
1049
|
-
* };
|
|
1050
|
-
* ```
|
|
1051
|
-
*
|
|
1052
|
-
* @example API Handler (Response)
|
|
1053
|
-
* ```tsx:src/app/api/authenticated/route.ts tab="API Handler (Response)" tab-group="is-authenticated-route-handler"
|
|
1054
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1055
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1056
|
-
*
|
|
1057
|
-
* export const GET = async (req: NextRequest) => {
|
|
1058
|
-
* const res = new NextResponse("YOUR CUSTOM RESPONSE");
|
|
1059
|
-
*
|
|
1060
|
-
* const authenticated = await isAuthenticated(req, res);
|
|
1061
|
-
*
|
|
1062
|
-
* if (authenticated) {
|
|
1063
|
-
* res.cookies.set("something", "important");
|
|
1064
|
-
* }
|
|
1065
|
-
*
|
|
1066
|
-
* return res;
|
|
1067
|
-
* };
|
|
1068
|
-
* ```
|
|
1069
|
-
*
|
|
1070
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
1071
|
-
* @param res Optional response to update if refreshed authentication cookies or headers are required.
|
|
1072
|
-
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
1073
|
-
*
|
|
1074
|
-
* @category Functions
|
|
1075
|
-
*/
|
|
1076
|
-
declare function isAuthenticated(req: NextRequest$1 | Request, res?: NextResponse$1 | Response): Promise<boolean>;
|
|
1077
|
-
/**
|
|
1078
|
-
* Checks whether the current user is authenticated in the Pages Router or Node.js runtime.
|
|
1079
|
-
*
|
|
1080
|
-
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
1081
|
-
*
|
|
1082
|
-
* @example Pages Router (Pages)
|
|
1083
|
-
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="is-authenticated-pages"
|
|
1084
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1085
|
-
* import { GetServerSideProps } from "next";
|
|
1086
|
-
*
|
|
1087
|
-
* type Props = {
|
|
1088
|
-
* authenticated: boolean;
|
|
1089
|
-
* };
|
|
1090
|
-
*
|
|
1091
|
-
* export default function Home({ authenticated }: Props) {
|
|
1092
|
-
* return <pre>User is {authenticated ? "logged in" : "guest"}</pre>;
|
|
1093
|
-
* }
|
|
1094
|
-
*
|
|
1095
|
-
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
1096
|
-
* const authenticated = await isAuthenticated(ctx.req, ctx.res);
|
|
1097
|
-
*
|
|
1098
|
-
* return {
|
|
1099
|
-
* props: {
|
|
1100
|
-
* authenticated
|
|
1101
|
-
* }
|
|
1102
|
-
* };
|
|
1103
|
-
* };
|
|
1104
|
-
* ```
|
|
1105
|
-
*
|
|
1106
|
-
* @example Pages Router (API)
|
|
1107
|
-
* ```tsx:src/pages/api/authenticated.ts tab="Pages Router (API)" tab-group="is-authenticated-pages"
|
|
1108
|
-
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1109
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
1110
|
-
*
|
|
1111
|
-
* export default async function handler(
|
|
1112
|
-
* req: NextApiRequest,
|
|
1113
|
-
* res: NextApiResponse
|
|
1114
|
-
* ) {
|
|
1115
|
-
* const authenticated = await isAuthenticated(req, res);
|
|
1116
|
-
*
|
|
1117
|
-
* res.status(200).json({ authenticated });
|
|
1118
|
-
* }
|
|
1119
|
-
* ```
|
|
1120
|
-
*
|
|
1121
|
-
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
1122
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
1123
|
-
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
1124
|
-
*
|
|
1125
|
-
* @category Functions
|
|
1126
|
-
*/
|
|
1127
|
-
declare function isAuthenticated(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>): Promise<boolean>;
|
|
1128
|
-
/**
|
|
1129
|
-
* Ensures the current user is authenticated. If not, redirects to the sign-in flow.
|
|
1130
|
-
*
|
|
1131
|
-
* > **App Router only.** Intended for Server Components, Route Handlers, and Server Actions.
|
|
1132
|
-
*
|
|
1133
|
-
* @example Server Component
|
|
1134
|
-
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="protect"
|
|
1135
|
-
* import { protect } from "@monocloud/auth-nextjs";
|
|
1136
|
-
*
|
|
1137
|
-
* export default async function Home() {
|
|
1138
|
-
* await protect();
|
|
1139
|
-
*
|
|
1140
|
-
* return <>You are signed in.</>;
|
|
1141
|
-
* }
|
|
1142
|
-
* ```
|
|
1143
|
-
*
|
|
1144
|
-
* @example Server Action
|
|
1145
|
-
* ```tsx:src/action.ts tab="Server Action" tab-group="protect"
|
|
1146
|
-
* "use server";
|
|
1147
|
-
*
|
|
1148
|
-
* import { protect } from "@monocloud/auth-nextjs";
|
|
1149
|
-
*
|
|
1150
|
-
* export async function getMessage() {
|
|
1151
|
-
* await protect();
|
|
1152
|
-
*
|
|
1153
|
-
* return { secret: "sssshhhhh!!!" };
|
|
1154
|
-
* }
|
|
1155
|
-
* ```
|
|
1156
|
-
*
|
|
1157
|
-
* @example API Handler
|
|
1158
|
-
* ```tsx:src/app/api/protected/route.ts tab="API Handler" tab-group="protect"
|
|
1159
|
-
* import { protect } from "@monocloud/auth-nextjs";
|
|
1160
|
-
* import { NextResponse } from "next/server";
|
|
1161
|
-
*
|
|
1162
|
-
* export const GET = async () => {
|
|
1163
|
-
* await protect();
|
|
1164
|
-
*
|
|
1165
|
-
* return NextResponse.json({ secret: "ssshhhh!!!" });
|
|
1166
|
-
* };
|
|
1167
|
-
* ```
|
|
1168
|
-
*
|
|
1169
|
-
* @param options Optional configuration for redirect behavior (for example, return URL or sign-in parameters).
|
|
1170
|
-
* @returns Resolves if the user is authenticated; otherwise triggers a redirect.
|
|
1171
|
-
*
|
|
1172
|
-
* @category Functions
|
|
1173
|
-
*/
|
|
1174
|
-
declare function protect(options?: ProtectOptions): Promise<void>;
|
|
1175
|
-
/**
|
|
1176
|
-
* Wraps an App Router API route handler and ensures that only authenticated (and optionally authorized) requests can access the route.
|
|
1177
|
-
*
|
|
1178
|
-
* Intended for Next.js App Router Route Handlers.
|
|
1179
|
-
*
|
|
1180
|
-
* @example
|
|
1181
|
-
* ```tsx:src/app/api/protected/route.ts
|
|
1182
|
-
* import { protectApi } from "@monocloud/auth-nextjs";
|
|
1183
|
-
* import { NextResponse } from "next/server";
|
|
1184
|
-
*
|
|
1185
|
-
* export const GET = protectApi(async () => {
|
|
1186
|
-
* return NextResponse.json({
|
|
1187
|
-
* message: "You accessed a protected endpoint",
|
|
1188
|
-
* });
|
|
1189
|
-
* });
|
|
1190
|
-
* ```
|
|
1191
|
-
*
|
|
1192
|
-
* @param handler The route handler to protect.
|
|
1193
|
-
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
1194
|
-
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
1195
|
-
*
|
|
1196
|
-
* @category Functions
|
|
1197
|
-
*/
|
|
1198
|
-
declare function protectApi(handler: AppRouterApiHandlerFn, options?: ProtectApiAppOptions): AppRouterApiHandlerFn;
|
|
1199
|
-
/**
|
|
1200
|
-
* Wraps a Pages Router API route handler and ensures that only authenticated (and optionally authorized) requests can access the route.
|
|
1201
|
-
*
|
|
1202
|
-
* Intended for Next.js Pages Router API routes.
|
|
1203
|
-
*
|
|
1204
|
-
* @example
|
|
1205
|
-
* ```tsx:src/pages/api/protected.ts
|
|
1206
|
-
* import { protectApi } from "@monocloud/auth-nextjs";
|
|
1207
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
1208
|
-
*
|
|
1209
|
-
* export default protectApi(
|
|
1210
|
-
* async (req: NextApiRequest, res: NextApiResponse) => {
|
|
1211
|
-
* return res.json({
|
|
1212
|
-
* message: "You accessed a protected endpoint",
|
|
1213
|
-
* });
|
|
1214
|
-
* }
|
|
1215
|
-
* );
|
|
1216
|
-
* ```
|
|
1217
|
-
*
|
|
1218
|
-
* @param handler - The route handler to protect.
|
|
1219
|
-
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
1220
|
-
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
1221
|
-
*
|
|
1222
|
-
* @category Functions
|
|
1223
|
-
*/
|
|
1224
|
-
declare function protectApi(handler: NextApiHandler$1, options?: ProtectApiPageOptions): NextApiHandler$1;
|
|
1225
|
-
/**
|
|
1226
|
-
* Restricts access to App Router server-rendered pages.
|
|
1227
|
-
*
|
|
1228
|
-
* **Access control**
|
|
1229
|
-
* - If the user is not authenticated, `onAccessDenied` is invoked (or default behavior applies).
|
|
1230
|
-
* - If the user is authenticated but fails group checks, `onGroupAccessDenied` is invoked (or the default "Access Denied" view is rendered).
|
|
1231
|
-
*
|
|
1232
|
-
* Both behaviors can be customized via options.
|
|
1233
|
-
*
|
|
1234
|
-
* @example Basic Usage
|
|
1235
|
-
* ```tsx:src/app/page.tsx tab="Basic Usage" tab-group="protectPage-app"
|
|
1236
|
-
* import { protectPage } from "@monocloud/auth-nextjs";
|
|
1237
|
-
*
|
|
1238
|
-
* export default protectPage(async function Home({ user }) {
|
|
1239
|
-
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
1240
|
-
* });
|
|
1241
|
-
* ```
|
|
1242
|
-
*
|
|
1243
|
-
* @example With Options
|
|
1244
|
-
* ```tsx:src/app/page.tsx tab="With Options" tab-group="protectPage-app"
|
|
1245
|
-
* import { protectPage } from "@monocloud/auth-nextjs";
|
|
1246
|
-
*
|
|
1247
|
-
* export default protectPage(
|
|
1248
|
-
* async function Home({ user }) {
|
|
1249
|
-
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
1250
|
-
* },
|
|
1251
|
-
* {
|
|
1252
|
-
* returnUrl: "/dashboard",
|
|
1253
|
-
* groups: ["admin"],
|
|
1254
|
-
* }
|
|
1255
|
-
* );
|
|
1256
|
-
* ```
|
|
1257
|
-
*
|
|
1258
|
-
* @param component The App Router server component to protect.
|
|
1259
|
-
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
1260
|
-
* @returns A wrapped page component that enforces authentication before rendering.
|
|
1261
|
-
*
|
|
1262
|
-
* @category Functions
|
|
1263
|
-
*/
|
|
1264
|
-
declare function protectPage(component: ProtectedAppServerComponent, options?: ProtectAppPageOptions): AppRouterPageHandler;
|
|
1265
|
-
/**
|
|
1266
|
-
* Restricts access to Pages Router server-rendered pages using `getServerSideProps`.
|
|
1267
|
-
*
|
|
1268
|
-
* **Access control**
|
|
1269
|
-
* - If the user is not authenticated, `onAccessDenied` is invoked (or default behavior applies).
|
|
1270
|
-
* - If the user is authenticated but fails group checks, the page can still render and `groupAccessDenied` is provided in props. Use `onGroupAccessDenied` to customize the props or behavior.
|
|
1271
|
-
*
|
|
1272
|
-
* Both behaviors can be customized via options.
|
|
1273
|
-
*
|
|
1274
|
-
* @example Basic Usage
|
|
1275
|
-
* ```tsx:src/pages/index.tsx tab="Basic Usage" tab-group="protectPage-page"
|
|
1276
|
-
* import { protectPage, MonoCloudUser } from "@monocloud/auth-nextjs";
|
|
1277
|
-
*
|
|
1278
|
-
* type Props = {
|
|
1279
|
-
* user: MonoCloudUser;
|
|
1280
|
-
* };
|
|
1281
|
-
*
|
|
1282
|
-
* export default function Home({ user }: Props) {
|
|
1283
|
-
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
1284
|
-
* }
|
|
1285
|
-
*
|
|
1286
|
-
* export const getServerSideProps = protectPage();
|
|
1287
|
-
* ```
|
|
1288
|
-
*
|
|
1289
|
-
* @example With Options
|
|
1290
|
-
* ```tsx:src/pages/index.tsx tab="With Options" tab-group="protectPage-page"
|
|
1291
|
-
* import { protectPage, MonoCloudUser } from "@monocloud/auth-nextjs";
|
|
1292
|
-
* import { GetServerSidePropsContext } from "next";
|
|
1293
|
-
*
|
|
1294
|
-
* type Props = {
|
|
1295
|
-
* user: MonoCloudUser;
|
|
1296
|
-
* url: string;
|
|
1297
|
-
* };
|
|
1298
|
-
*
|
|
1299
|
-
* export default function Home({ user, url }: Props) {
|
|
1300
|
-
* console.log(url);
|
|
1301
|
-
* return <div>Hi {user?.email}. You accessed a protected page.</div>;
|
|
1302
|
-
* }
|
|
1303
|
-
*
|
|
1304
|
-
* export const getServerSideProps = protectPage({
|
|
1305
|
-
* returnUrl: "/dashboard",
|
|
1306
|
-
* groups: ["admin"],
|
|
1307
|
-
* getServerSideProps: async (context: GetServerSidePropsContext) => ({
|
|
1308
|
-
* props: { url: context.resolvedUrl }
|
|
1309
|
-
* })
|
|
1310
|
-
* });
|
|
1311
|
-
* ```
|
|
1312
|
-
*
|
|
1313
|
-
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
1314
|
-
* @typeParam P - Props returned from `getServerSideProps`.
|
|
1315
|
-
* @typeParam Q - Query parameters parsed from the URL.
|
|
1316
|
-
* @returns A getServerSideProps wrapper that enforces authentication before executing the page logic.
|
|
1317
|
-
*
|
|
1318
|
-
* @category Functions
|
|
1319
|
-
*/
|
|
1320
|
-
declare function protectPage<P extends Record<string, any> = Record<string, any>, Q extends ParsedUrlQuery = ParsedUrlQuery>(options?: ProtectPagePageOptions<P, Q>): ProtectPagePageReturnType<P, Q>;
|
|
1321
|
-
/**
|
|
1322
|
-
* Checks whether the currently authenticated user is a member of **any** of the specified groups.
|
|
1323
|
-
*
|
|
1324
|
-
* The `groups` parameter accepts group identifiers (IDs or names).
|
|
1325
|
-
*
|
|
1326
|
-
* The authenticated user's session may contain groups represented as:
|
|
1327
|
-
* - Group IDs
|
|
1328
|
-
* - Group names
|
|
1329
|
-
* - `Group` objects (for example, `{ id: string; name: string }`)
|
|
1330
|
-
*
|
|
1331
|
-
* Matching is always performed against the group's **ID** and **name**, regardless of how the group is represented in the session.
|
|
1332
|
-
*
|
|
1333
|
-
* @example Server Component
|
|
1334
|
-
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="is-user-in-group-rsc"
|
|
1335
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1336
|
-
*
|
|
1337
|
-
* export default async function AdminPanel() {
|
|
1338
|
-
* const isAdmin = await isUserInGroup(["admin"]);
|
|
1339
|
-
*
|
|
1340
|
-
* if (!isAdmin) {
|
|
1341
|
-
* return <div>Access Denied</div>;
|
|
1342
|
-
* }
|
|
1343
|
-
*
|
|
1344
|
-
* return <div>Admin Control Panel</div>;
|
|
1345
|
-
* }
|
|
1346
|
-
* ```
|
|
1347
|
-
*
|
|
1348
|
-
* @example Server Action
|
|
1349
|
-
* ```tsx:src/action.ts tab="Server Action" tab-group="is-user-in-group-rsc"
|
|
1350
|
-
* "use server";
|
|
1351
|
-
*
|
|
1352
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1353
|
-
*
|
|
1354
|
-
* export async function deletePostAction() {
|
|
1355
|
-
* const canDelete = await isUserInGroup(["admin", "editor"]);
|
|
1356
|
-
*
|
|
1357
|
-
* if (!canDelete) {
|
|
1358
|
-
* return { success: false };
|
|
1359
|
-
* }
|
|
1360
|
-
*
|
|
1361
|
-
* return { success: true };
|
|
1362
|
-
* }
|
|
1363
|
-
* ```
|
|
1364
|
-
*
|
|
1365
|
-
* @example API Handler
|
|
1366
|
-
* ```tsx:src/app/api/group-check/route.ts tab="API Handler" tab-group="is-user-in-group-rsc"
|
|
1367
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1368
|
-
* import { NextResponse } from "next/server";
|
|
1369
|
-
*
|
|
1370
|
-
* export const GET = async () => {
|
|
1371
|
-
* const allowed = await isUserInGroup(["admin", "editor"]);
|
|
1372
|
-
*
|
|
1373
|
-
* if (!allowed) {
|
|
1374
|
-
* return new NextResponse("Forbidden", { status: 403 });
|
|
1375
|
-
* }
|
|
1376
|
-
*
|
|
1377
|
-
* return NextResponse.json({ status: "success" });
|
|
1378
|
-
* };
|
|
1379
|
-
* ```
|
|
1380
|
-
*
|
|
1381
|
-
* @example Middleware
|
|
1382
|
-
* ```tsx:src/proxy.ts tab="Middleware" tab-group="is-user-in-group-rsc"
|
|
1383
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1384
|
-
* import { NextResponse } from "next/server";
|
|
1385
|
-
*
|
|
1386
|
-
* export default async function proxy() {
|
|
1387
|
-
* const isAdmin = await isUserInGroup(["admin"]);
|
|
1388
|
-
*
|
|
1389
|
-
* if (!isAdmin) {
|
|
1390
|
-
* return new NextResponse("User is not admin", { status: 403 });
|
|
1391
|
-
* }
|
|
1392
|
-
*
|
|
1393
|
-
* return NextResponse.next();
|
|
1394
|
-
* }
|
|
1395
|
-
* ```
|
|
1396
|
-
*
|
|
1397
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1398
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1399
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1400
|
-
*
|
|
1401
|
-
* @category Functions
|
|
1402
|
-
*/
|
|
1403
|
-
declare function isUserInGroup(groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1404
|
-
/**
|
|
1405
|
-
* Checks group membership using an explicit Web or Next.js request.
|
|
1406
|
-
*
|
|
1407
|
-
* Use this overload when you already have access to a `Request` or `NextRequest` (for example, in Middleware or Route Handlers).
|
|
1408
|
-
*
|
|
1409
|
-
* @example Middleware (Request)
|
|
1410
|
-
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="is-user-in-group-request"
|
|
1411
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1412
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1413
|
-
*
|
|
1414
|
-
* export default async function proxy(req: NextRequest) {
|
|
1415
|
-
* const isAdmin = await isUserInGroup(req, ["admin"]);
|
|
1416
|
-
*
|
|
1417
|
-
* if (!isAdmin) {
|
|
1418
|
-
* return new NextResponse("User is not admin", { status: 403 });
|
|
1419
|
-
* }
|
|
1420
|
-
*
|
|
1421
|
-
* return NextResponse.next();
|
|
1422
|
-
* }
|
|
1423
|
-
* ```
|
|
1424
|
-
*
|
|
1425
|
-
* @example API Handler (Request)
|
|
1426
|
-
* ```tsx:src/app/api/group-check/route.ts tab="API Handler (Request)" tab-group="is-user-in-group-request"
|
|
1427
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1428
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1429
|
-
*
|
|
1430
|
-
* export const GET = async (req: NextRequest) => {
|
|
1431
|
-
* const isMember = await isUserInGroup(req, ["admin", "editor"]);
|
|
1432
|
-
*
|
|
1433
|
-
* return NextResponse.json({ isMember });
|
|
1434
|
-
* };
|
|
1435
|
-
* ```
|
|
1436
|
-
*
|
|
1437
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
1438
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1439
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1440
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1441
|
-
*
|
|
1442
|
-
* @category Functions
|
|
1443
|
-
*/
|
|
1444
|
-
declare function isUserInGroup(req: NextRequest$1 | Request, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1445
|
-
/**
|
|
1446
|
-
* Checks group membership using an explicit request and response.
|
|
1447
|
-
*
|
|
1448
|
-
* Use this overload when you have already created a response and want refreshed authentication cookies or headers applied to it.
|
|
1449
|
-
*
|
|
1450
|
-
* @example Middleware (Response)
|
|
1451
|
-
* ```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="is-user-in-group-response"
|
|
1452
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1453
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1454
|
-
*
|
|
1455
|
-
* export default async function proxy(req: NextRequest) {
|
|
1456
|
-
* const res = NextResponse.next();
|
|
1457
|
-
*
|
|
1458
|
-
* const isAdmin = await isUserInGroup(req, res, ["admin"]);
|
|
1459
|
-
*
|
|
1460
|
-
* if (!isAdmin) {
|
|
1461
|
-
* return new NextResponse("User is not admin", { status: 403 });
|
|
1462
|
-
* }
|
|
1463
|
-
*
|
|
1464
|
-
* res.headers.set("x-user", "admin");
|
|
1465
|
-
*
|
|
1466
|
-
* return res;
|
|
1467
|
-
* }
|
|
1468
|
-
* ```
|
|
1469
|
-
*
|
|
1470
|
-
* @example API Handler (Response)
|
|
1471
|
-
* ```tsx:src/app/api/group-check/route.ts tab="API Handler (Response)" tab-group="is-user-in-group-response"
|
|
1472
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1473
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1474
|
-
*
|
|
1475
|
-
* export const GET = async (req: NextRequest) => {
|
|
1476
|
-
* const res = new NextResponse("Restricted Content");
|
|
1477
|
-
*
|
|
1478
|
-
* const allowed = await isUserInGroup(req, res, ["admin"]);
|
|
1479
|
-
*
|
|
1480
|
-
* if (!allowed) {
|
|
1481
|
-
* return new NextResponse("Not Allowed", res);
|
|
1482
|
-
* }
|
|
1483
|
-
*
|
|
1484
|
-
* return res;
|
|
1485
|
-
* };
|
|
1486
|
-
* ```
|
|
1487
|
-
*
|
|
1488
|
-
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
1489
|
-
* @param res Existing response to update with refreshed authentication cookies or headers when required.
|
|
1490
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1491
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1492
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1493
|
-
*
|
|
1494
|
-
* @category Functions
|
|
1495
|
-
*/
|
|
1496
|
-
declare function isUserInGroup(req: NextRequest$1 | Request, res: NextResponse$1 | Response, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1497
|
-
/**
|
|
1498
|
-
* Checks group membership in the Pages Router or Node.js runtime.
|
|
1499
|
-
*
|
|
1500
|
-
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
1501
|
-
*
|
|
1502
|
-
* @example Pages Router (Pages)
|
|
1503
|
-
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="is-user-in-group-pages"
|
|
1504
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1505
|
-
* import { GetServerSideProps } from "next";
|
|
1506
|
-
*
|
|
1507
|
-
* type Props = {
|
|
1508
|
-
* isAdmin: boolean;
|
|
1509
|
-
* };
|
|
1510
|
-
*
|
|
1511
|
-
* export default function Home({ isAdmin }: Props) {
|
|
1512
|
-
* return <div>User is admin: {isAdmin.toString()}</div>;
|
|
1513
|
-
* }
|
|
1514
|
-
*
|
|
1515
|
-
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
1516
|
-
* const isAdmin = await isUserInGroup(ctx.req, ctx.res, ["admin"]);
|
|
1517
|
-
*
|
|
1518
|
-
* return {
|
|
1519
|
-
* props: {
|
|
1520
|
-
* isAdmin
|
|
1521
|
-
* }
|
|
1522
|
-
* };
|
|
1523
|
-
* };
|
|
1524
|
-
* ```
|
|
1525
|
-
*
|
|
1526
|
-
* @example Pages Router (API)
|
|
1527
|
-
* ```tsx:src/pages/api/group-check.ts tab="Pages Router (API)" tab-group="is-user-in-group-pages"
|
|
1528
|
-
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1529
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
1530
|
-
*
|
|
1531
|
-
* export default async function handler(
|
|
1532
|
-
* req: NextApiRequest,
|
|
1533
|
-
* res: NextApiResponse
|
|
1534
|
-
* ) {
|
|
1535
|
-
* const isAdmin = await isUserInGroup(req, res, ["admin"]);
|
|
1536
|
-
*
|
|
1537
|
-
* if (!isAdmin) {
|
|
1538
|
-
* return res.status(403).json({ error: "Forbidden" });
|
|
1539
|
-
* }
|
|
1540
|
-
*
|
|
1541
|
-
* res.status(200).json({ message: "Welcome Admin" });
|
|
1542
|
-
* }
|
|
1543
|
-
* ```
|
|
1544
|
-
*
|
|
1545
|
-
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
1546
|
-
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
1547
|
-
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1548
|
-
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1549
|
-
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1550
|
-
*
|
|
1551
|
-
* @category Functions
|
|
1552
|
-
*/
|
|
1553
|
-
declare function isUserInGroup(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1554
|
-
/**
|
|
1555
|
-
* Redirects the user to the sign-in flow.
|
|
1556
|
-
*
|
|
1557
|
-
* > **App Router only**. Intended for use in Server Components, Route Handlers, and Server Actions.
|
|
1558
|
-
*
|
|
1559
|
-
* This helper performs a server-side redirect to the configured sign-in route. Execution does not continue after the redirect is triggered.
|
|
1560
|
-
*
|
|
1561
|
-
* @example Server Component
|
|
1562
|
-
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="redirect-to-sign-in"
|
|
1563
|
-
* import { isUserInGroup, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1564
|
-
*
|
|
1565
|
-
* export default async function Home() {
|
|
1566
|
-
* const allowed = await isUserInGroup(["admin"]);
|
|
1567
|
-
*
|
|
1568
|
-
* if (!allowed) {
|
|
1569
|
-
* await redirectToSignIn({ returnUrl: "/home" });
|
|
1570
|
-
* }
|
|
1571
|
-
*
|
|
1572
|
-
* return <>You are signed in.</>;
|
|
1573
|
-
* }
|
|
1574
|
-
* ```
|
|
1575
|
-
*
|
|
1576
|
-
* @example Server Action
|
|
1577
|
-
* ```tsx:src/action.ts tab="Server Action" tab-group="redirect-to-sign-in"
|
|
1578
|
-
* "use server";
|
|
1579
|
-
*
|
|
1580
|
-
* import { getSession, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1581
|
-
*
|
|
1582
|
-
* export async function protectedAction() {
|
|
1583
|
-
* const session = await getSession();
|
|
1584
|
-
*
|
|
1585
|
-
* if (!session) {
|
|
1586
|
-
* await redirectToSignIn();
|
|
1587
|
-
* }
|
|
1588
|
-
*
|
|
1589
|
-
* return { data: "Sensitive Data" };
|
|
1590
|
-
* }
|
|
1591
|
-
* ```
|
|
1592
|
-
*
|
|
1593
|
-
* @example API Handler
|
|
1594
|
-
* ```tsx:src/app/api/protected/route.ts tab="API Handler" tab-group="redirect-to-sign-in"
|
|
1595
|
-
* import { getSession, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1596
|
-
* import { NextResponse } from "next/server";
|
|
1597
|
-
*
|
|
1598
|
-
* export const GET = async () => {
|
|
1599
|
-
* const session = await getSession();
|
|
1600
|
-
*
|
|
1601
|
-
* if (!session) {
|
|
1602
|
-
* await redirectToSignIn({
|
|
1603
|
-
* returnUrl: "/dashboard",
|
|
1604
|
-
* });
|
|
1605
|
-
* }
|
|
1606
|
-
*
|
|
1607
|
-
* return NextResponse.json({ data: "Protected content" });
|
|
1608
|
-
* };
|
|
1609
|
-
* ```
|
|
1610
|
-
*
|
|
1611
|
-
* @param options Optional configuration for the redirect, such as `returnUrl` or additional sign-in parameters.
|
|
1612
|
-
* @returns Never resolves. Triggers a redirect to the sign-in flow.
|
|
1613
|
-
*
|
|
1614
|
-
* @category Functions
|
|
1615
|
-
*/
|
|
1616
|
-
declare function redirectToSignIn(options?: RedirectToSignInOptions): Promise<void>;
|
|
1617
|
-
/**
|
|
1618
|
-
* Redirects the user to the sign-out flow.
|
|
1619
|
-
*
|
|
1620
|
-
* > **App Router only**. Intended for use in Server Components, Route Handlers, and Server Actions.
|
|
1621
|
-
*
|
|
1622
|
-
* This helper performs a server-side redirect to the configured sign-out route. Execution does not continue after the redirect is triggered.
|
|
1623
|
-
*
|
|
1624
|
-
* @example Server Component
|
|
1625
|
-
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="redirect-to-sign-out"
|
|
1626
|
-
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1627
|
-
*
|
|
1628
|
-
* export default async function Page() {
|
|
1629
|
-
* const session = await getSession();
|
|
1630
|
-
*
|
|
1631
|
-
* // Example: Force sign-out if a specific condition is met (e.g., account suspended)
|
|
1632
|
-
* if (session?.user.isSuspended) {
|
|
1633
|
-
* await redirectToSignOut();
|
|
1634
|
-
* }
|
|
1635
|
-
*
|
|
1636
|
-
* return <>Welcome User</>;
|
|
1637
|
-
* }
|
|
1638
|
-
* ```
|
|
1639
|
-
*
|
|
1640
|
-
* @example Server Action
|
|
1641
|
-
* ```tsx:src/action.ts tab="Server Action" tab-group="redirect-to-sign-out"
|
|
1642
|
-
* "use server";
|
|
1643
|
-
*
|
|
1644
|
-
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1645
|
-
*
|
|
1646
|
-
* export async function signOutAction() {
|
|
1647
|
-
* const session = await getSession();
|
|
1648
|
-
*
|
|
1649
|
-
* if (session) {
|
|
1650
|
-
* await redirectToSignOut();
|
|
1651
|
-
* }
|
|
1652
|
-
* }
|
|
1653
|
-
* ```
|
|
1654
|
-
*
|
|
1655
|
-
* @example API Handler
|
|
1656
|
-
* ```tsx:src/app/api/signout/route.ts tab="API Handler" tab-group="redirect-to-sign-out"
|
|
1657
|
-
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1658
|
-
* import { NextResponse } from "next/server";
|
|
1659
|
-
*
|
|
1660
|
-
* export const GET = async () => {
|
|
1661
|
-
* const session = await getSession();
|
|
1662
|
-
*
|
|
1663
|
-
* if (session) {
|
|
1664
|
-
* await redirectToSignOut({
|
|
1665
|
-
* postLogoutRedirectUri: "/goodbye",
|
|
1666
|
-
* });
|
|
1667
|
-
* }
|
|
1668
|
-
*
|
|
1669
|
-
* return NextResponse.json({ status: "already_signed_out" });
|
|
1670
|
-
* };
|
|
1671
|
-
* ```
|
|
1672
|
-
*
|
|
1673
|
-
* @param options Optional configuration for the redirect, such as `postLogoutRedirectUri` or additional sign-out parameters.
|
|
1674
|
-
* @returns Never resolves. Triggers a redirect to the sign-out flow.
|
|
1675
|
-
*
|
|
1676
|
-
* @category Functions
|
|
1677
|
-
*/
|
|
1678
|
-
declare function redirectToSignOut(options?: RedirectToSignOutOptions): Promise<void>;
|
|
1679
|
-
//#endregion
|
|
1680
|
-
export { type AccessToken, type Address, type AppOnError, type AppRouterApiHandlerFn, type AppRouterApiOnAccessDeniedHandler, type AppRouterApiOnGroupAccessDeniedHandler, type AppRouterContext, type AppRouterPageHandler, type ApplicationState, type Authenticators, type AuthorizationParams, type CodeChallengeMethod, type CustomProtectedRouteMatcher, type DisplayOptions, type ExtraAuthParams, type GetTokensOptions, type Group, type GroupOptions, type IdTokenClaims, type Indicator, type IsUserInGroupOptions, type Jwk, MonoCloudAuthBaseError, type MonoCloudAuthHandler, type MonoCloudAuthOptions, type MonoCloudCookieOptions, MonoCloudHttpError, type MonoCloudMiddlewareOptions, MonoCloudNextClient, MonoCloudOPError, type MonoCloudOptions, type MonoCloudRequest, type MonoCloudRoutes, type MonoCloudSession, type MonoCloudSessionOptions, type MonoCloudSessionOptionsBase, type MonoCloudSessionStore, type MonoCloudStateOptions, type MonoCloudStatePartialOptions, MonoCloudTokenError, type MonoCloudTokens, type MonoCloudUser, MonoCloudValidationError, type NextMiddlewareOnAccessDenied, type NextMiddlewareOnGroupAccessDenied, type NextMiddlewareResult, type OnBackChannelLogout, type OnError, type OnSessionCreating, type OnSetApplicationState, type PageOnError, type PageRouterApiOnAccessDeniedHandler, type PageRouterApiOnGroupAccessDeniedHandler, type Prompt, type ProtectApiAppOptions, type ProtectApiPageOptions, type ProtectAppPageOptions, type ProtectOptions, type ProtectPageGetServerSidePropsContext, type ProtectPagePageOnAccessDeniedType, type ProtectPagePageOnGroupAccessDeniedType, type ProtectPagePageOptions, type ProtectPagePageReturnType, type ProtectedAppServerComponent, type ProtectedAppServerComponentProps, type ProtectedRouteMatcher, type ProtectedRoutes, type RedirectToSignInOptions, type RedirectToSignOutOptions, type ResponseModes, type ResponseTypes, type SameSiteValues, type SecurityAlgorithms, type SessionLifetime, type UserinfoResponse, authMiddleware, getSession, getTokens, isAuthenticated, isUserInGroup, monoCloudAuth, protect, protectApi, protectPage, redirectToSignIn, redirectToSignOut };
|
|
1681
|
-
//# sourceMappingURL=index.d.mts.map
|
|
1
|
+
import { AppOnError, AppRouterApiHandlerFn, AppRouterApiOnAccessDeniedHandler, AppRouterApiOnGroupAccessDeniedHandler, AppRouterContext, AppRouterPageHandler, CustomProtectedRouteMatcher, ExtraAuthParams, GroupOptions, IsUserInGroupOptions, MonoCloudAuthHandler, MonoCloudAuthOptions, MonoCloudMiddlewareOptions, NextMiddlewareOnAccessDenied, NextMiddlewareOnGroupAccessDenied, NextMiddlewareResult, OnError, PageOnError, PageRouterApiOnAccessDeniedHandler, PageRouterApiOnGroupAccessDeniedHandler, ProtectApiAppOptions, ProtectApiPageOptions, ProtectAppPageOptions, ProtectOptions, ProtectPageGetServerSidePropsContext, ProtectPagePageOnAccessDeniedType, ProtectPagePageOnGroupAccessDeniedType, ProtectPagePageOptions, ProtectPagePageReturnType, ProtectedAppServerComponent, ProtectedAppServerComponentProps, ProtectedRouteMatcher, ProtectedRoutes, RedirectToSignInOptions, RedirectToSignOutOptions } from "./types.mjs";
|
|
2
|
+
import { MonoCloudNextClient } from "./monocloud-next-client.mjs";
|
|
3
|
+
import { authMiddleware, getSession, getTokens, isAuthenticated, isUserInGroup, monoCloudAuth, protect, protectApi, protectPage, redirectToSignIn, redirectToSignOut } from "./initialize.mjs";
|
|
4
|
+
import { AccessToken, Address, ApplicationState, Authenticators, AuthorizationParams, CodeChallengeMethod, DisplayOptions, GetSessionOptions, GetTokensOptions, Group, IdTokenClaims, Indicator, Jwk, MonoCloudAuthBaseError, MonoCloudCookieOptions, MonoCloudHttpError, MonoCloudOPError, MonoCloudOptions, MonoCloudRequest, MonoCloudRoutes, MonoCloudSession, MonoCloudSessionOptions, MonoCloudSessionOptionsBase, MonoCloudSessionStore, MonoCloudStateOptions, MonoCloudStatePartialOptions, MonoCloudTokenError, MonoCloudTokens, MonoCloudUser, MonoCloudValidationError, OnBackChannelLogout, OnSessionCreating, OnSetApplicationState, Prompt, ResponseModes, ResponseTypes, SameSiteValues, SecurityAlgorithms, SessionLifetime, UserinfoResponse } from "@monocloud/auth-node-core";
|
|
5
|
+
export { type AccessToken, type Address, type AppOnError, type AppRouterApiHandlerFn, type AppRouterApiOnAccessDeniedHandler, type AppRouterApiOnGroupAccessDeniedHandler, type AppRouterContext, type AppRouterPageHandler, type ApplicationState, type Authenticators, type AuthorizationParams, type CodeChallengeMethod, type CustomProtectedRouteMatcher, type DisplayOptions, type ExtraAuthParams, type GetSessionOptions, type GetTokensOptions, type Group, type GroupOptions, type IdTokenClaims, type Indicator, type IsUserInGroupOptions, type Jwk, MonoCloudAuthBaseError, type MonoCloudAuthHandler, type MonoCloudAuthOptions, type MonoCloudCookieOptions, MonoCloudHttpError, type MonoCloudMiddlewareOptions, MonoCloudNextClient, MonoCloudOPError, type MonoCloudOptions, type MonoCloudRequest, type MonoCloudRoutes, type MonoCloudSession, type MonoCloudSessionOptions, type MonoCloudSessionOptionsBase, type MonoCloudSessionStore, type MonoCloudStateOptions, type MonoCloudStatePartialOptions, MonoCloudTokenError, type MonoCloudTokens, type MonoCloudUser, MonoCloudValidationError, type NextMiddlewareOnAccessDenied, type NextMiddlewareOnGroupAccessDenied, type NextMiddlewareResult, type OnBackChannelLogout, type OnError, type OnSessionCreating, type OnSetApplicationState, type PageOnError, type PageRouterApiOnAccessDeniedHandler, type PageRouterApiOnGroupAccessDeniedHandler, type Prompt, type ProtectApiAppOptions, type ProtectApiPageOptions, type ProtectAppPageOptions, type ProtectOptions, type ProtectPageGetServerSidePropsContext, type ProtectPagePageOnAccessDeniedType, type ProtectPagePageOnGroupAccessDeniedType, type ProtectPagePageOptions, type ProtectPagePageReturnType, type ProtectedAppServerComponent, type ProtectedAppServerComponentProps, type ProtectedRouteMatcher, type ProtectedRoutes, type RedirectToSignInOptions, type RedirectToSignOutOptions, type ResponseModes, type ResponseTypes, type SameSiteValues, type SecurityAlgorithms, type SessionLifetime, type UserinfoResponse, authMiddleware, getSession, getTokens, isAuthenticated, isUserInGroup, monoCloudAuth, protect, protectApi, protectPage, redirectToSignIn, redirectToSignOut };
|