@tern-secure/nextjs 4.2.3 → 4.2.5
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/dist/cjs/server/ternSecureMiddleware.js +11 -26
- package/dist/cjs/server/ternSecureMiddleware.js.map +1 -1
- package/dist/esm/server/ternSecureMiddleware.js +10 -26
- package/dist/esm/server/ternSecureMiddleware.js.map +1 -1
- package/dist/types/server/ternSecureMiddleware.d.ts +5 -4
- package/dist/types/server/ternSecureMiddleware.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -19,18 +19,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var ternSecureMiddleware_exports = {};
|
|
20
20
|
__export(ternSecureMiddleware_exports, {
|
|
21
21
|
createRouteMatcher: () => createRouteMatcher,
|
|
22
|
+
runtime: () => runtime,
|
|
22
23
|
ternSecureMiddleware: () => ternSecureMiddleware
|
|
23
24
|
});
|
|
24
25
|
module.exports = __toCommonJS(ternSecureMiddleware_exports);
|
|
25
26
|
var import_server = require("next/server");
|
|
26
|
-
|
|
27
|
+
const runtime = "edge";
|
|
27
28
|
function createRouteMatcher(patterns) {
|
|
28
29
|
return (request) => {
|
|
29
30
|
const { pathname } = request.nextUrl;
|
|
30
31
|
return patterns.some((pattern) => {
|
|
31
|
-
const regexPattern = new RegExp(
|
|
32
|
-
`^${pattern.replace(/\*/g, ".*").replace(/\((.*)\)/, "(?:$1)?")}$`
|
|
33
|
-
);
|
|
32
|
+
const regexPattern = new RegExp(`^${pattern.replace(/\*/g, ".*").replace(/$$(.*)$$/, "(?:$1)?")}$`);
|
|
34
33
|
return regexPattern.test(pathname);
|
|
35
34
|
});
|
|
36
35
|
};
|
|
@@ -48,35 +47,20 @@ function ternSecureMiddleware(callback) {
|
|
|
48
47
|
if (currentPath !== "/sign-in") {
|
|
49
48
|
const redirectUrl = new URL("/sign-in", request.url);
|
|
50
49
|
redirectUrl.searchParams.set("redirect", currentPath);
|
|
51
|
-
|
|
52
|
-
} else {
|
|
53
|
-
throw new Error("UNAUTHENTICATED");
|
|
50
|
+
return import_server.NextResponse.redirect(redirectUrl);
|
|
54
51
|
}
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
54
|
};
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
await callback(auth, request);
|
|
63
|
-
return import_server.NextResponse.next();
|
|
64
|
-
} catch (error) {
|
|
65
|
-
if (error instanceof Error && error.message === "Unauthorized access") {
|
|
66
|
-
console.log("middleware: Unauthorized access, redirecting to sign-in");
|
|
67
|
-
return import_server.NextResponse.redirect(error.message);
|
|
55
|
+
if (callback) {
|
|
56
|
+
const result = await callback(auth, request);
|
|
57
|
+
if (result instanceof Response) {
|
|
58
|
+
return result;
|
|
68
59
|
}
|
|
69
|
-
throw error;
|
|
70
60
|
}
|
|
61
|
+
return import_server.NextResponse.next();
|
|
71
62
|
} catch (error) {
|
|
72
|
-
console.error("Middleware error:",
|
|
73
|
-
error: error instanceof Error ? {
|
|
74
|
-
name: error.name,
|
|
75
|
-
message: error.message,
|
|
76
|
-
stack: error.stack
|
|
77
|
-
} : error,
|
|
78
|
-
path: request.nextUrl.pathname
|
|
79
|
-
});
|
|
63
|
+
console.error("Middleware error:", error);
|
|
80
64
|
return import_server.NextResponse.redirect(new URL("/sign-in", request.url));
|
|
81
65
|
}
|
|
82
66
|
};
|
|
@@ -84,6 +68,7 @@ function ternSecureMiddleware(callback) {
|
|
|
84
68
|
// Annotate the CommonJS export names for ESM import in node:
|
|
85
69
|
0 && (module.exports = {
|
|
86
70
|
createRouteMatcher,
|
|
71
|
+
runtime,
|
|
87
72
|
ternSecureMiddleware
|
|
88
73
|
});
|
|
89
74
|
//# sourceMappingURL=ternSecureMiddleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/ternSecureMiddleware.ts"],"sourcesContent":["import { type NextRequest, NextResponse } from 'next/server';\nimport type { UserInfo } from './types'\
|
|
1
|
+
{"version":3,"sources":["../../../src/server/ternSecureMiddleware.ts"],"sourcesContent":["import { type NextRequest, NextResponse } from 'next/server';\nimport type { UserInfo } from './types'\n\nexport const runtime = \"edge\"\n\ninterface Auth {\n user: UserInfo | null\n sessionId: string | null\n protect: () => Promise<Response | undefined>\n}\n\ntype MiddlewareCallback = (\n auth: Auth,\n request: NextRequest\n) => Promise<Response | undefined>\n\n\n/**\n * Create a route matcher function for public paths\n */\nexport function createRouteMatcher(patterns: string[]) {\n return (request: NextRequest): boolean => {\n const { pathname } = request.nextUrl\n return patterns.some((pattern) => {\n // Convert route pattern to regex\n const regexPattern = new RegExp(`^${pattern.replace(/\\*/g, \".*\").replace(/$$(.*)$$/, \"(?:$1)?\")}$`)\n return regexPattern.test(pathname)\n })\n }\n}\n\n\n/**\n * Middleware factory that handles authentication and custom logic\n * @param customHandler Optional function for additional custom logic\n */\n\nexport function ternSecureMiddleware(callback?: MiddlewareCallback) {\n return async function middleware(request: NextRequest) {\n try {\n\n const hasCookies = request.cookies.has('_session_cookie') || request.cookies.has('_session_token')\n\n const auth: Auth = {\n user: null,\n sessionId: null,\n protect: async () => {\n if (!hasCookies) {\n const currentPath = request.nextUrl.pathname\n if (currentPath !== '/sign-in') {\n const redirectUrl = new URL('/sign-in', request.url)\n redirectUrl.searchParams.set('redirect', currentPath)\n return NextResponse.redirect(redirectUrl)\n }\n }\n },\n }\n\n //if (!callback) {\n // return NextResponse.next()\n // }\n\n if (callback){\n const result = await callback(auth, request)\n if (result instanceof Response) {\n return result\n }\n }\n\n\n // Continue to the next middleware or route handler\n return NextResponse.next()\n } catch (error) {\n console.error(\"Middleware error:\", error)\n return NextResponse.redirect(new URL('/sign-in', request.url))\n }\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAGxC,MAAM,UAAU;AAiBhB,SAAS,mBAAmB,UAAoB;AACrD,SAAO,CAAC,YAAkC;AACxC,UAAM,EAAE,SAAS,IAAI,QAAQ;AAC7B,WAAO,SAAS,KAAK,CAAC,YAAY;AAEhC,YAAM,eAAe,IAAI,OAAO,IAAI,QAAQ,QAAQ,OAAO,IAAI,EAAE,QAAQ,YAAY,SAAS,CAAC,GAAG;AAClG,aAAO,aAAa,KAAK,QAAQ;AAAA,IACnC,CAAC;AAAA,EACH;AACF;AAQO,SAAS,qBAAqB,UAA+B;AAClE,SAAO,eAAe,WAAW,SAAsB;AACrD,QAAI;AAEF,YAAM,aAAa,QAAQ,QAAQ,IAAI,iBAAiB,KAAK,QAAQ,QAAQ,IAAI,gBAAgB;AAEjG,YAAM,OAAa;AAAA,QACjB,MAAM;AAAA,QACN,WAAW;AAAA,QACX,SAAS,YAAY;AACnB,cAAI,CAAC,YAAY;AACf,kBAAM,cAAc,QAAQ,QAAQ;AACpC,gBAAI,gBAAgB,YAAY;AAC9B,oBAAM,cAAc,IAAI,IAAI,YAAY,QAAQ,GAAG;AACnD,0BAAY,aAAa,IAAI,YAAY,WAAW;AACpD,qBAAO,2BAAa,SAAS,WAAW;AAAA,YAC1C;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAMF,UAAI,UAAS;AACT,cAAM,SAAS,MAAM,SAAS,MAAM,OAAO;AAC3C,YAAI,kBAAkB,UAAU;AAC9B,iBAAO;AAAA,QACT;AAAA,MACF;AAIA,aAAO,2BAAa,KAAK;AAAA,IAC3B,SAAS,OAAO;AACd,cAAQ,MAAM,qBAAqB,KAAK;AACxC,aAAO,2BAAa,SAAS,IAAI,IAAI,YAAY,QAAQ,GAAG,CAAC;AAAA,IAC/D;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { NextResponse } from "next/server";
|
|
2
|
-
|
|
2
|
+
const runtime = "edge";
|
|
3
3
|
function createRouteMatcher(patterns) {
|
|
4
4
|
return (request) => {
|
|
5
5
|
const { pathname } = request.nextUrl;
|
|
6
6
|
return patterns.some((pattern) => {
|
|
7
|
-
const regexPattern = new RegExp(
|
|
8
|
-
`^${pattern.replace(/\*/g, ".*").replace(/\((.*)\)/, "(?:$1)?")}$`
|
|
9
|
-
);
|
|
7
|
+
const regexPattern = new RegExp(`^${pattern.replace(/\*/g, ".*").replace(/$$(.*)$$/, "(?:$1)?")}$`);
|
|
10
8
|
return regexPattern.test(pathname);
|
|
11
9
|
});
|
|
12
10
|
};
|
|
@@ -24,41 +22,27 @@ function ternSecureMiddleware(callback) {
|
|
|
24
22
|
if (currentPath !== "/sign-in") {
|
|
25
23
|
const redirectUrl = new URL("/sign-in", request.url);
|
|
26
24
|
redirectUrl.searchParams.set("redirect", currentPath);
|
|
27
|
-
|
|
28
|
-
} else {
|
|
29
|
-
throw new Error("UNAUTHENTICATED");
|
|
25
|
+
return NextResponse.redirect(redirectUrl);
|
|
30
26
|
}
|
|
31
27
|
}
|
|
32
28
|
}
|
|
33
29
|
};
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
await callback(auth, request);
|
|
39
|
-
return NextResponse.next();
|
|
40
|
-
} catch (error) {
|
|
41
|
-
if (error instanceof Error && error.message === "Unauthorized access") {
|
|
42
|
-
console.log("middleware: Unauthorized access, redirecting to sign-in");
|
|
43
|
-
return NextResponse.redirect(error.message);
|
|
30
|
+
if (callback) {
|
|
31
|
+
const result = await callback(auth, request);
|
|
32
|
+
if (result instanceof Response) {
|
|
33
|
+
return result;
|
|
44
34
|
}
|
|
45
|
-
throw error;
|
|
46
35
|
}
|
|
36
|
+
return NextResponse.next();
|
|
47
37
|
} catch (error) {
|
|
48
|
-
console.error("Middleware error:",
|
|
49
|
-
error: error instanceof Error ? {
|
|
50
|
-
name: error.name,
|
|
51
|
-
message: error.message,
|
|
52
|
-
stack: error.stack
|
|
53
|
-
} : error,
|
|
54
|
-
path: request.nextUrl.pathname
|
|
55
|
-
});
|
|
38
|
+
console.error("Middleware error:", error);
|
|
56
39
|
return NextResponse.redirect(new URL("/sign-in", request.url));
|
|
57
40
|
}
|
|
58
41
|
};
|
|
59
42
|
}
|
|
60
43
|
export {
|
|
61
44
|
createRouteMatcher,
|
|
45
|
+
runtime,
|
|
62
46
|
ternSecureMiddleware
|
|
63
47
|
};
|
|
64
48
|
//# sourceMappingURL=ternSecureMiddleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/ternSecureMiddleware.ts"],"sourcesContent":["import { type NextRequest, NextResponse } from 'next/server';\nimport type { UserInfo } from './types'\
|
|
1
|
+
{"version":3,"sources":["../../../src/server/ternSecureMiddleware.ts"],"sourcesContent":["import { type NextRequest, NextResponse } from 'next/server';\nimport type { UserInfo } from './types'\n\nexport const runtime = \"edge\"\n\ninterface Auth {\n user: UserInfo | null\n sessionId: string | null\n protect: () => Promise<Response | undefined>\n}\n\ntype MiddlewareCallback = (\n auth: Auth,\n request: NextRequest\n) => Promise<Response | undefined>\n\n\n/**\n * Create a route matcher function for public paths\n */\nexport function createRouteMatcher(patterns: string[]) {\n return (request: NextRequest): boolean => {\n const { pathname } = request.nextUrl\n return patterns.some((pattern) => {\n // Convert route pattern to regex\n const regexPattern = new RegExp(`^${pattern.replace(/\\*/g, \".*\").replace(/$$(.*)$$/, \"(?:$1)?\")}$`)\n return regexPattern.test(pathname)\n })\n }\n}\n\n\n/**\n * Middleware factory that handles authentication and custom logic\n * @param customHandler Optional function for additional custom logic\n */\n\nexport function ternSecureMiddleware(callback?: MiddlewareCallback) {\n return async function middleware(request: NextRequest) {\n try {\n\n const hasCookies = request.cookies.has('_session_cookie') || request.cookies.has('_session_token')\n\n const auth: Auth = {\n user: null,\n sessionId: null,\n protect: async () => {\n if (!hasCookies) {\n const currentPath = request.nextUrl.pathname\n if (currentPath !== '/sign-in') {\n const redirectUrl = new URL('/sign-in', request.url)\n redirectUrl.searchParams.set('redirect', currentPath)\n return NextResponse.redirect(redirectUrl)\n }\n }\n },\n }\n\n //if (!callback) {\n // return NextResponse.next()\n // }\n\n if (callback){\n const result = await callback(auth, request)\n if (result instanceof Response) {\n return result\n }\n }\n\n\n // Continue to the next middleware or route handler\n return NextResponse.next()\n } catch (error) {\n console.error(\"Middleware error:\", error)\n return NextResponse.redirect(new URL('/sign-in', request.url))\n }\n }\n}"],"mappings":"AAAA,SAA2B,oBAAoB;AAGxC,MAAM,UAAU;AAiBhB,SAAS,mBAAmB,UAAoB;AACrD,SAAO,CAAC,YAAkC;AACxC,UAAM,EAAE,SAAS,IAAI,QAAQ;AAC7B,WAAO,SAAS,KAAK,CAAC,YAAY;AAEhC,YAAM,eAAe,IAAI,OAAO,IAAI,QAAQ,QAAQ,OAAO,IAAI,EAAE,QAAQ,YAAY,SAAS,CAAC,GAAG;AAClG,aAAO,aAAa,KAAK,QAAQ;AAAA,IACnC,CAAC;AAAA,EACH;AACF;AAQO,SAAS,qBAAqB,UAA+B;AAClE,SAAO,eAAe,WAAW,SAAsB;AACrD,QAAI;AAEF,YAAM,aAAa,QAAQ,QAAQ,IAAI,iBAAiB,KAAK,QAAQ,QAAQ,IAAI,gBAAgB;AAEjG,YAAM,OAAa;AAAA,QACjB,MAAM;AAAA,QACN,WAAW;AAAA,QACX,SAAS,YAAY;AACnB,cAAI,CAAC,YAAY;AACf,kBAAM,cAAc,QAAQ,QAAQ;AACpC,gBAAI,gBAAgB,YAAY;AAC9B,oBAAM,cAAc,IAAI,IAAI,YAAY,QAAQ,GAAG;AACnD,0BAAY,aAAa,IAAI,YAAY,WAAW;AACpD,qBAAO,aAAa,SAAS,WAAW;AAAA,YAC1C;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAMF,UAAI,UAAS;AACT,cAAM,SAAS,MAAM,SAAS,MAAM,OAAO;AAC3C,YAAI,kBAAkB,UAAU;AAC9B,iBAAO;AAAA,QACT;AAAA,MACF;AAIA,aAAO,aAAa,KAAK;AAAA,IAC3B,SAAS,OAAO;AACd,cAAQ,MAAM,qBAAqB,KAAK;AACxC,aAAO,aAAa,SAAS,IAAI,IAAI,YAAY,QAAQ,GAAG,CAAC;AAAA,IAC/D;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { type NextRequest
|
|
1
|
+
import { type NextRequest } from 'next/server';
|
|
2
2
|
import type { UserInfo } from './types';
|
|
3
|
+
export declare const runtime = "edge";
|
|
3
4
|
interface Auth {
|
|
4
5
|
user: UserInfo | null;
|
|
5
6
|
sessionId: string | null;
|
|
6
|
-
protect: () => Promise<
|
|
7
|
+
protect: () => Promise<Response | undefined>;
|
|
7
8
|
}
|
|
8
|
-
type MiddlewareCallback = (auth: Auth, request: NextRequest) => Promise<
|
|
9
|
+
type MiddlewareCallback = (auth: Auth, request: NextRequest) => Promise<Response | undefined>;
|
|
9
10
|
/**
|
|
10
11
|
* Create a route matcher function for public paths
|
|
11
12
|
*/
|
|
@@ -14,6 +15,6 @@ export declare function createRouteMatcher(patterns: string[]): (request: NextRe
|
|
|
14
15
|
* Middleware factory that handles authentication and custom logic
|
|
15
16
|
* @param customHandler Optional function for additional custom logic
|
|
16
17
|
*/
|
|
17
|
-
export declare function ternSecureMiddleware(callback
|
|
18
|
+
export declare function ternSecureMiddleware(callback?: MiddlewareCallback): (request: NextRequest) => Promise<Response>;
|
|
18
19
|
export {};
|
|
19
20
|
//# sourceMappingURL=ternSecureMiddleware.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ternSecureMiddleware.d.ts","sourceRoot":"","sources":["../../../src/server/ternSecureMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,
|
|
1
|
+
{"version":3,"file":"ternSecureMiddleware.d.ts","sourceRoot":"","sources":["../../../src/server/ternSecureMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAgB,MAAM,aAAa,CAAC;AAC7D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAEvC,eAAO,MAAM,OAAO,SAAS,CAAA;AAE7B,UAAU,IAAI;IACZ,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAA;IACrB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,OAAO,EAAE,MAAM,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAA;CAC7C;AAED,KAAK,kBAAkB,GAAG,CACxB,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,WAAW,KACjB,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAA;AAGlC;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,aAClC,WAAW,KAAG,OAAO,CAQvC;AAGD;;;GAGG;AAEH,wBAAgB,oBAAoB,CAAC,QAAQ,CAAC,EAAE,kBAAkB,aACtB,WAAW,uBAuCtD"}
|