jcc-express-mvc 1.6.5 → 1.6.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/Core/Inertia.d.ts +1 -1
- package/Core/Inertia.d.ts.map +1 -1
- package/Core/Inertia.js +1 -1
- package/index.d.ts +2 -2
- package/index.d.ts.map +1 -1
- package/lib/Auth/AuthMiddleware.d.ts +3 -2
- package/lib/Auth/AuthMiddleware.d.ts.map +1 -1
- package/lib/Auth/AuthMiddleware.js +36 -40
- package/lib/Auth/index.d.ts +13 -12
- package/lib/Auth/index.d.ts.map +1 -1
- package/lib/Auth/index.js +87 -71
- package/lib/Cache/FileCacheDriver.d.ts +24 -0
- package/lib/Cache/FileCacheDriver.d.ts.map +1 -0
- package/lib/Cache/FileCacheDriver.js +162 -0
- package/lib/Cache/MemoryDriver.d.ts +15 -0
- package/lib/Cache/MemoryDriver.d.ts.map +1 -0
- package/lib/Cache/MemoryDriver.js +56 -0
- package/lib/Cache/RedisDriver.d.ts +22 -0
- package/lib/Cache/RedisDriver.d.ts.map +1 -0
- package/lib/Cache/RedisDriver.js +88 -0
- package/lib/Cache/TaggedCache.d.ts +12 -0
- package/lib/Cache/TaggedCache.d.ts.map +1 -0
- package/lib/Cache/TaggedCache.js +42 -0
- package/lib/Cache/index.d.ts +40 -0
- package/lib/Cache/index.d.ts.map +1 -0
- package/lib/Cache/index.js +148 -0
- package/lib/Container/index.d.ts +14 -5
- package/lib/Container/index.d.ts.map +1 -1
- package/lib/Container/index.js +65 -39
- package/lib/Error/AppError.d.ts +8 -4
- package/lib/Error/AppError.d.ts.map +1 -1
- package/lib/Error/AppError.js +29 -2
- package/lib/Error/AppErrorHandler.d.ts.map +1 -1
- package/lib/Error/AppErrorHandler.js +2 -5
- package/lib/Error/DisplayErrorCode.d.ts +1 -1
- package/lib/Error/DisplayErrorCode.d.ts.map +1 -1
- package/lib/Error/DisplayErrorCode.js +2 -1
- package/lib/Error/ValidationException.d.ts +6 -0
- package/lib/Error/ValidationException.d.ts.map +1 -0
- package/lib/Error/ValidationException.js +12 -0
- package/lib/Http/index.d.ts +5 -5
- package/lib/Inertia/index.d.ts +2 -6
- package/lib/Inertia/index.d.ts.map +1 -1
- package/lib/Inertia/index.js +67 -23
- package/lib/Inertia/{newer.d.ts → old-index.d.ts} +2 -4
- package/lib/Inertia/old-index.d.ts.map +1 -0
- package/lib/Inertia/old-index.js +55 -0
- package/lib/Interface/index.d.ts +181 -8
- package/lib/Interface/index.d.ts.map +1 -1
- package/lib/Middleware/index.d.ts +0 -1
- package/lib/Middleware/index.d.ts.map +1 -1
- package/lib/Middleware/index.js +8 -9
- package/lib/Request/request.d.ts +60 -0
- package/lib/Request/request.d.ts.map +1 -1
- package/lib/Request/request.js +101 -1
- package/lib/Response/index.d.ts.map +1 -1
- package/lib/Response/index.js +5 -3
- package/lib/Session/DatabaseSession.d.ts +11 -0
- package/lib/Session/DatabaseSession.d.ts.map +1 -0
- package/lib/Session/DatabaseSession.js +82 -0
- package/lib/Session/FileSession.d.ts +13 -0
- package/lib/Session/FileSession.d.ts.map +1 -0
- package/lib/Session/FileSession.js +114 -0
- package/lib/Session/RedisSession.d.ts +13 -0
- package/lib/Session/RedisSession.d.ts.map +1 -0
- package/lib/Session/RedisSession.js +75 -0
- package/lib/Session/SessionManager.d.ts +12 -0
- package/lib/Session/SessionManager.d.ts.map +1 -0
- package/lib/Session/SessionManager.js +93 -0
- package/lib/Session/SessionMiddleware.d.ts +6 -0
- package/lib/Session/SessionMiddleware.d.ts.map +1 -0
- package/lib/Session/SessionMiddleware.js +27 -0
- package/lib/Session/index.d.ts +24 -0
- package/lib/Session/index.d.ts.map +1 -0
- package/lib/Session/index.js +74 -0
- package/lib/Storage/index.d.ts +51 -0
- package/lib/Storage/index.d.ts.map +1 -0
- package/lib/Storage/index.js +301 -0
- package/lib/Templating-engine/index.d.ts.map +1 -1
- package/lib/Templating-engine/index.js +4 -1
- package/lib/Type/index.d.ts +4 -2
- package/lib/Type/index.d.ts.map +1 -1
- package/lib/util/index.d.ts +6 -4
- package/lib/util/index.d.ts.map +1 -1
- package/lib/util/index.js +26 -7
- package/package.json +1 -1
- package/lib/Inertia/new.d.ts +0 -10
- package/lib/Inertia/new.d.ts.map +0 -1
- package/lib/Inertia/new.js +0 -150
- package/lib/Inertia/newer.d.ts.map +0 -1
- package/lib/Inertia/newer.js +0 -168
package/Core/Inertia.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "../lib/Inertia
|
|
1
|
+
export * from "../lib/Inertia";
|
|
2
2
|
//# sourceMappingURL=Inertia.d.ts.map
|
package/Core/Inertia.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Inertia.d.ts","sourceRoot":"","sources":["../../Framework/Core/Inertia.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"Inertia.d.ts","sourceRoot":"","sources":["../../Framework/Core/Inertia.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
package/Core/Inertia.js
CHANGED
|
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("../lib/Inertia
|
|
17
|
+
__exportStar(require("../lib/Inertia"), exports);
|
package/index.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ export { config } from "./lib/Config/Config";
|
|
|
3
3
|
export type { AppRequest as Request, AppResponse as Response, AppNext as Next, } from "./lib/Interface";
|
|
4
4
|
export { bcrypt, verifyHash, jwtSign, jwtVerify, saveImage, asyncHandler, cloudinaryUpload, } from "./lib/util";
|
|
5
5
|
export declare const guest: (req: import("./lib/Interface").AppRequest, res: import("./lib/Interface").AppResponse, next: import("./lib/Interface").AppNext) => any;
|
|
6
|
-
export declare const apiAuth: (
|
|
7
|
-
export declare const auth: (
|
|
6
|
+
export declare const apiAuth: (req: import("./lib/Interface").AppRequest, res: import("./lib/Interface").AppResponse, next: import("./lib/Interface").AppNext) => Promise<import("./lib/Interface").AppResponse | undefined>;
|
|
7
|
+
export declare const auth: (req: import("./lib/Interface").AppRequest, res: import("./lib/Interface").AppResponse, next: import("./lib/Interface").AppNext) => Promise<void>;
|
|
8
8
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../Framework/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,IAAI,IAAI,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,YAAY,EACV,UAAU,IAAI,OAAO,EACrB,WAAW,IAAI,QAAQ,EACvB,OAAO,IAAI,IAAI,GAChB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,MAAM,EACN,UAAU,EACV,OAAO,EACP,SAAS,EACT,SAAS,EACT,YAAY,EACZ,gBAAgB,GACjB,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,KAAK,yIAAuB,CAAC;AAC1C,eAAO,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../Framework/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,IAAI,IAAI,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,YAAY,EACV,UAAU,IAAI,OAAO,EACrB,WAAW,IAAI,QAAQ,EACvB,OAAO,IAAI,IAAI,GAChB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,MAAM,EACN,UAAU,EACV,OAAO,EACP,SAAS,EACT,SAAS,EACT,YAAY,EACZ,gBAAgB,GACjB,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,KAAK,yIAAuB,CAAC;AAC1C,eAAO,MAAM,OAAO,gMAAyB,CAAC;AAC9C,eAAO,MAAM,IAAI,mJAAsB,CAAC"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { AppNext, AppRequest, AppResponse } from "../Interface";
|
|
2
2
|
declare class AuthMiddleware {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/** Middleware: API authentication */
|
|
4
|
+
apiAuth(req: AppRequest, res: AppResponse, next: AppNext): Promise<AppResponse | undefined>;
|
|
5
|
+
auth(req: AppRequest, res: AppResponse, next: AppNext): Promise<void>;
|
|
5
6
|
guest(req: AppRequest, res: AppResponse, next: AppNext): any;
|
|
6
7
|
}
|
|
7
8
|
export declare const authMiddleware: AuthMiddleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthMiddleware.d.ts","sourceRoot":"","sources":["../../../Framework/lib/Auth/AuthMiddleware.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AuthMiddleware.d.ts","sourceRoot":"","sources":["../../../Framework/lib/Auth/AuthMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAIhE,cAAM,cAAc;IAClB,qCAAqC;IACxB,OAAO,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO;IAuBxD,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO;IAuB3D,KAAK,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO;CAK9D;AAED,eAAO,MAAM,cAAc,gBAAuB,CAAC"}
|
|
@@ -1,60 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.authMiddleware = void 0;
|
|
4
|
+
const Config_1 = require("../Config/Config");
|
|
4
5
|
const util_1 = require("../util");
|
|
5
6
|
const { User } = (0, util_1.getModel)("User");
|
|
6
7
|
class AuthMiddleware {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
else if (request.cookies.token) {
|
|
14
|
-
token = request.cookies.token;
|
|
15
|
-
}
|
|
16
|
-
else if (request.header("x-auth-token")) {
|
|
17
|
-
token = request.header("x-auth-token");
|
|
18
|
-
}
|
|
19
|
-
if (!token) {
|
|
20
|
-
return response.json({ message: "Not authorize" }).status(401);
|
|
21
|
-
}
|
|
8
|
+
/** Middleware: API authentication */
|
|
9
|
+
async apiAuth(req, res, next) {
|
|
10
|
+
const token = req.headers.authorization?.split(" ")[1] || req.cookies.auth_token;
|
|
11
|
+
if (!token)
|
|
12
|
+
return res.status(401).json({ message: "Not authorized" });
|
|
22
13
|
try {
|
|
23
|
-
// request.id = jwtVerify(token);
|
|
24
14
|
const id = (0, util_1.jwtVerify)(token);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
user = await User.where("id", id).first();
|
|
33
|
-
}
|
|
34
|
-
request.user = user;
|
|
15
|
+
const user = Config_1.config.get("DB_ORM") === "mongodb"
|
|
16
|
+
? await User.findById(id)
|
|
17
|
+
: await User.where("id", id).first();
|
|
18
|
+
if (!user)
|
|
19
|
+
return res.status(401).json({ message: "Not authorized" });
|
|
20
|
+
req.user = user;
|
|
21
|
+
req.id = id;
|
|
35
22
|
next();
|
|
36
23
|
}
|
|
37
24
|
catch (err) {
|
|
38
|
-
|
|
39
|
-
return response.json({ message: "Not authorize" }).status(401);
|
|
25
|
+
return res.status(401).json({ message: "Not authorized" });
|
|
40
26
|
}
|
|
41
27
|
}
|
|
42
|
-
auth(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
28
|
+
async auth(req, res, next) {
|
|
29
|
+
const token = req.cookies.auth_token;
|
|
30
|
+
if (!token)
|
|
31
|
+
return res.redirect(`/login?redirect=${req.url || "/"}`);
|
|
32
|
+
try {
|
|
33
|
+
const payload = (0, util_1.jwtVerify)(token);
|
|
34
|
+
const user = await (0, util_1.findUserById)(User, payload.id);
|
|
35
|
+
if (!user) {
|
|
36
|
+
res.clearCookie("auth_token");
|
|
37
|
+
res.clearCookie("refresh_token");
|
|
38
|
+
return res.redirect(`/login?redirect=${req.url || "/"}`);
|
|
39
|
+
}
|
|
40
|
+
req.user = user;
|
|
41
|
+
res.locals.Auth = user;
|
|
42
|
+
next();
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
res.clearCookie("auth_token");
|
|
46
|
+
res.clearCookie("refresh_token");
|
|
47
|
+
return res.redirect(`/login?redirect=${req.url || "/"}`);
|
|
49
48
|
}
|
|
50
|
-
return request.isAuthenticated()
|
|
51
|
-
? next()
|
|
52
|
-
: response.redirect(url ? `/login?redirect=${url}` : "/login");
|
|
53
49
|
}
|
|
54
50
|
guest(req, res, next) {
|
|
55
|
-
|
|
51
|
+
const token = req.cookies.auth_token;
|
|
52
|
+
if (token)
|
|
56
53
|
return res.redirectBack();
|
|
57
|
-
}
|
|
58
54
|
return next();
|
|
59
55
|
}
|
|
60
56
|
}
|
package/lib/Auth/index.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
declare module "express-session" {
|
|
3
|
-
interface SessionData {
|
|
4
|
-
appRedirect?: string;
|
|
5
|
-
isLoggedIn?: boolean;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
1
|
+
import { AppRequest, AppResponse, AppNext } from "../Interface";
|
|
8
2
|
export declare class Authentication {
|
|
9
|
-
|
|
10
|
-
static
|
|
11
|
-
|
|
12
|
-
static
|
|
13
|
-
|
|
3
|
+
/** Get user lookup field (email, phone, username) */
|
|
4
|
+
private static getCredentials;
|
|
5
|
+
/** Fetch user from DB (MongoDB, Sequelize, or JCC ORM) */
|
|
6
|
+
private static getUser;
|
|
7
|
+
/** Generate and attach tokens to cookies */
|
|
8
|
+
private static setTokens;
|
|
9
|
+
/** Handle user login attempt */
|
|
10
|
+
static attempt: (req: AppRequest, res: AppResponse, next: AppNext) => Promise<void | AppResponse>;
|
|
11
|
+
/** Refresh token middleware */
|
|
12
|
+
static refreshToken(req: AppRequest, res: AppResponse, next: AppNext): Promise<AppResponse | undefined>;
|
|
13
|
+
/** Logout handler */
|
|
14
|
+
static logout(req: AppRequest, res: AppResponse): void;
|
|
14
15
|
}
|
|
15
16
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/Auth/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../Framework/lib/Auth/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../Framework/lib/Auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAahE,qBAAa,cAAc;IACzB,qDAAqD;IACrD,OAAO,CAAC,MAAM,CAAC,cAAc;IAc7B,0DAA0D;mBACrC,OAAO;IAuB5B,4CAA4C;IAC5C,OAAO,CAAC,MAAM,CAAC,SAAS;IAuBxB,gCAAgC;IAChC,MAAM,CAAC,OAAO,GAAU,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,OAAO,iCAsBtE;IAEF,+BAA+B;WAClB,YAAY,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO;IAmB1E,qBAAqB;IACrB,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW;CAKhD"}
|
package/lib/Auth/index.js
CHANGED
|
@@ -1,93 +1,109 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
2
|
+
var _a;
|
|
5
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
4
|
exports.Authentication = void 0;
|
|
7
|
-
const passport_1 = __importDefault(require("passport"));
|
|
8
5
|
const util_1 = require("../util");
|
|
6
|
+
const Config_1 = require("../Config/Config");
|
|
7
|
+
const ValidationException_1 = require("../Error/ValidationException");
|
|
9
8
|
const { User } = (0, util_1.getModel)("User");
|
|
10
9
|
class Authentication {
|
|
11
|
-
|
|
12
|
-
static
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
true ? user : false;
|
|
10
|
+
/** Get user lookup field (email, phone, username) */
|
|
11
|
+
static getCredentials(data) {
|
|
12
|
+
const query = {};
|
|
13
|
+
if (data.email) {
|
|
14
|
+
query.email = data.email;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
else if (data.phone) {
|
|
17
|
+
query.phone = data.phone;
|
|
22
18
|
}
|
|
19
|
+
else if (data.username) {
|
|
20
|
+
query.username = data.username;
|
|
21
|
+
}
|
|
22
|
+
return query;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
static async
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
!user.roleType ||
|
|
34
|
-
!user.isAdmin ||
|
|
35
|
-
!(user.role === "admin" ||
|
|
36
|
-
user.role === "Admin" ||
|
|
37
|
-
user.roleType === "admin" ||
|
|
38
|
-
user.roleType === "Admin" ||
|
|
39
|
-
user.isAdmin === true ||
|
|
40
|
-
user.isAdmin === "admin" ||
|
|
41
|
-
user.isAdmin === "Admin")) {
|
|
42
|
-
return res.status(403).json({ message: "Not authorized" });
|
|
43
|
-
}
|
|
44
|
-
next();
|
|
24
|
+
/** Fetch user from DB (MongoDB, Sequelize, or JCC ORM) */
|
|
25
|
+
static async getUser(data) {
|
|
26
|
+
const field = this.getCredentials(data);
|
|
27
|
+
if (!Object.keys(field).length)
|
|
28
|
+
return { user: null, field: "email" };
|
|
29
|
+
let user = null;
|
|
30
|
+
const orm = Config_1.config.get("DB_ORM");
|
|
31
|
+
if (orm === "mongodb") {
|
|
32
|
+
user = await User.findOne(field).select("+password");
|
|
45
33
|
}
|
|
46
|
-
|
|
47
|
-
|
|
34
|
+
else if (orm === "sequelize") {
|
|
35
|
+
user = await User.findOne({ where: field });
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
// JCC ORM
|
|
39
|
+
User.hidden = User.hidden.filter((v) => v !== "password");
|
|
40
|
+
user = await User.where(Object.keys(field)[0], Object.values(field)[0]).first();
|
|
48
41
|
}
|
|
42
|
+
return { user, field: Object.keys(field)[0] || "email" };
|
|
43
|
+
}
|
|
44
|
+
/** Generate and attach tokens to cookies */
|
|
45
|
+
static setTokens(res, userId) {
|
|
46
|
+
const accessToken = (0, util_1.jwtSign)(userId.toString(), { expiresIn: "1h" });
|
|
47
|
+
const refreshToken = (0, util_1.jwtSign)(userId.toString(), { expiresIn: "7d" });
|
|
48
|
+
const cookieOptions = {
|
|
49
|
+
httpOnly: true,
|
|
50
|
+
secure: Config_1.config.get("APP_ENV") === "production",
|
|
51
|
+
sameSite: "lax",
|
|
52
|
+
};
|
|
53
|
+
res.cookie("auth_token", accessToken, {
|
|
54
|
+
...cookieOptions,
|
|
55
|
+
maxAge: 1000 * 60 * 60, // 1 hour
|
|
56
|
+
});
|
|
57
|
+
res.cookie("refresh_token", refreshToken, {
|
|
58
|
+
...cookieOptions,
|
|
59
|
+
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
|
|
60
|
+
});
|
|
61
|
+
return { accessToken, refreshToken };
|
|
49
62
|
}
|
|
50
|
-
|
|
51
|
-
static async
|
|
63
|
+
/** Refresh token middleware */
|
|
64
|
+
static async refreshToken(req, res, next) {
|
|
52
65
|
try {
|
|
53
|
-
const
|
|
54
|
-
if (!
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
const refreshToken = req.cookies.refresh_token;
|
|
67
|
+
if (!refreshToken)
|
|
68
|
+
throw new Error("No refresh token");
|
|
69
|
+
const userId = (0, util_1.jwtVerify)(refreshToken);
|
|
70
|
+
this.setTokens(res, userId);
|
|
71
|
+
// Use universal finder
|
|
72
|
+
req.user = await (0, util_1.findUserById)(User, userId);
|
|
60
73
|
next();
|
|
61
74
|
}
|
|
62
75
|
catch (error) {
|
|
63
|
-
|
|
76
|
+
res.clearCookie("auth_token");
|
|
77
|
+
res.clearCookie("refresh_token");
|
|
78
|
+
return res.status(401).json({ message: "Unauthorized" });
|
|
64
79
|
}
|
|
65
80
|
}
|
|
66
|
-
|
|
67
|
-
static
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
81
|
+
/** Logout handler */
|
|
82
|
+
static logout(req, res) {
|
|
83
|
+
res.clearCookie("auth_token");
|
|
84
|
+
res.clearCookie("refresh_token");
|
|
85
|
+
return res.redirect("/login");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.Authentication = Authentication;
|
|
89
|
+
_a = Authentication;
|
|
90
|
+
/** Handle user login attempt */
|
|
91
|
+
Authentication.attempt = async (req, res, next) => {
|
|
92
|
+
try {
|
|
93
|
+
const { user, field } = await _a.getUser(req.body);
|
|
94
|
+
if (!user)
|
|
95
|
+
throw new ValidationException_1.ValidationException({ [field]: "Invalid credentials" }, req);
|
|
96
|
+
if (!(await (0, util_1.verifyHash)(req.body.password, user.password))) {
|
|
97
|
+
throw new ValidationException_1.ValidationException({ [field]: "Invalid credentials" }, req);
|
|
78
98
|
}
|
|
79
|
-
|
|
80
|
-
|
|
99
|
+
const tokens = _a.setTokens(res, user.id || user._id);
|
|
100
|
+
if (req.expectsJson() && !req.isInertia()) {
|
|
101
|
+
return res.status(200).json({ tokens, user });
|
|
81
102
|
}
|
|
103
|
+
const redirectTo = req.query.redirect?.toString() || "/home";
|
|
104
|
+
return res.redirect(303, redirectTo);
|
|
82
105
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (err) {
|
|
86
|
-
next(err);
|
|
87
|
-
}
|
|
88
|
-
return res.redirect(303, "/login");
|
|
89
|
-
});
|
|
106
|
+
catch (error) {
|
|
107
|
+
next(error);
|
|
90
108
|
}
|
|
91
|
-
}
|
|
92
|
-
exports.Authentication = Authentication;
|
|
93
|
-
// export default Authentication;
|
|
109
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CacheDriver } from "../Interface";
|
|
2
|
+
export declare class FileDriver implements CacheDriver {
|
|
3
|
+
private cacheDir;
|
|
4
|
+
constructor(config?: {
|
|
5
|
+
cacheDir?: string;
|
|
6
|
+
});
|
|
7
|
+
private ensureCacheDir;
|
|
8
|
+
private getFilePath;
|
|
9
|
+
private readCacheFile;
|
|
10
|
+
private writeCacheFile;
|
|
11
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
12
|
+
put<T = any>(key: string, value: T, seconds?: number): Promise<void>;
|
|
13
|
+
forget(key: string): Promise<boolean>;
|
|
14
|
+
flush(): Promise<void>;
|
|
15
|
+
forever<T = any>(key: string, value: T): Promise<void>;
|
|
16
|
+
increment(key: string, value?: number): Promise<number>;
|
|
17
|
+
decrement(key: string, value?: number): Promise<number>;
|
|
18
|
+
many(keys: string[]): Promise<Record<string, any>>;
|
|
19
|
+
putMany(values: Record<string, any>, seconds?: number): Promise<void>;
|
|
20
|
+
has(key: string): Promise<boolean>;
|
|
21
|
+
size(): Promise<number>;
|
|
22
|
+
cleanup(): Promise<number>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=FileCacheDriver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FileCacheDriver.d.ts","sourceRoot":"","sources":["../../../Framework/lib/Cache/FileCacheDriver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAK3C,qBAAa,UAAW,YAAW,WAAW;IAC5C,OAAO,CAAC,QAAQ,CAAS;gBAEb,MAAM,GAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAO;YAIhC,cAAc;IAQ5B,OAAO,CAAC,WAAW;YAML,aAAa;YAWb,cAAc;IAQtB,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAc5C,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMpE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAUrC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAetB,OAAO,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAKtD,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,GAAE,MAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAO1D,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,GAAE,MAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAI1D,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAUlD,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQrE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAKlC,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC;IASvB,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;CAoBjC"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.FileDriver = void 0;
|
|
37
|
+
const fs = __importStar(require("fs/promises"));
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
const crypto = __importStar(require("crypto"));
|
|
40
|
+
class FileDriver {
|
|
41
|
+
constructor(config = {}) {
|
|
42
|
+
this.cacheDir = config.cacheDir || path.join(process.cwd(), "cache");
|
|
43
|
+
}
|
|
44
|
+
async ensureCacheDir() {
|
|
45
|
+
try {
|
|
46
|
+
await fs.access(this.cacheDir);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
await fs.mkdir(this.cacheDir, { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
getFilePath(key) {
|
|
53
|
+
// Create a safe filename using hash to avoid filesystem issues
|
|
54
|
+
const hash = crypto.createHash("md5").update(key).digest("hex");
|
|
55
|
+
return path.join(this.cacheDir, `${hash}.cache`);
|
|
56
|
+
}
|
|
57
|
+
async readCacheFile(filePath) {
|
|
58
|
+
try {
|
|
59
|
+
const data = await fs.readFile(filePath, "utf8");
|
|
60
|
+
return JSON.parse(data);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async writeCacheFile(filePath, data) {
|
|
67
|
+
await this.ensureCacheDir();
|
|
68
|
+
await fs.writeFile(filePath, JSON.stringify(data), "utf8");
|
|
69
|
+
}
|
|
70
|
+
async get(key) {
|
|
71
|
+
const filePath = this.getFilePath(key);
|
|
72
|
+
const item = await this.readCacheFile(filePath);
|
|
73
|
+
if (!item)
|
|
74
|
+
return null;
|
|
75
|
+
if (item.expires && Date.now() > item.expires) {
|
|
76
|
+
await this.forget(key);
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return item.value;
|
|
80
|
+
}
|
|
81
|
+
async put(key, value, seconds) {
|
|
82
|
+
const filePath = this.getFilePath(key);
|
|
83
|
+
const expires = seconds ? Date.now() + seconds * 1000 : undefined;
|
|
84
|
+
await this.writeCacheFile(filePath, { value, expires });
|
|
85
|
+
}
|
|
86
|
+
async forget(key) {
|
|
87
|
+
const filePath = this.getFilePath(key);
|
|
88
|
+
try {
|
|
89
|
+
await fs.unlink(filePath);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async flush() {
|
|
97
|
+
try {
|
|
98
|
+
const files = await fs.readdir(this.cacheDir);
|
|
99
|
+
const cacheFiles = files.filter((file) => file.endsWith(".cache"));
|
|
100
|
+
await Promise.all(cacheFiles.map((file) => fs.unlink(path.join(this.cacheDir, file)).catch(() => { })));
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// Directory might not exist, which is fine
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async forever(key, value) {
|
|
107
|
+
const filePath = this.getFilePath(key);
|
|
108
|
+
await this.writeCacheFile(filePath, { value });
|
|
109
|
+
}
|
|
110
|
+
async increment(key, value = 1) {
|
|
111
|
+
const current = (await this.get(key)) || 0;
|
|
112
|
+
const newValue = current + value;
|
|
113
|
+
await this.put(key, newValue);
|
|
114
|
+
return newValue;
|
|
115
|
+
}
|
|
116
|
+
async decrement(key, value = 1) {
|
|
117
|
+
return this.increment(key, -value);
|
|
118
|
+
}
|
|
119
|
+
async many(keys) {
|
|
120
|
+
const result = {};
|
|
121
|
+
await Promise.all(keys.map(async (key) => {
|
|
122
|
+
result[key] = await this.get(key);
|
|
123
|
+
}));
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
async putMany(values, seconds) {
|
|
127
|
+
await Promise.all(Object.entries(values).map(([key, value]) => this.put(key, value, seconds)));
|
|
128
|
+
}
|
|
129
|
+
async has(key) {
|
|
130
|
+
return (await this.get(key)) !== null;
|
|
131
|
+
}
|
|
132
|
+
// Additional utility methods specific to file cache
|
|
133
|
+
async size() {
|
|
134
|
+
try {
|
|
135
|
+
const files = await fs.readdir(this.cacheDir);
|
|
136
|
+
return files.filter((file) => file.endsWith(".cache")).length;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return 0;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async cleanup() {
|
|
143
|
+
let cleaned = 0;
|
|
144
|
+
try {
|
|
145
|
+
const files = await fs.readdir(this.cacheDir);
|
|
146
|
+
const cacheFiles = files.filter((file) => file.endsWith(".cache"));
|
|
147
|
+
for (const file of cacheFiles) {
|
|
148
|
+
const filePath = path.join(this.cacheDir, file);
|
|
149
|
+
const item = await this.readCacheFile(filePath);
|
|
150
|
+
if (item?.expires && Date.now() > item.expires) {
|
|
151
|
+
await fs.unlink(filePath);
|
|
152
|
+
cleaned++;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// Directory might not exist
|
|
158
|
+
}
|
|
159
|
+
return cleaned;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
exports.FileDriver = FileDriver;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CacheDriver } from "../Interface";
|
|
2
|
+
export declare class MemoryDriver implements CacheDriver {
|
|
3
|
+
private cache;
|
|
4
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
5
|
+
put<T = any>(key: string, value: T, seconds?: number): Promise<void>;
|
|
6
|
+
forget(key: string): Promise<boolean>;
|
|
7
|
+
flush(): Promise<void>;
|
|
8
|
+
forever<T = any>(key: string, value: T): Promise<void>;
|
|
9
|
+
increment(key: string, value?: number): Promise<number>;
|
|
10
|
+
decrement(key: string, value?: number): Promise<number>;
|
|
11
|
+
many(keys: string[]): Promise<Record<string, any>>;
|
|
12
|
+
putMany(values: Record<string, any>, seconds?: number): Promise<void>;
|
|
13
|
+
has(key: string): Promise<boolean>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=MemoryDriver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryDriver.d.ts","sourceRoot":"","sources":["../../../Framework/lib/Cache/MemoryDriver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,qBAAa,YAAa,YAAW,WAAW;IAC9C,OAAO,CAAC,KAAK,CAA4D;IAEnE,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAY5C,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKpE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIrC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,OAAO,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAItD,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,GAAE,MAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAO1D,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,GAAE,MAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAI1D,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAQlD,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMrE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAGzC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryDriver = void 0;
|
|
4
|
+
class MemoryDriver {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.cache = new Map();
|
|
7
|
+
}
|
|
8
|
+
async get(key) {
|
|
9
|
+
const item = this.cache.get(key);
|
|
10
|
+
if (!item)
|
|
11
|
+
return null;
|
|
12
|
+
if (item.expires && Date.now() > item.expires) {
|
|
13
|
+
this.cache.delete(key);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return item.value;
|
|
17
|
+
}
|
|
18
|
+
async put(key, value, seconds) {
|
|
19
|
+
const expires = seconds ? Date.now() + seconds * 1000 : undefined;
|
|
20
|
+
this.cache.set(key, { value, expires });
|
|
21
|
+
}
|
|
22
|
+
async forget(key) {
|
|
23
|
+
return this.cache.delete(key);
|
|
24
|
+
}
|
|
25
|
+
async flush() {
|
|
26
|
+
this.cache.clear();
|
|
27
|
+
}
|
|
28
|
+
async forever(key, value) {
|
|
29
|
+
this.cache.set(key, { value });
|
|
30
|
+
}
|
|
31
|
+
async increment(key, value = 1) {
|
|
32
|
+
const current = (await this.get(key)) || 0;
|
|
33
|
+
const newValue = current + value;
|
|
34
|
+
await this.put(key, newValue);
|
|
35
|
+
return newValue;
|
|
36
|
+
}
|
|
37
|
+
async decrement(key, value = 1) {
|
|
38
|
+
return this.increment(key, -value);
|
|
39
|
+
}
|
|
40
|
+
async many(keys) {
|
|
41
|
+
const result = {};
|
|
42
|
+
for (const key of keys) {
|
|
43
|
+
result[key] = await this.get(key);
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
async putMany(values, seconds) {
|
|
48
|
+
for (const [key, value] of Object.entries(values)) {
|
|
49
|
+
await this.put(key, value, seconds);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async has(key) {
|
|
53
|
+
return (await this.get(key)) !== null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.MemoryDriver = MemoryDriver;
|