transactional-auth-next 0.1.0
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 +312 -0
- package/dist/chunk-4S34DQOR.mjs +52 -0
- package/dist/client/index.d.mts +73 -0
- package/dist/client/index.d.ts +73 -0
- package/dist/client/index.js +110 -0
- package/dist/client/index.mjs +71 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +80 -0
- package/dist/index.mjs +10 -0
- package/dist/middleware/index.d.mts +56 -0
- package/dist/middleware/index.d.ts +56 -0
- package/dist/middleware/index.js +119 -0
- package/dist/middleware/index.mjs +93 -0
- package/dist/server/index.d.mts +97 -0
- package/dist/server/index.d.ts +97 -0
- package/dist/server/index.js +369 -0
- package/dist/server/index.mjs +296 -0
- package/dist/types-D3JPYyLl.d.mts +57 -0
- package/dist/types-D3JPYyLl.d.ts +57 -0
- package/package.json +80 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { T as TransactionalAuthConfig } from './types-D3JPYyLl.js';
|
|
2
|
+
export { L as LoginOptions, b as LogoutOptions, S as Session, a as TransactionalAuthUser } from './types-D3JPYyLl.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transactional Auth Next - Configuration
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Initialize the Transactional Auth SDK with your configuration.
|
|
10
|
+
* Call this once in your application, typically in a layout or middleware.
|
|
11
|
+
*/
|
|
12
|
+
declare function initTransactionalAuth(config: TransactionalAuthConfig): void;
|
|
13
|
+
/**
|
|
14
|
+
* Get the current configuration.
|
|
15
|
+
* Throws if not initialized.
|
|
16
|
+
*/
|
|
17
|
+
declare function getConfig(): TransactionalAuthConfig;
|
|
18
|
+
/**
|
|
19
|
+
* Check if the SDK is initialized.
|
|
20
|
+
*/
|
|
21
|
+
declare function isInitialized(): boolean;
|
|
22
|
+
|
|
23
|
+
export { TransactionalAuthConfig, getConfig, initTransactionalAuth, isInitialized };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
getConfig: () => getConfig,
|
|
24
|
+
initTransactionalAuth: () => initTransactionalAuth,
|
|
25
|
+
isInitialized: () => isInitialized
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/config.ts
|
|
30
|
+
var globalConfig = null;
|
|
31
|
+
function initTransactionalAuth(config) {
|
|
32
|
+
globalConfig = {
|
|
33
|
+
...config,
|
|
34
|
+
scope: config.scope || "openid profile email",
|
|
35
|
+
cookieName: config.cookieName || "transactional_session",
|
|
36
|
+
cookieOptions: {
|
|
37
|
+
secure: config.cookieOptions?.secure ?? process.env.NODE_ENV === "production",
|
|
38
|
+
sameSite: config.cookieOptions?.sameSite ?? "lax",
|
|
39
|
+
maxAge: config.cookieOptions?.maxAge ?? 7 * 24 * 60 * 60
|
|
40
|
+
// 7 days
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function getConfig() {
|
|
45
|
+
if (!globalConfig) {
|
|
46
|
+
const domain = process.env.TRANSACTIONAL_AUTH_DOMAIN || process.env.NEXT_PUBLIC_TRANSACTIONAL_AUTH_DOMAIN;
|
|
47
|
+
const clientId = process.env.TRANSACTIONAL_AUTH_CLIENT_ID || process.env.NEXT_PUBLIC_TRANSACTIONAL_AUTH_CLIENT_ID;
|
|
48
|
+
const clientSecret = process.env.TRANSACTIONAL_AUTH_CLIENT_SECRET;
|
|
49
|
+
const baseUrl = process.env.TRANSACTIONAL_AUTH_BASE_URL || process.env.NEXT_PUBLIC_APP_URL;
|
|
50
|
+
if (domain && clientId) {
|
|
51
|
+
globalConfig = {
|
|
52
|
+
domain,
|
|
53
|
+
clientId,
|
|
54
|
+
clientSecret,
|
|
55
|
+
baseUrl,
|
|
56
|
+
scope: "openid profile email",
|
|
57
|
+
cookieName: "transactional_session",
|
|
58
|
+
cookieOptions: {
|
|
59
|
+
secure: process.env.NODE_ENV === "production",
|
|
60
|
+
sameSite: "lax",
|
|
61
|
+
maxAge: 7 * 24 * 60 * 60
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
return globalConfig;
|
|
65
|
+
}
|
|
66
|
+
throw new Error(
|
|
67
|
+
"Transactional Auth not initialized. Call initTransactionalAuth() or set environment variables."
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return globalConfig;
|
|
71
|
+
}
|
|
72
|
+
function isInitialized() {
|
|
73
|
+
return globalConfig !== null;
|
|
74
|
+
}
|
|
75
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
76
|
+
0 && (module.exports = {
|
|
77
|
+
getConfig,
|
|
78
|
+
initTransactionalAuth,
|
|
79
|
+
isInitialized
|
|
80
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import { S as Session } from '../types-D3JPYyLl.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transactional Auth Next - Middleware
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
interface AuthMiddlewareConfig {
|
|
9
|
+
/** Cookie name for session (defaults to 'transactional_session') */
|
|
10
|
+
cookieName?: string;
|
|
11
|
+
/** Paths that require authentication (glob patterns) */
|
|
12
|
+
protectedPaths?: string[];
|
|
13
|
+
/** Paths that are always public (glob patterns) */
|
|
14
|
+
publicPaths?: string[];
|
|
15
|
+
/** Where to redirect unauthenticated users (defaults to '/api/auth/login') */
|
|
16
|
+
loginUrl?: string;
|
|
17
|
+
/** Callback to handle unauthorized access */
|
|
18
|
+
onUnauthorized?: (request: NextRequest) => NextResponse | Promise<NextResponse>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create authentication middleware for Next.js.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* // middleware.ts
|
|
26
|
+
* import { createAuthMiddleware } from 'transactional-auth-next/middleware';
|
|
27
|
+
*
|
|
28
|
+
* export default createAuthMiddleware({
|
|
29
|
+
* protectedPaths: ['/dashboard/*', '/settings/*'],
|
|
30
|
+
* publicPaths: ['/', '/about', '/api/public/*'],
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* export const config = {
|
|
34
|
+
* matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
35
|
+
* };
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function createAuthMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
39
|
+
/**
|
|
40
|
+
* Helper to protect API routes.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // app/api/protected/route.ts
|
|
45
|
+
* import { withAuth } from 'transactional-auth-next/middleware';
|
|
46
|
+
*
|
|
47
|
+
* export const GET = withAuth(async (request, session) => {
|
|
48
|
+
* return Response.json({ user: session.user });
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function withAuth<T extends unknown[]>(handler: (request: NextRequest, session: Session, ...args: T) => Response | Promise<Response>, options?: {
|
|
53
|
+
cookieName?: string;
|
|
54
|
+
}): (request: NextRequest, ...args: T) => Promise<Response>;
|
|
55
|
+
|
|
56
|
+
export { type AuthMiddlewareConfig, createAuthMiddleware, withAuth };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import { S as Session } from '../types-D3JPYyLl.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transactional Auth Next - Middleware
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
interface AuthMiddlewareConfig {
|
|
9
|
+
/** Cookie name for session (defaults to 'transactional_session') */
|
|
10
|
+
cookieName?: string;
|
|
11
|
+
/** Paths that require authentication (glob patterns) */
|
|
12
|
+
protectedPaths?: string[];
|
|
13
|
+
/** Paths that are always public (glob patterns) */
|
|
14
|
+
publicPaths?: string[];
|
|
15
|
+
/** Where to redirect unauthenticated users (defaults to '/api/auth/login') */
|
|
16
|
+
loginUrl?: string;
|
|
17
|
+
/** Callback to handle unauthorized access */
|
|
18
|
+
onUnauthorized?: (request: NextRequest) => NextResponse | Promise<NextResponse>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create authentication middleware for Next.js.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* // middleware.ts
|
|
26
|
+
* import { createAuthMiddleware } from 'transactional-auth-next/middleware';
|
|
27
|
+
*
|
|
28
|
+
* export default createAuthMiddleware({
|
|
29
|
+
* protectedPaths: ['/dashboard/*', '/settings/*'],
|
|
30
|
+
* publicPaths: ['/', '/about', '/api/public/*'],
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* export const config = {
|
|
34
|
+
* matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
35
|
+
* };
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function createAuthMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
39
|
+
/**
|
|
40
|
+
* Helper to protect API routes.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // app/api/protected/route.ts
|
|
45
|
+
* import { withAuth } from 'transactional-auth-next/middleware';
|
|
46
|
+
*
|
|
47
|
+
* export const GET = withAuth(async (request, session) => {
|
|
48
|
+
* return Response.json({ user: session.user });
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function withAuth<T extends unknown[]>(handler: (request: NextRequest, session: Session, ...args: T) => Response | Promise<Response>, options?: {
|
|
53
|
+
cookieName?: string;
|
|
54
|
+
}): (request: NextRequest, ...args: T) => Promise<Response>;
|
|
55
|
+
|
|
56
|
+
export { type AuthMiddlewareConfig, createAuthMiddleware, withAuth };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/middleware/index.ts
|
|
21
|
+
var middleware_exports = {};
|
|
22
|
+
__export(middleware_exports, {
|
|
23
|
+
createAuthMiddleware: () => createAuthMiddleware,
|
|
24
|
+
withAuth: () => withAuth
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
27
|
+
var import_server = require("next/server");
|
|
28
|
+
function createAuthMiddleware(config = {}) {
|
|
29
|
+
const {
|
|
30
|
+
cookieName = "transactional_session",
|
|
31
|
+
protectedPaths = [],
|
|
32
|
+
publicPaths = [],
|
|
33
|
+
loginUrl = "/api/auth/login",
|
|
34
|
+
onUnauthorized
|
|
35
|
+
} = config;
|
|
36
|
+
return async (request) => {
|
|
37
|
+
const { pathname } = request.nextUrl;
|
|
38
|
+
if (isPathMatch(pathname, publicPaths)) {
|
|
39
|
+
return import_server.NextResponse.next();
|
|
40
|
+
}
|
|
41
|
+
const isProtected = protectedPaths.length === 0 || isPathMatch(pathname, protectedPaths);
|
|
42
|
+
if (!isProtected) {
|
|
43
|
+
return import_server.NextResponse.next();
|
|
44
|
+
}
|
|
45
|
+
const sessionCookie = request.cookies.get(cookieName);
|
|
46
|
+
if (!sessionCookie?.value) {
|
|
47
|
+
return handleUnauthorized(request, loginUrl, onUnauthorized);
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const session = JSON.parse(
|
|
51
|
+
Buffer.from(sessionCookie.value, "base64").toString("utf-8")
|
|
52
|
+
);
|
|
53
|
+
if (session.expiresAt < Date.now() / 1e3) {
|
|
54
|
+
return handleUnauthorized(request, loginUrl, onUnauthorized);
|
|
55
|
+
}
|
|
56
|
+
const requestHeaders = new Headers(request.headers);
|
|
57
|
+
requestHeaders.set("x-auth-user-id", session.user.sub);
|
|
58
|
+
if (session.user.email) {
|
|
59
|
+
requestHeaders.set("x-auth-user-email", session.user.email);
|
|
60
|
+
}
|
|
61
|
+
return import_server.NextResponse.next({
|
|
62
|
+
request: {
|
|
63
|
+
headers: requestHeaders
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
} catch {
|
|
67
|
+
return handleUnauthorized(request, loginUrl, onUnauthorized);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function isPathMatch(pathname, patterns) {
|
|
72
|
+
return patterns.some((pattern) => {
|
|
73
|
+
const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
74
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
75
|
+
return regex.test(pathname);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async function handleUnauthorized(request, loginUrl, onUnauthorized) {
|
|
79
|
+
if (onUnauthorized) {
|
|
80
|
+
return onUnauthorized(request);
|
|
81
|
+
}
|
|
82
|
+
const url = new URL(loginUrl, request.url);
|
|
83
|
+
url.searchParams.set("returnTo", request.nextUrl.pathname);
|
|
84
|
+
return import_server.NextResponse.redirect(url);
|
|
85
|
+
}
|
|
86
|
+
function withAuth(handler, options) {
|
|
87
|
+
const cookieName = options?.cookieName || "transactional_session";
|
|
88
|
+
return async (request, ...args) => {
|
|
89
|
+
const sessionCookie = request.cookies.get(cookieName);
|
|
90
|
+
if (!sessionCookie?.value) {
|
|
91
|
+
return Response.json(
|
|
92
|
+
{ error: { code: "unauthorized", message: "Not authenticated" } },
|
|
93
|
+
{ status: 401 }
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const session = JSON.parse(
|
|
98
|
+
Buffer.from(sessionCookie.value, "base64").toString("utf-8")
|
|
99
|
+
);
|
|
100
|
+
if (session.expiresAt < Date.now() / 1e3) {
|
|
101
|
+
return Response.json(
|
|
102
|
+
{ error: { code: "unauthorized", message: "Session expired" } },
|
|
103
|
+
{ status: 401 }
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return handler(request, session, ...args);
|
|
107
|
+
} catch {
|
|
108
|
+
return Response.json(
|
|
109
|
+
{ error: { code: "unauthorized", message: "Invalid session" } },
|
|
110
|
+
{ status: 401 }
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
116
|
+
0 && (module.exports = {
|
|
117
|
+
createAuthMiddleware,
|
|
118
|
+
withAuth
|
|
119
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// src/middleware/index.ts
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
function createAuthMiddleware(config = {}) {
|
|
4
|
+
const {
|
|
5
|
+
cookieName = "transactional_session",
|
|
6
|
+
protectedPaths = [],
|
|
7
|
+
publicPaths = [],
|
|
8
|
+
loginUrl = "/api/auth/login",
|
|
9
|
+
onUnauthorized
|
|
10
|
+
} = config;
|
|
11
|
+
return async (request) => {
|
|
12
|
+
const { pathname } = request.nextUrl;
|
|
13
|
+
if (isPathMatch(pathname, publicPaths)) {
|
|
14
|
+
return NextResponse.next();
|
|
15
|
+
}
|
|
16
|
+
const isProtected = protectedPaths.length === 0 || isPathMatch(pathname, protectedPaths);
|
|
17
|
+
if (!isProtected) {
|
|
18
|
+
return NextResponse.next();
|
|
19
|
+
}
|
|
20
|
+
const sessionCookie = request.cookies.get(cookieName);
|
|
21
|
+
if (!sessionCookie?.value) {
|
|
22
|
+
return handleUnauthorized(request, loginUrl, onUnauthorized);
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const session = JSON.parse(
|
|
26
|
+
Buffer.from(sessionCookie.value, "base64").toString("utf-8")
|
|
27
|
+
);
|
|
28
|
+
if (session.expiresAt < Date.now() / 1e3) {
|
|
29
|
+
return handleUnauthorized(request, loginUrl, onUnauthorized);
|
|
30
|
+
}
|
|
31
|
+
const requestHeaders = new Headers(request.headers);
|
|
32
|
+
requestHeaders.set("x-auth-user-id", session.user.sub);
|
|
33
|
+
if (session.user.email) {
|
|
34
|
+
requestHeaders.set("x-auth-user-email", session.user.email);
|
|
35
|
+
}
|
|
36
|
+
return NextResponse.next({
|
|
37
|
+
request: {
|
|
38
|
+
headers: requestHeaders
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
} catch {
|
|
42
|
+
return handleUnauthorized(request, loginUrl, onUnauthorized);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function isPathMatch(pathname, patterns) {
|
|
47
|
+
return patterns.some((pattern) => {
|
|
48
|
+
const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
49
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
50
|
+
return regex.test(pathname);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async function handleUnauthorized(request, loginUrl, onUnauthorized) {
|
|
54
|
+
if (onUnauthorized) {
|
|
55
|
+
return onUnauthorized(request);
|
|
56
|
+
}
|
|
57
|
+
const url = new URL(loginUrl, request.url);
|
|
58
|
+
url.searchParams.set("returnTo", request.nextUrl.pathname);
|
|
59
|
+
return NextResponse.redirect(url);
|
|
60
|
+
}
|
|
61
|
+
function withAuth(handler, options) {
|
|
62
|
+
const cookieName = options?.cookieName || "transactional_session";
|
|
63
|
+
return async (request, ...args) => {
|
|
64
|
+
const sessionCookie = request.cookies.get(cookieName);
|
|
65
|
+
if (!sessionCookie?.value) {
|
|
66
|
+
return Response.json(
|
|
67
|
+
{ error: { code: "unauthorized", message: "Not authenticated" } },
|
|
68
|
+
{ status: 401 }
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const session = JSON.parse(
|
|
73
|
+
Buffer.from(sessionCookie.value, "base64").toString("utf-8")
|
|
74
|
+
);
|
|
75
|
+
if (session.expiresAt < Date.now() / 1e3) {
|
|
76
|
+
return Response.json(
|
|
77
|
+
{ error: { code: "unauthorized", message: "Session expired" } },
|
|
78
|
+
{ status: 401 }
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return handler(request, session, ...args);
|
|
82
|
+
} catch {
|
|
83
|
+
return Response.json(
|
|
84
|
+
{ error: { code: "unauthorized", message: "Invalid session" } },
|
|
85
|
+
{ status: 401 }
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export {
|
|
91
|
+
createAuthMiddleware,
|
|
92
|
+
withAuth
|
|
93
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { S as Session, a as TransactionalAuthUser, L as LoginOptions, b as LogoutOptions } from '../types-D3JPYyLl.mjs';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transactional Auth Next - Server Session Management
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the current session from cookies (Server Component).
|
|
10
|
+
* Returns null if not authenticated.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* // app/page.tsx (Server Component)
|
|
15
|
+
* import { getSession } from 'transactional-auth-next/server';
|
|
16
|
+
*
|
|
17
|
+
* export default async function Page() {
|
|
18
|
+
* const session = await getSession();
|
|
19
|
+
*
|
|
20
|
+
* if (!session) {
|
|
21
|
+
* return <p>Not logged in</p>;
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* return <p>Hello, {session.user.name}</p>;
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function getSession(): Promise<Session | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Get the current user from the session (Server Component).
|
|
31
|
+
* Returns null if not authenticated.
|
|
32
|
+
*/
|
|
33
|
+
declare function getUser(): Promise<TransactionalAuthUser | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Get the access token from the session (Server Component).
|
|
36
|
+
* Returns null if not authenticated.
|
|
37
|
+
*/
|
|
38
|
+
declare function getAccessToken(): Promise<string | null>;
|
|
39
|
+
/**
|
|
40
|
+
* Check if the user is authenticated (Server Component).
|
|
41
|
+
*/
|
|
42
|
+
declare function isAuthenticated(): Promise<boolean>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Transactional Auth Next - Route Handlers
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Handle login request - redirects to auth server.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* // app/api/auth/login/route.ts
|
|
54
|
+
* import { handleLogin } from 'transactional-auth-next/server';
|
|
55
|
+
*
|
|
56
|
+
* export const GET = handleLogin;
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function handleLogin(options?: LoginOptions): (request: NextRequest) => Promise<NextResponse>;
|
|
60
|
+
/**
|
|
61
|
+
* Handle callback from auth server - exchanges code for tokens.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* // app/api/auth/callback/route.ts
|
|
66
|
+
* import { handleCallback } from 'transactional-auth-next/server';
|
|
67
|
+
*
|
|
68
|
+
* export const GET = handleCallback;
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function handleCallback(): (request: NextRequest) => Promise<NextResponse>;
|
|
72
|
+
/**
|
|
73
|
+
* Handle logout request.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // app/api/auth/logout/route.ts
|
|
78
|
+
* import { handleLogout } from 'transactional-auth-next/server';
|
|
79
|
+
*
|
|
80
|
+
* export const GET = handleLogout;
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
declare function handleLogout(options?: LogoutOptions): (request: NextRequest) => Promise<NextResponse>;
|
|
84
|
+
/**
|
|
85
|
+
* Handle getting current session (API route).
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* // app/api/auth/session/route.ts
|
|
90
|
+
* import { handleSession } from 'transactional-auth-next/server';
|
|
91
|
+
*
|
|
92
|
+
* export const GET = handleSession;
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function handleSession(): () => Promise<NextResponse>;
|
|
96
|
+
|
|
97
|
+
export { getAccessToken, getSession, getUser, handleCallback, handleLogin, handleLogout, handleSession, isAuthenticated };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { S as Session, a as TransactionalAuthUser, L as LoginOptions, b as LogoutOptions } from '../types-D3JPYyLl.js';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Transactional Auth Next - Server Session Management
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the current session from cookies (Server Component).
|
|
10
|
+
* Returns null if not authenticated.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* // app/page.tsx (Server Component)
|
|
15
|
+
* import { getSession } from 'transactional-auth-next/server';
|
|
16
|
+
*
|
|
17
|
+
* export default async function Page() {
|
|
18
|
+
* const session = await getSession();
|
|
19
|
+
*
|
|
20
|
+
* if (!session) {
|
|
21
|
+
* return <p>Not logged in</p>;
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* return <p>Hello, {session.user.name}</p>;
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function getSession(): Promise<Session | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Get the current user from the session (Server Component).
|
|
31
|
+
* Returns null if not authenticated.
|
|
32
|
+
*/
|
|
33
|
+
declare function getUser(): Promise<TransactionalAuthUser | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Get the access token from the session (Server Component).
|
|
36
|
+
* Returns null if not authenticated.
|
|
37
|
+
*/
|
|
38
|
+
declare function getAccessToken(): Promise<string | null>;
|
|
39
|
+
/**
|
|
40
|
+
* Check if the user is authenticated (Server Component).
|
|
41
|
+
*/
|
|
42
|
+
declare function isAuthenticated(): Promise<boolean>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Transactional Auth Next - Route Handlers
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Handle login request - redirects to auth server.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* // app/api/auth/login/route.ts
|
|
54
|
+
* import { handleLogin } from 'transactional-auth-next/server';
|
|
55
|
+
*
|
|
56
|
+
* export const GET = handleLogin;
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function handleLogin(options?: LoginOptions): (request: NextRequest) => Promise<NextResponse>;
|
|
60
|
+
/**
|
|
61
|
+
* Handle callback from auth server - exchanges code for tokens.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* // app/api/auth/callback/route.ts
|
|
66
|
+
* import { handleCallback } from 'transactional-auth-next/server';
|
|
67
|
+
*
|
|
68
|
+
* export const GET = handleCallback;
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function handleCallback(): (request: NextRequest) => Promise<NextResponse>;
|
|
72
|
+
/**
|
|
73
|
+
* Handle logout request.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // app/api/auth/logout/route.ts
|
|
78
|
+
* import { handleLogout } from 'transactional-auth-next/server';
|
|
79
|
+
*
|
|
80
|
+
* export const GET = handleLogout;
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
declare function handleLogout(options?: LogoutOptions): (request: NextRequest) => Promise<NextResponse>;
|
|
84
|
+
/**
|
|
85
|
+
* Handle getting current session (API route).
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* // app/api/auth/session/route.ts
|
|
90
|
+
* import { handleSession } from 'transactional-auth-next/server';
|
|
91
|
+
*
|
|
92
|
+
* export const GET = handleSession;
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function handleSession(): () => Promise<NextResponse>;
|
|
96
|
+
|
|
97
|
+
export { getAccessToken, getSession, getUser, handleCallback, handleLogin, handleLogout, handleSession, isAuthenticated };
|