@imtbl/auth-nextjs 2.12.4-alpha.5 → 2.12.4-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -120
- package/dist/node/{chunk-OPPMGNFZ.js → chunk-BRDI4KXS.js} +75 -14
- package/dist/node/client/index.cjs +9 -18
- package/dist/node/client/index.js +8 -17
- package/dist/node/index.cjs +39 -21
- package/dist/node/index.js +15 -46
- package/dist/node/server/index.cjs +107 -59
- package/dist/node/server/index.js +48 -47
- package/dist/types/client/callback.d.ts +9 -4
- package/dist/types/client/provider.d.ts +20 -6
- package/dist/types/config.d.ts +9 -11
- package/dist/types/index.d.ts +28 -14
- package/dist/types/refresh.d.ts +1 -1
- package/dist/types/server/index.d.ts +104 -2
- package/dist/types/types.d.ts +19 -32
- package/package.json +5 -5
- package/dist/types/server/with-page-auth.d.ts +0 -94
package/dist/types/config.d.ts
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NextAuthConfig } from 'next-auth';
|
|
2
2
|
import type { ImmutableAuthConfig } from './types';
|
|
3
3
|
/**
|
|
4
|
-
* Create
|
|
4
|
+
* Create Auth.js v5 configuration for Immutable authentication
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```typescript
|
|
8
8
|
* // lib/auth.ts
|
|
9
|
-
* import
|
|
9
|
+
* import NextAuth from "next-auth";
|
|
10
|
+
* import { createAuthConfig } from "@imtbl/auth-nextjs";
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
+
* const config = {
|
|
12
13
|
* clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
13
14
|
* redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
|
|
14
|
-
* }
|
|
15
|
-
*
|
|
16
|
-
* // pages/api/auth/[...nextauth].ts
|
|
17
|
-
* import NextAuth from "next-auth";
|
|
18
|
-
* import { authOptions } from "@/lib/auth";
|
|
15
|
+
* };
|
|
19
16
|
*
|
|
20
|
-
* export
|
|
17
|
+
* export const { handlers, auth, signIn, signOut } = NextAuth(createAuthConfig(config));
|
|
21
18
|
* ```
|
|
22
19
|
*/
|
|
23
|
-
export declare function
|
|
20
|
+
export declare function createAuthConfig(config: ImmutableAuthConfig): NextAuthConfig;
|
|
21
|
+
export declare const createAuthOptions: typeof createAuthConfig;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import NextAuthImport from 'next-auth';
|
|
2
|
+
import type { NextAuthConfig } from 'next-auth';
|
|
2
3
|
import type { ImmutableAuthConfig } from './types';
|
|
4
|
+
declare const NextAuth: typeof NextAuthImport;
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
6
|
+
* Auth.js v5 config options that can be overridden.
|
|
5
7
|
* Excludes 'providers' as that's managed internally.
|
|
6
8
|
*/
|
|
7
|
-
export type ImmutableAuthOverrides = Omit<
|
|
9
|
+
export type ImmutableAuthOverrides = Omit<NextAuthConfig, 'providers'>;
|
|
8
10
|
/**
|
|
9
|
-
*
|
|
11
|
+
* Return type of createImmutableAuth - the NextAuth instance with handlers
|
|
12
|
+
*/
|
|
13
|
+
export type ImmutableAuthResult = ReturnType<typeof NextAuth>;
|
|
14
|
+
/**
|
|
15
|
+
* Create an Auth.js v5 instance with Immutable authentication
|
|
10
16
|
*
|
|
11
17
|
* @param config - Immutable auth configuration
|
|
12
|
-
* @param overrides - Optional
|
|
18
|
+
* @param overrides - Optional Auth.js options to override defaults
|
|
19
|
+
* @returns NextAuth instance with { handlers, auth, signIn, signOut }
|
|
13
20
|
*
|
|
14
21
|
* @remarks
|
|
15
22
|
* Callback composition: The `jwt` and `session` callbacks are composed rather than
|
|
@@ -17,20 +24,24 @@ export type ImmutableAuthOverrides = Omit<NextAuthOptions, 'providers'>;
|
|
|
17
24
|
* your custom callbacks receive the result. Other callbacks (`signIn`, `redirect`)
|
|
18
25
|
* are replaced entirely if provided.
|
|
19
26
|
*
|
|
20
|
-
* @example Basic usage
|
|
27
|
+
* @example Basic usage (App Router)
|
|
21
28
|
* ```typescript
|
|
22
|
-
* //
|
|
23
|
-
* import {
|
|
29
|
+
* // lib/auth.ts
|
|
30
|
+
* import { createImmutableAuth } from "@imtbl/auth-nextjs";
|
|
24
31
|
*
|
|
25
|
-
* export
|
|
32
|
+
* export const { handlers, auth, signIn, signOut } = createImmutableAuth({
|
|
26
33
|
* clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
27
34
|
* redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
|
|
28
35
|
* });
|
|
36
|
+
*
|
|
37
|
+
* // app/api/auth/[...nextauth]/route.ts
|
|
38
|
+
* import { handlers } from "@/lib/auth";
|
|
39
|
+
* export const { GET, POST } = handlers;
|
|
29
40
|
* ```
|
|
30
41
|
*
|
|
31
|
-
* @example With
|
|
42
|
+
* @example With Auth.js overrides
|
|
32
43
|
* ```typescript
|
|
33
|
-
* export
|
|
44
|
+
* export const { handlers, auth } = createImmutableAuth(
|
|
34
45
|
* { clientId: "...", redirectUri: "..." },
|
|
35
46
|
* {
|
|
36
47
|
* pages: { signIn: "/custom-login", error: "/auth-error" },
|
|
@@ -41,7 +52,7 @@ export type ImmutableAuthOverrides = Omit<NextAuthOptions, 'providers'>;
|
|
|
41
52
|
*
|
|
42
53
|
* @example With custom jwt callback (composed with internal callback)
|
|
43
54
|
* ```typescript
|
|
44
|
-
* export
|
|
55
|
+
* export const { handlers, auth } = createImmutableAuth(
|
|
45
56
|
* { clientId: "...", redirectUri: "..." },
|
|
46
57
|
* {
|
|
47
58
|
* callbacks: {
|
|
@@ -56,8 +67,11 @@ export type ImmutableAuthOverrides = Omit<NextAuthOptions, 'providers'>;
|
|
|
56
67
|
* );
|
|
57
68
|
* ```
|
|
58
69
|
*/
|
|
59
|
-
export declare function
|
|
60
|
-
export
|
|
70
|
+
export declare function createImmutableAuth(config: ImmutableAuthConfig, overrides?: ImmutableAuthOverrides): ImmutableAuthResult;
|
|
71
|
+
export declare const ImmutableAuth: typeof createImmutableAuth;
|
|
72
|
+
export { createAuthConfig } from './config';
|
|
73
|
+
export type { ImmutableAuthConfig, ImmutableTokenData, ImmutableUser, ZkEvmInfo, } from './types';
|
|
61
74
|
export type { LoginOptions, DirectLoginOptions } from '@imtbl/auth';
|
|
62
75
|
export { MarketingConsentStatus } from '@imtbl/auth';
|
|
63
76
|
export { refreshAccessToken, isTokenExpired } from './refresh';
|
|
77
|
+
export { DEFAULT_AUTH_DOMAIN, DEFAULT_AUDIENCE, DEFAULT_SCOPE, DEFAULT_NEXTAUTH_BASE_PATH, } from './constants';
|
package/dist/types/refresh.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { JWT } from 'next-auth/jwt';
|
|
|
2
2
|
import type { ImmutableAuthConfig } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* Refresh the access token using the refresh token
|
|
5
|
-
* Called by
|
|
5
|
+
* Called by Auth.js JWT callback when token is expired
|
|
6
6
|
*/
|
|
7
7
|
export declare function refreshAccessToken(token: JWT, config: ImmutableAuthConfig): Promise<JWT>;
|
|
8
8
|
/**
|
|
@@ -1,2 +1,104 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { type NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import type { Session } from 'next-auth';
|
|
3
|
+
export { createImmutableAuth, createAuthConfig } from '../index';
|
|
4
|
+
/**
|
|
5
|
+
* Options for createAuthMiddleware
|
|
6
|
+
*/
|
|
7
|
+
export interface AuthMiddlewareOptions {
|
|
8
|
+
/**
|
|
9
|
+
* URL to redirect to when not authenticated
|
|
10
|
+
* @default "/login"
|
|
11
|
+
*/
|
|
12
|
+
loginUrl?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Paths that should be protected (regex patterns)
|
|
15
|
+
* If not provided, middleware should be configured via Next.js matcher
|
|
16
|
+
*/
|
|
17
|
+
protectedPaths?: (string | RegExp)[];
|
|
18
|
+
/**
|
|
19
|
+
* Paths that should be excluded from protection (regex patterns)
|
|
20
|
+
* Takes precedence over protectedPaths
|
|
21
|
+
*/
|
|
22
|
+
publicPaths?: (string | RegExp)[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Type for the auth function returned by createImmutableAuth
|
|
26
|
+
*/
|
|
27
|
+
export type AuthFunction = () => Promise<Session | null>;
|
|
28
|
+
/**
|
|
29
|
+
* Create a Next.js middleware for protecting routes with Immutable authentication.
|
|
30
|
+
*
|
|
31
|
+
* This is the App Router replacement for `withPageAuthRequired`.
|
|
32
|
+
*
|
|
33
|
+
* @param auth - The auth function from createImmutableAuth
|
|
34
|
+
* @param options - Middleware options
|
|
35
|
+
* @returns A Next.js middleware function
|
|
36
|
+
*
|
|
37
|
+
* @example Basic usage with Next.js middleware:
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // middleware.ts
|
|
40
|
+
* import { createAuthMiddleware } from "@imtbl/auth-nextjs/server";
|
|
41
|
+
* import { auth } from "@/lib/auth";
|
|
42
|
+
*
|
|
43
|
+
* export default createAuthMiddleware(auth, {
|
|
44
|
+
* loginUrl: "/login",
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* export const config = {
|
|
48
|
+
* matcher: ["/dashboard/:path*", "/profile/:path*"],
|
|
49
|
+
* };
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example With path configuration:
|
|
53
|
+
* ```typescript
|
|
54
|
+
* export default createAuthMiddleware(auth, {
|
|
55
|
+
* loginUrl: "/login",
|
|
56
|
+
* protectedPaths: [/^\/dashboard/, /^\/profile/],
|
|
57
|
+
* publicPaths: [/^\/api\/public/],
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function createAuthMiddleware(auth: AuthFunction, options?: AuthMiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
62
|
+
/**
|
|
63
|
+
* Higher-order function to protect a Server Action or Route Handler.
|
|
64
|
+
*
|
|
65
|
+
* The returned function forwards all arguments from Next.js to your handler,
|
|
66
|
+
* allowing access to the request, context, form data, or any other arguments.
|
|
67
|
+
*
|
|
68
|
+
* @param auth - The auth function from createImmutableAuth
|
|
69
|
+
* @param handler - The handler function to protect. Receives session as first arg,
|
|
70
|
+
* followed by any arguments passed by Next.js (request, context, etc.)
|
|
71
|
+
* @returns A protected handler that checks authentication before executing
|
|
72
|
+
*
|
|
73
|
+
* @example Protecting a Route Handler with request access:
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // app/api/protected/route.ts
|
|
76
|
+
* import { withAuth } from "@imtbl/auth-nextjs/server";
|
|
77
|
+
* import { auth } from "@/lib/auth";
|
|
78
|
+
*
|
|
79
|
+
* export const POST = withAuth(auth, async (session, request: Request) => {
|
|
80
|
+
* const body = await request.json();
|
|
81
|
+
* return Response.json({ user: session.user, data: body });
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* export const GET = withAuth(auth, async (session, request: Request, context) => {
|
|
85
|
+
* const { params } = context;
|
|
86
|
+
* return Response.json({ user: session.user, params: await params });
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @example Protecting a Server Action:
|
|
91
|
+
* ```typescript
|
|
92
|
+
* // app/actions.ts
|
|
93
|
+
* "use server";
|
|
94
|
+
* import { withAuth } from "@imtbl/auth-nextjs/server";
|
|
95
|
+
* import { auth } from "@/lib/auth";
|
|
96
|
+
*
|
|
97
|
+
* export const protectedAction = withAuth(auth, async (session, formData: FormData) => {
|
|
98
|
+
* const userId = session.user.sub;
|
|
99
|
+
* const name = formData.get("name");
|
|
100
|
+
* // ... your action logic
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export declare function withAuth<TArgs extends unknown[], TReturn>(auth: AuthFunction, handler: (session: Session, ...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { DefaultSession,
|
|
2
|
-
import type {
|
|
1
|
+
import type { DefaultSession, Session } from 'next-auth';
|
|
2
|
+
import type { JWT } from 'next-auth/jwt';
|
|
3
3
|
/**
|
|
4
|
-
* Configuration for ImmutableAuthProvider and
|
|
4
|
+
* Configuration for ImmutableAuthProvider and createImmutableAuth
|
|
5
5
|
*/
|
|
6
6
|
export interface ImmutableAuthConfig {
|
|
7
7
|
/**
|
|
@@ -45,11 +45,11 @@ export interface ImmutableUser {
|
|
|
45
45
|
nickname?: string;
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* Auth.js v5 module augmentation to add Immutable-specific fields
|
|
49
49
|
*/
|
|
50
50
|
declare module 'next-auth' {
|
|
51
51
|
interface Session extends DefaultSession {
|
|
52
|
-
user: ImmutableUser;
|
|
52
|
+
user: ImmutableUser & DefaultSession['user'];
|
|
53
53
|
accessToken: string;
|
|
54
54
|
refreshToken?: string;
|
|
55
55
|
idToken?: string;
|
|
@@ -57,9 +57,10 @@ declare module 'next-auth' {
|
|
|
57
57
|
zkEvm?: ZkEvmInfo;
|
|
58
58
|
error?: string;
|
|
59
59
|
}
|
|
60
|
-
interface User
|
|
60
|
+
interface User {
|
|
61
|
+
id: string;
|
|
61
62
|
sub: string;
|
|
62
|
-
email?: string;
|
|
63
|
+
email?: string | null;
|
|
63
64
|
nickname?: string;
|
|
64
65
|
accessToken: string;
|
|
65
66
|
refreshToken?: string;
|
|
@@ -69,20 +70,20 @@ declare module 'next-auth' {
|
|
|
69
70
|
}
|
|
70
71
|
}
|
|
71
72
|
declare module 'next-auth/jwt' {
|
|
72
|
-
interface JWT
|
|
73
|
-
sub
|
|
74
|
-
email?: string;
|
|
73
|
+
interface JWT {
|
|
74
|
+
sub?: string;
|
|
75
|
+
email?: string | null;
|
|
75
76
|
nickname?: string;
|
|
76
|
-
accessToken
|
|
77
|
+
accessToken?: string;
|
|
77
78
|
refreshToken?: string;
|
|
78
79
|
idToken?: string;
|
|
79
|
-
accessTokenExpires
|
|
80
|
+
accessTokenExpires?: number;
|
|
80
81
|
zkEvm?: ZkEvmInfo;
|
|
81
82
|
error?: string;
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
/**
|
|
85
|
-
* Token data passed from client to
|
|
86
|
+
* Token data passed from client to Auth.js credentials provider
|
|
86
87
|
*/
|
|
87
88
|
export interface ImmutableTokenData {
|
|
88
89
|
accessToken: string;
|
|
@@ -127,11 +128,11 @@ export interface ImmutableAuthProviderProps {
|
|
|
127
128
|
config: ImmutableAuthConfig;
|
|
128
129
|
/**
|
|
129
130
|
* Initial session from server (for SSR hydration)
|
|
130
|
-
* Can be Session from
|
|
131
|
+
* Can be Session from auth() or any compatible session object
|
|
131
132
|
*/
|
|
132
133
|
session?: Session | DefaultSession | null;
|
|
133
134
|
/**
|
|
134
|
-
* Custom base path for
|
|
135
|
+
* Custom base path for Auth.js API routes
|
|
135
136
|
* Use this when you have multiple auth endpoints (e.g., per environment)
|
|
136
137
|
* @default "/api/auth"
|
|
137
138
|
*/
|
|
@@ -146,7 +147,7 @@ export interface UseImmutableAuthReturn {
|
|
|
146
147
|
*/
|
|
147
148
|
user: ImmutableUser | null;
|
|
148
149
|
/**
|
|
149
|
-
* Full
|
|
150
|
+
* Full Auth.js session with tokens
|
|
150
151
|
*/
|
|
151
152
|
session: Session | null;
|
|
152
153
|
/**
|
|
@@ -163,7 +164,7 @@ export interface UseImmutableAuthReturn {
|
|
|
163
164
|
*/
|
|
164
165
|
signIn: (options?: import('@imtbl/auth').LoginOptions) => Promise<void>;
|
|
165
166
|
/**
|
|
166
|
-
* Sign out from both
|
|
167
|
+
* Sign out from both Auth.js and Immutable
|
|
167
168
|
*/
|
|
168
169
|
signOut: () => Promise<void>;
|
|
169
170
|
/**
|
|
@@ -175,18 +176,4 @@ export interface UseImmutableAuthReturn {
|
|
|
175
176
|
*/
|
|
176
177
|
auth: import('@imtbl/auth').Auth | null;
|
|
177
178
|
}
|
|
178
|
-
|
|
179
|
-
* Options for withPageAuthRequired
|
|
180
|
-
*/
|
|
181
|
-
export interface WithPageAuthRequiredOptions {
|
|
182
|
-
/**
|
|
183
|
-
* URL to redirect to when not authenticated
|
|
184
|
-
* @default "/login"
|
|
185
|
-
*/
|
|
186
|
-
loginUrl?: string;
|
|
187
|
-
/**
|
|
188
|
-
* URL to redirect to after login
|
|
189
|
-
* @default current page
|
|
190
|
-
*/
|
|
191
|
-
returnTo?: string | false;
|
|
192
|
-
}
|
|
179
|
+
export type { JWT };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imtbl/auth-nextjs",
|
|
3
|
-
"version": "2.12.4-alpha.
|
|
4
|
-
"description": "Next.js authentication integration for Immutable SDK using
|
|
3
|
+
"version": "2.12.4-alpha.6",
|
|
4
|
+
"description": "Next.js App Router authentication integration for Immutable SDK using Auth.js v5",
|
|
5
5
|
"author": "Immutable",
|
|
6
6
|
"bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
|
|
7
7
|
"homepage": "https://github.com/immutable/ts-immutable-sdk#readme",
|
|
@@ -51,11 +51,11 @@
|
|
|
51
51
|
"dist"
|
|
52
52
|
],
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@imtbl/auth": "2.12.4-alpha.
|
|
54
|
+
"@imtbl/auth": "2.12.4-alpha.6"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"next": "14.2.25",
|
|
58
|
-
"next-auth": "^
|
|
58
|
+
"next-auth": "^5.0.0-beta.30",
|
|
59
59
|
"react": "^18.2.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"jest": "^29.4.3",
|
|
68
68
|
"jest-environment-jsdom": "^29.4.3",
|
|
69
69
|
"next": "14.2.25",
|
|
70
|
-
"next-auth": "^
|
|
70
|
+
"next-auth": "^5.0.0-beta.30",
|
|
71
71
|
"react": "^18.2.0",
|
|
72
72
|
"ts-node": "^10.9.1",
|
|
73
73
|
"tsup": "^8.3.0",
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import type { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
|
|
2
|
-
import type { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
-
import { type Session } from 'next-auth';
|
|
4
|
-
import type { ImmutableAuthConfig, WithPageAuthRequiredOptions } from '../types';
|
|
5
|
-
/**
|
|
6
|
-
* Extended options for withPageAuthRequired
|
|
7
|
-
*/
|
|
8
|
-
export interface WithPageAuthRequiredFullOptions<P extends Record<string, unknown> = Record<string, unknown>> extends WithPageAuthRequiredOptions {
|
|
9
|
-
/**
|
|
10
|
-
* Custom getServerSideProps that runs after auth check.
|
|
11
|
-
* Session is guaranteed to exist when this runs.
|
|
12
|
-
*/
|
|
13
|
-
getServerSideProps?: (ctx: GetServerSidePropsContext, session: Session) => Promise<GetServerSidePropsResult<P>>;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Props added by withPageAuthRequired
|
|
17
|
-
*/
|
|
18
|
-
export interface WithPageAuthRequiredProps {
|
|
19
|
-
session: Session;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Get the Immutable session on the server side.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```typescript
|
|
26
|
-
* // pages/api/user.ts
|
|
27
|
-
* import { getImmutableSession } from "@imtbl/auth-nextjs/server";
|
|
28
|
-
*
|
|
29
|
-
* const config = { clientId: "...", redirectUri: "..." };
|
|
30
|
-
*
|
|
31
|
-
* export default async function handler(req, res) {
|
|
32
|
-
* const session = await getImmutableSession(req, res, config);
|
|
33
|
-
* if (!session) {
|
|
34
|
-
* return res.status(401).json({ error: "Not authenticated" });
|
|
35
|
-
* }
|
|
36
|
-
* res.json({ user: session.user });
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example In getServerSideProps
|
|
41
|
-
* ```typescript
|
|
42
|
-
* export const getServerSideProps = async (ctx) => {
|
|
43
|
-
* const session = await getImmutableSession(ctx.req, ctx.res, config);
|
|
44
|
-
* return { props: { user: session?.user ?? null } };
|
|
45
|
-
* };
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export declare function getImmutableSession(req: IncomingMessage & {
|
|
49
|
-
cookies: Partial<Record<string, string>>;
|
|
50
|
-
}, res: ServerResponse, config: ImmutableAuthConfig): Promise<Session | null>;
|
|
51
|
-
/**
|
|
52
|
-
* Higher-order function that protects a page with authentication.
|
|
53
|
-
*
|
|
54
|
-
* When a signed-out user visits the page:
|
|
55
|
-
* 1. Server checks session via getServerSession() → returns null
|
|
56
|
-
* 2. Returns HTTP redirect to login page with returnTo parameter
|
|
57
|
-
* 3. After login, user is redirected back to original page
|
|
58
|
-
*
|
|
59
|
-
* @example Basic usage:
|
|
60
|
-
* ```typescript
|
|
61
|
-
* // pages/dashboard.tsx
|
|
62
|
-
* import { withPageAuthRequired } from "@imtbl/auth-nextjs/server";
|
|
63
|
-
*
|
|
64
|
-
* const config = { clientId: "...", redirectUri: "..." };
|
|
65
|
-
*
|
|
66
|
-
* function DashboardPage() {
|
|
67
|
-
* // Page only renders if user is authenticated
|
|
68
|
-
* return <h1>Dashboard</h1>;
|
|
69
|
-
* }
|
|
70
|
-
*
|
|
71
|
-
* export default DashboardPage;
|
|
72
|
-
* export const getServerSideProps = withPageAuthRequired(config);
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* @example With additional data fetching:
|
|
76
|
-
* ```typescript
|
|
77
|
-
* export const getServerSideProps = withPageAuthRequired(config, {
|
|
78
|
-
* async getServerSideProps(ctx, session) {
|
|
79
|
-
* // session is guaranteed to exist here
|
|
80
|
-
* const data = await fetchData(session.accessToken);
|
|
81
|
-
* return { props: { data } };
|
|
82
|
-
* },
|
|
83
|
-
* });
|
|
84
|
-
* ```
|
|
85
|
-
*
|
|
86
|
-
* @example With custom options:
|
|
87
|
-
* ```typescript
|
|
88
|
-
* export const getServerSideProps = withPageAuthRequired(config, {
|
|
89
|
-
* loginUrl: "/auth/signin",
|
|
90
|
-
* returnTo: "/dashboard",
|
|
91
|
-
* });
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
export declare function withPageAuthRequired<P extends Record<string, unknown> = Record<string, unknown>>(config: ImmutableAuthConfig, options?: WithPageAuthRequiredFullOptions<P>): GetServerSideProps<WithPageAuthRequiredProps & P>;
|