speedly 1.2.13 → 1.2.17
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/auth/auth.d.ts +2 -19
- package/dist/cjs/auth/auth.js +27 -68
- package/dist/cjs/auth/auth2.d.ts +18 -0
- package/dist/cjs/auth/auth2.js +93 -0
- package/dist/cjs/auth/types.d.ts +19 -0
- package/dist/cjs/auth/types.js +2 -0
- package/dist/cjs/db/db.d.ts +2 -2
- package/dist/cjs/db/db.js +4 -7
- package/dist/cjs/index.js +5 -8
- package/dist/cjs/model/translation.js +1 -4
- package/dist/cjs/uploader/uploader.d.ts +1 -1
- package/dist/cjs/uploader/uploader.js +5 -8
- package/dist/cjs/util/getConfig.js +2 -5
- package/dist/cjs/util/translator.js +3 -6
- package/dist/esm/auth/auth.d.ts +2 -19
- package/dist/esm/auth/auth.js +26 -64
- package/dist/esm/auth/types.d.ts +19 -0
- package/dist/esm/auth/types.js +2 -0
- package/package.json +1 -1
package/dist/cjs/auth/auth.d.ts
CHANGED
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
type AdminArgs = [{
|
|
4
|
-
permission: string;
|
|
5
|
-
}, ...Handler[]] | Handler[];
|
|
6
|
-
interface UseAuth {
|
|
7
|
-
(req: Request, res: Response, next: NextFunction): any;
|
|
8
|
-
user?: (...handlers: Handler[]) => UseAuth;
|
|
9
|
-
admin?: (...handlers: AdminArgs) => UseAuth;
|
|
10
|
-
any?: (...handlers: Handler[]) => UseAuth;
|
|
11
|
-
}
|
|
12
|
-
declare const auth: (config?: {
|
|
13
|
-
admin: {
|
|
14
|
-
role: string;
|
|
15
|
-
model: string;
|
|
16
|
-
};
|
|
17
|
-
jwtSecretEnv: string;
|
|
18
|
-
customValidator: (req: Request, key: string) => boolean;
|
|
19
|
-
}) => UseAuth;
|
|
1
|
+
import * as Types from "./types";
|
|
2
|
+
declare const auth: Types.Auth;
|
|
20
3
|
export default auth;
|
package/dist/cjs/auth/auth.js
CHANGED
|
@@ -1,72 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const getConfig_1 =
|
|
7
|
-
const gConfig = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
next({ status: 405, json: { message: 'you don\'t have access to this section' } });
|
|
34
|
-
}
|
|
35
|
-
else
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
console.log('auth', 42, error);
|
|
41
|
-
next({ status: 403, json: { message: (error instanceof Error ? error.message : 'error on authentication please login again') } });
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
holders.admin = (...handlers) => {
|
|
45
|
-
if (!Array.isArray(handlers))
|
|
46
|
-
throw new Error('handlers must be an array');
|
|
47
|
-
const hasConfig = typeof handlers[0] === 'object' && 'permission' in handlers[0];
|
|
48
|
-
const configObj = hasConfig ? handlers[0] : undefined;
|
|
49
|
-
const handlerFns = hasConfig ? handlers.slice(1) : handlers;
|
|
50
|
-
handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ''}`] = {
|
|
51
|
-
...(configObj ? { config: configObj } : {}),
|
|
52
|
-
handlers: handlerFns
|
|
53
|
-
};
|
|
54
|
-
return useAuth;
|
|
55
|
-
};
|
|
56
|
-
holders.user = (...handlers) => {
|
|
57
|
-
if (!Array.isArray(handlers))
|
|
58
|
-
throw new Error('handlers must be an array');
|
|
59
|
-
handlerState.user = { handlers };
|
|
60
|
-
return useAuth;
|
|
61
|
-
};
|
|
62
|
-
holders.any = (...handlers) => {
|
|
63
|
-
if (!Array.isArray(handlers))
|
|
64
|
-
throw new Error('handlers must be an array');
|
|
65
|
-
handlerState.any = { handlers };
|
|
66
|
-
return useAuth;
|
|
67
|
-
};
|
|
68
|
-
useAuth = Object.assign(useAuth, holders);
|
|
69
|
-
return useAuth;
|
|
3
|
+
const getConfig_1 = require("../util/getConfig");
|
|
4
|
+
const gConfig = {
|
|
5
|
+
admin: { role: "ADMIN", model: "../models/admin" },
|
|
6
|
+
jwtSecretEnv: "JWT_KEY",
|
|
7
|
+
customValidator: (req, key) => {
|
|
8
|
+
return true;
|
|
9
|
+
},
|
|
10
|
+
...(0, getConfig_1.default)("auth"),
|
|
11
|
+
};
|
|
12
|
+
const executer = (authType) => async (req, res, next) => {
|
|
13
|
+
const accessResult = await gConfig?.customValidator?.(req, authType);
|
|
14
|
+
if (accessResult == null)
|
|
15
|
+
return next({ status: 401, json: { message: 'Unauthorized' } });
|
|
16
|
+
if (!accessResult)
|
|
17
|
+
return next({ status: 403, json: { message: 'Forbidden' } });
|
|
18
|
+
return next();
|
|
19
|
+
};
|
|
20
|
+
const auth = {
|
|
21
|
+
user: () => {
|
|
22
|
+
return executer("user");
|
|
23
|
+
},
|
|
24
|
+
admin: (config) => {
|
|
25
|
+
return executer(`admin${config?.permission ? `:${config.permission}` : ''}`);
|
|
26
|
+
},
|
|
27
|
+
any: () => {
|
|
28
|
+
return executer("any");
|
|
29
|
+
},
|
|
70
30
|
};
|
|
71
|
-
console.log('auth', 81, typeof auth);
|
|
72
31
|
exports.default = auth;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
|
|
3
|
+
type UseAuth = (req: Request, res: Response, next: NextFunction) => UseAuth | {
|
|
4
|
+
user?: (...handlers: Handler[]) => unknown;
|
|
5
|
+
admin?: (...handlers: [{
|
|
6
|
+
permission: string;
|
|
7
|
+
}, ...Handler[]] | Handler[]) => unknown;
|
|
8
|
+
any?: (...handlers: Handler[]) => unknown;
|
|
9
|
+
};
|
|
10
|
+
declare const auth: (config?: {
|
|
11
|
+
admin: {
|
|
12
|
+
role: string;
|
|
13
|
+
model: string;
|
|
14
|
+
};
|
|
15
|
+
jwtSecretEnv: string;
|
|
16
|
+
customValidator: (req: Request, key: string) => boolean;
|
|
17
|
+
}) => UseAuth;
|
|
18
|
+
export default auth;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const getConfig_1 = __importDefault(require("../util/getConfig"));
|
|
7
|
+
const gConfig = {
|
|
8
|
+
admin: { role: "ADMIN", model: "../models/admin" },
|
|
9
|
+
jwtSecretEnv: "JWT_KEY",
|
|
10
|
+
customValidator: (req, key) => {
|
|
11
|
+
return true;
|
|
12
|
+
},
|
|
13
|
+
...(0, getConfig_1.default)("auth"),
|
|
14
|
+
};
|
|
15
|
+
const holders = {};
|
|
16
|
+
const auth = (config = gConfig) => {
|
|
17
|
+
// const adminModel = require('../models/admin')
|
|
18
|
+
let handlerState = {};
|
|
19
|
+
let useAuth = async (req, res, next) => {
|
|
20
|
+
try {
|
|
21
|
+
const nextFunc = (handlers, index = 0) => (errorMessage = "") => {
|
|
22
|
+
if (errorMessage)
|
|
23
|
+
return next(errorMessage);
|
|
24
|
+
if (!handlers.length || !handlers[index + 1])
|
|
25
|
+
return next();
|
|
26
|
+
handlers[index + 1](req, res, nextFunc(handlers, index + 1));
|
|
27
|
+
};
|
|
28
|
+
const keys = Object.keys(handlerState);
|
|
29
|
+
for (let i = 0; i < keys.length; i++) {
|
|
30
|
+
const key = keys[i];
|
|
31
|
+
if (!handlerState[key]?.handlers?.length)
|
|
32
|
+
continue;
|
|
33
|
+
if (await gConfig?.customValidator?.(req, key)) {
|
|
34
|
+
return await handlerState[key].handlers[0](req, res, nextFunc(handlerState[key].handlers));
|
|
35
|
+
}
|
|
36
|
+
else if ((await gConfig?.customValidator?.(req, key)) == null) {
|
|
37
|
+
return next({ status: 401, json: { message: "unauthorized" } });
|
|
38
|
+
}
|
|
39
|
+
else if (i === keys.length - 1) {
|
|
40
|
+
next({
|
|
41
|
+
status: 405,
|
|
42
|
+
json: { message: "you don't have access to this section" },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
console.log("auth", 42, error);
|
|
51
|
+
next({
|
|
52
|
+
status: 403,
|
|
53
|
+
json: {
|
|
54
|
+
message: error instanceof Error
|
|
55
|
+
? error.message
|
|
56
|
+
: "error on authentication please login again",
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
holders.admin = (...handlers) => {
|
|
62
|
+
if (!Array.isArray(handlers))
|
|
63
|
+
throw new Error("handlers must be an array");
|
|
64
|
+
const hasConfig = typeof handlers[0] === "object" && "permission" in handlers[0];
|
|
65
|
+
const configObj = hasConfig
|
|
66
|
+
? handlers[0]
|
|
67
|
+
: undefined;
|
|
68
|
+
const handlerFns = hasConfig
|
|
69
|
+
? handlers.slice(1)
|
|
70
|
+
: handlers;
|
|
71
|
+
handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ""}`] = {
|
|
72
|
+
...(configObj ? { config: configObj } : {}),
|
|
73
|
+
handlers: handlerFns,
|
|
74
|
+
};
|
|
75
|
+
return useAuth;
|
|
76
|
+
};
|
|
77
|
+
holders.user = (...handlers) => {
|
|
78
|
+
if (!Array.isArray(handlers))
|
|
79
|
+
throw new Error("handlers must be an array");
|
|
80
|
+
handlerState.user = { handlers };
|
|
81
|
+
return useAuth;
|
|
82
|
+
};
|
|
83
|
+
holders.any = (...handlers) => {
|
|
84
|
+
if (!Array.isArray(handlers))
|
|
85
|
+
throw new Error("handlers must be an array");
|
|
86
|
+
handlerState.any = { handlers };
|
|
87
|
+
return useAuth;
|
|
88
|
+
};
|
|
89
|
+
useAuth = Object.assign(useAuth, holders);
|
|
90
|
+
return useAuth;
|
|
91
|
+
};
|
|
92
|
+
console.log("auth", 81, typeof auth);
|
|
93
|
+
exports.default = auth;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
|
|
3
|
+
type Executer = (authField: string) => (req: Request, res: Response, next: NextFunction) => unknown;
|
|
4
|
+
type Auth = {
|
|
5
|
+
user: () => Handler;
|
|
6
|
+
admin: (config?: {
|
|
7
|
+
permission: string;
|
|
8
|
+
}) => Handler;
|
|
9
|
+
any: () => Handler;
|
|
10
|
+
};
|
|
11
|
+
type ConfigType = {
|
|
12
|
+
customValidator?: (req: Request, key: string) => Promise<boolean | null>;
|
|
13
|
+
jwtSecretEnv?: string;
|
|
14
|
+
admin?: {
|
|
15
|
+
role: string;
|
|
16
|
+
model: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export { Handler, Executer, Auth, ConfigType };
|
package/dist/cjs/db/db.d.ts
CHANGED
|
@@ -117,7 +117,7 @@ declare const db: (collectionName: string, config?: ConfigsType) => {
|
|
|
117
117
|
limit(value: number): /*elided*/ any;
|
|
118
118
|
populate(value: string | object | (string | object)[]): /*elided*/ any;
|
|
119
119
|
};
|
|
120
|
-
aggregate: (pipeline?:
|
|
120
|
+
aggregate: (pipeline?: any[]) => {
|
|
121
121
|
(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
122
122
|
select(value: string | {
|
|
123
123
|
[key: string]: -1 | 1;
|
|
@@ -177,5 +177,5 @@ declare const db: (collectionName: string, config?: ConfigsType) => {
|
|
|
177
177
|
limit(value: number): /*elided*/ any;
|
|
178
178
|
populate(value: string | object | (string | object)[]): /*elided*/ any;
|
|
179
179
|
};
|
|
180
|
-
}
|
|
180
|
+
};
|
|
181
181
|
export default db;
|
package/dist/cjs/db/db.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
let __path = "./models/";
|
|
7
|
-
const path_1 =
|
|
8
|
-
const strToObj_1 =
|
|
9
|
-
const getConfig_1 =
|
|
10
|
-
const translator_1 =
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const strToObj_1 = require("../util/strToObj");
|
|
6
|
+
const getConfig_1 = require("../util/getConfig");
|
|
7
|
+
const translator_1 = require("../util/translator");
|
|
11
8
|
let configs = {
|
|
12
9
|
dbType: "mongodb",
|
|
13
10
|
path: "../models",
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.models = exports.validator = exports.uploader = exports.db = exports.auth = void 0;
|
|
7
|
-
const auth_1 =
|
|
4
|
+
const auth_1 = require("./auth/auth");
|
|
8
5
|
exports.auth = auth_1.default;
|
|
9
|
-
const db_1 =
|
|
6
|
+
const db_1 = require("./db/db");
|
|
10
7
|
exports.db = db_1.default;
|
|
11
|
-
const uploader_1 =
|
|
8
|
+
const uploader_1 = require("./uploader/uploader");
|
|
12
9
|
exports.uploader = uploader_1.default;
|
|
13
|
-
const validator_1 =
|
|
10
|
+
const validator_1 = require("./validator/validator");
|
|
14
11
|
exports.validator = validator_1.default;
|
|
15
|
-
const translation_1 =
|
|
12
|
+
const translation_1 = require("./model/translation");
|
|
16
13
|
const models = { translation: translation_1.default };
|
|
17
14
|
exports.models = models;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const mongoose_1 =
|
|
3
|
+
const mongoose_1 = require("mongoose");
|
|
7
4
|
const schema = new mongoose_1.default.Schema({
|
|
8
5
|
text: { type: String, required: true },
|
|
9
6
|
lang: { type: String, required: true, },
|
|
@@ -19,5 +19,5 @@ declare const _default: (destination?: string | ((req: Request, file: Express.Mu
|
|
|
19
19
|
fields: (fields: multer.Field[]) => (req: Request, res: Response, next: NextFunction) => void;
|
|
20
20
|
any: () => (req: Request, res: Response, next: NextFunction) => void;
|
|
21
21
|
none: () => (req: Request, res: Response, next: NextFunction) => void;
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
export default _default;
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const path_1 =
|
|
7
|
-
const fs_1 =
|
|
8
|
-
const multer_1 =
|
|
9
|
-
const mongoose_1 =
|
|
10
|
-
const getConfig_1 =
|
|
3
|
+
const path_1 = require("path");
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const multer_1 = require("multer");
|
|
6
|
+
const mongoose_1 = require("mongoose");
|
|
7
|
+
const getConfig_1 = require("../util/getConfig");
|
|
11
8
|
const relativePath = '../../../public';
|
|
12
9
|
let configs = {
|
|
13
10
|
saveInDb: false,
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const fs_1 =
|
|
7
|
-
const path_1 =
|
|
3
|
+
const fs_1 = require("fs");
|
|
4
|
+
const path_1 = require("path");
|
|
8
5
|
const find = (field, ...args) => {
|
|
9
6
|
const rel = path_1.default.join.apply(null, [].slice.call(args));
|
|
10
7
|
if (!require?.main?.filename)
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const axios_1 =
|
|
7
|
-
const translation_1 =
|
|
8
|
-
const getConfig_1 =
|
|
3
|
+
const axios_1 = require("axios");
|
|
4
|
+
const translation_1 = require("./../model/translation");
|
|
5
|
+
const getConfig_1 = require("./getConfig");
|
|
9
6
|
const configs = { ...(0, getConfig_1.default)('translate') };
|
|
10
7
|
async function firstSuccessful(promises) {
|
|
11
8
|
return new Promise((resolve, reject) => {
|
package/dist/esm/auth/auth.d.ts
CHANGED
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
type AdminArgs = [{
|
|
4
|
-
permission: string;
|
|
5
|
-
}, ...Handler[]] | Handler[];
|
|
6
|
-
interface UseAuth {
|
|
7
|
-
(req: Request, res: Response, next: NextFunction): any;
|
|
8
|
-
user?: (...handlers: Handler[]) => UseAuth;
|
|
9
|
-
admin?: (...handlers: AdminArgs) => UseAuth;
|
|
10
|
-
any?: (...handlers: Handler[]) => UseAuth;
|
|
11
|
-
}
|
|
12
|
-
declare const auth: (config?: {
|
|
13
|
-
admin: {
|
|
14
|
-
role: string;
|
|
15
|
-
model: string;
|
|
16
|
-
};
|
|
17
|
-
jwtSecretEnv: string;
|
|
18
|
-
customValidator: (req: Request, key: string) => boolean;
|
|
19
|
-
}) => UseAuth;
|
|
1
|
+
import * as Types from "./types";
|
|
2
|
+
declare const auth: Types.Auth;
|
|
20
3
|
export default auth;
|
package/dist/esm/auth/auth.js
CHANGED
|
@@ -4,69 +4,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const getConfig_1 = __importDefault(require("../util/getConfig"));
|
|
7
|
-
const gConfig = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
next({ status: 405, json: { message: 'you don\'t have access to this section' } });
|
|
34
|
-
}
|
|
35
|
-
else
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
console.log('auth', 42, error);
|
|
41
|
-
next({ status: 403, json: { message: (error instanceof Error ? error.message : 'error on authentication please login again') } });
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
holders.admin = (...handlers) => {
|
|
45
|
-
if (!Array.isArray(handlers))
|
|
46
|
-
throw new Error('handlers must be an array');
|
|
47
|
-
const hasConfig = typeof handlers[0] === 'object' && 'permission' in handlers[0];
|
|
48
|
-
const configObj = hasConfig ? handlers[0] : undefined;
|
|
49
|
-
const handlerFns = hasConfig ? handlers.slice(1) : handlers;
|
|
50
|
-
handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ''}`] = {
|
|
51
|
-
...(configObj ? { config: configObj } : {}),
|
|
52
|
-
handlers: handlerFns
|
|
53
|
-
};
|
|
54
|
-
return useAuth;
|
|
55
|
-
};
|
|
56
|
-
holders.user = (...handlers) => {
|
|
57
|
-
if (!Array.isArray(handlers))
|
|
58
|
-
throw new Error('handlers must be an array');
|
|
59
|
-
handlerState.user = { handlers };
|
|
60
|
-
return useAuth;
|
|
61
|
-
};
|
|
62
|
-
holders.any = (...handlers) => {
|
|
63
|
-
if (!Array.isArray(handlers))
|
|
64
|
-
throw new Error('handlers must be an array');
|
|
65
|
-
handlerState.any = { handlers };
|
|
66
|
-
return useAuth;
|
|
67
|
-
};
|
|
68
|
-
useAuth = Object.assign(useAuth, holders);
|
|
69
|
-
return useAuth;
|
|
7
|
+
const gConfig = {
|
|
8
|
+
admin: { role: "ADMIN", model: "../models/admin" },
|
|
9
|
+
jwtSecretEnv: "JWT_KEY",
|
|
10
|
+
customValidator: (req, key) => {
|
|
11
|
+
return true;
|
|
12
|
+
},
|
|
13
|
+
...(0, getConfig_1.default)("auth"),
|
|
14
|
+
};
|
|
15
|
+
const executer = (authType) => async (req, res, next) => {
|
|
16
|
+
const accessResult = await gConfig?.customValidator?.(req, authType);
|
|
17
|
+
if (accessResult == null)
|
|
18
|
+
return next({ status: 401, json: { message: 'Unauthorized' } });
|
|
19
|
+
if (!accessResult)
|
|
20
|
+
return next({ status: 403, json: { message: 'Forbidden' } });
|
|
21
|
+
return next();
|
|
22
|
+
};
|
|
23
|
+
const auth = {
|
|
24
|
+
user: () => {
|
|
25
|
+
return executer("user");
|
|
26
|
+
},
|
|
27
|
+
admin: (config) => {
|
|
28
|
+
return executer(`admin${config?.permission ? `:${config.permission}` : ''}`);
|
|
29
|
+
},
|
|
30
|
+
any: () => {
|
|
31
|
+
return executer("any");
|
|
32
|
+
},
|
|
70
33
|
};
|
|
71
|
-
console.log('auth', 81, typeof auth);
|
|
72
34
|
exports.default = auth;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
|
|
3
|
+
type Executer = (authField: string) => (req: Request, res: Response, next: NextFunction) => unknown;
|
|
4
|
+
type Auth = {
|
|
5
|
+
user: () => Handler;
|
|
6
|
+
admin: (config?: {
|
|
7
|
+
permission: string;
|
|
8
|
+
}) => Handler;
|
|
9
|
+
any: () => Handler;
|
|
10
|
+
};
|
|
11
|
+
type ConfigType = {
|
|
12
|
+
customValidator?: (req: Request, key: string) => Promise<boolean | null>;
|
|
13
|
+
jwtSecretEnv?: string;
|
|
14
|
+
admin?: {
|
|
15
|
+
role: string;
|
|
16
|
+
model: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export { Handler, Executer, Auth, ConfigType };
|