@proteinjs/user-auth 1.0.1
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/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/dist/generated/index.d.ts +5 -0
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/index.js +28 -0
- package/dist/generated/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/src/UserAuth.d.ts +24 -0
- package/dist/src/UserAuth.d.ts.map +1 -0
- package/dist/src/UserAuth.js +60 -0
- package/dist/src/UserAuth.js.map +1 -0
- package/dist/src/guestUser.d.ts +12 -0
- package/dist/src/guestUser.d.ts.map +1 -0
- package/dist/src/guestUser.js +18 -0
- package/dist/src/guestUser.js.map +1 -0
- package/generated/index.ts +25 -0
- package/index.ts +2 -0
- package/jest.config.js +18 -0
- package/package.json +38 -0
- package/src/UserAuth.ts +70 -0
- package/src/guestUser.ts +12 -0
- package/tsconfig.json +18 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Brent Bahry
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../generated/index.ts"],"names":[],"mappings":"AAAA,oCAAoC;AAEpC,OAAO,uBAAuB,CAAC;AAC/B,OAAO,QAAQ,CAAC;AAqBhB,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** Load Dependency Source Graphs */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
require("@proteinjs/reflection");
|
|
19
|
+
require("moment");
|
|
20
|
+
/** Generate Source Graph */
|
|
21
|
+
var sourceGraph = "{\"options\":{\"directed\":true,\"multigraph\":false,\"compound\":false},\"nodes\":[{\"v\":\"@proteinjs/user-auth/AuthenticatedUserRepo\",\"value\":{\"packageName\":\"@proteinjs/user-auth\",\"name\":\"AuthenticatedUserRepo\",\"filePath\":\"/home/runner/work/user/user/packages/auth/src/UserAuth.ts\",\"qualifiedName\":\"@proteinjs/user-auth/AuthenticatedUserRepo\",\"properties\":[],\"methods\":[{\"name\":\"getUser\",\"returnType\":{\"packageName\":\"@proteinjs/user-auth\",\"name\":\"AuthenticatedUser\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/user-auth/AuthenticatedUser\",\"typeParameters\":null,\"directParents\":null},\"isAsync\":false,\"isOptional\":false,\"isAbstract\":true,\"isStatic\":false,\"visibility\":\"public\",\"parameters\":[]}],\"typeParameters\":[],\"directParents\":[{\"packageName\":\"@proteinjs/reflection\",\"name\":\"Loadable\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/reflection/Loadable\",\"properties\":[],\"methods\":[],\"typeParameters\":[],\"directParents\":[]}],\"sourceType\":3}},{\"v\":\"@proteinjs/reflection/Loadable\"}],\"edges\":[{\"v\":\"@proteinjs/user-auth/AuthenticatedUserRepo\",\"w\":\"@proteinjs/reflection/Loadable\",\"value\":\"extends interface\"}]}";
|
|
22
|
+
/** Generate Source Links */
|
|
23
|
+
var sourceLinks = {};
|
|
24
|
+
/** Load Source Graph and Links */
|
|
25
|
+
var reflection_1 = require("@proteinjs/reflection");
|
|
26
|
+
reflection_1.SourceRepository.merge(sourceGraph, sourceLinks);
|
|
27
|
+
__exportStar(require("../index"), exports);
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../generated/index.ts"],"names":[],"mappings":";AAAA,oCAAoC;;;;;;;;;;;;;;;;AAEpC,iCAA+B;AAC/B,kBAAgB;AAGhB,4BAA4B;AAE5B,IAAM,WAAW,GAAG,isCAAisC,CAAC;AAGttC,4BAA4B;AAG5B,IAAM,WAAW,GAAG,EACnB,CAAC;AAGF,kCAAkC;AAElC,oDAAyD;AACzD,6BAAgB,CAAC,KAAK,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AAGjD,2CAAyB"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iBAAiB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./src/UserAuth"), exports);
|
|
18
|
+
__exportStar(require("./src/guestUser"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA8B;AAC9B,kDAA+B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Loadable } from '@proteinjs/reflection';
|
|
2
|
+
export interface AuthenticatedUser {
|
|
3
|
+
email: string;
|
|
4
|
+
roles: string[];
|
|
5
|
+
}
|
|
6
|
+
export interface AuthenticatedUserRepo extends Loadable {
|
|
7
|
+
getUser(): AuthenticatedUser;
|
|
8
|
+
}
|
|
9
|
+
export declare const getAuthenticatedUserRepo: () => AuthenticatedUserRepo | undefined;
|
|
10
|
+
export declare class UserAuth {
|
|
11
|
+
private static userRepo?;
|
|
12
|
+
private static getUserRepo;
|
|
13
|
+
static isLoggedIn(): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* @return true if user has role or user has role 'admin'
|
|
16
|
+
*/
|
|
17
|
+
static hasRole(role: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* @param has (default) `all` - return true if user has all roles
|
|
20
|
+
* @param has `at least one` - return true if user has at least one role
|
|
21
|
+
*/
|
|
22
|
+
static hasRoles(roles: string[], has?: 'all' | 'at least one'): boolean;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=UserAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserAuth.d.ts","sourceRoot":"","sources":["../../src/UserAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAoB,MAAM,uBAAuB,CAAA;AAGlE,MAAM,WAAW,iBAAiB;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,MAAM,WAAW,qBAAsB,SAAQ,QAAQ;IACnD,OAAO,IAAI,iBAAiB,CAAC;CAChC;AAED,eAAO,MAAM,wBAAwB,yCAAqH,CAAC;AAE3J,qBAAa,QAAQ;IACjB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAwB;IAEhD,OAAO,CAAC,MAAM,CAAC,WAAW;IAO1B,MAAM,CAAC,UAAU,IAAI,OAAO;IAS5B;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAYrC;;;OAGG;IACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,GAAG,GAAE,KAAK,GAAC,cAAsB,GAAG,OAAO;CAiB/E"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UserAuth = exports.getAuthenticatedUserRepo = void 0;
|
|
4
|
+
var reflection_1 = require("@proteinjs/reflection");
|
|
5
|
+
var guestUser_1 = require("./guestUser");
|
|
6
|
+
var getAuthenticatedUserRepo = function () { return reflection_1.SourceRepository.get().object('@proteinjs/user-auth/AuthenticatedUserRepo'); };
|
|
7
|
+
exports.getAuthenticatedUserRepo = getAuthenticatedUserRepo;
|
|
8
|
+
var UserAuth = /** @class */ (function () {
|
|
9
|
+
function UserAuth() {
|
|
10
|
+
}
|
|
11
|
+
UserAuth.getUserRepo = function () {
|
|
12
|
+
if (!UserAuth.userRepo)
|
|
13
|
+
UserAuth.userRepo = (0, exports.getAuthenticatedUserRepo)();
|
|
14
|
+
return UserAuth.userRepo;
|
|
15
|
+
};
|
|
16
|
+
UserAuth.isLoggedIn = function () {
|
|
17
|
+
var userRepo = UserAuth.getUserRepo();
|
|
18
|
+
if (!userRepo)
|
|
19
|
+
return true;
|
|
20
|
+
var user = userRepo.getUser();
|
|
21
|
+
return user.email != guestUser_1.guestUser.email;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* @return true if user has role or user has role 'admin'
|
|
25
|
+
*/
|
|
26
|
+
UserAuth.hasRole = function (role) {
|
|
27
|
+
var userRepo = UserAuth.getUserRepo();
|
|
28
|
+
if (!userRepo)
|
|
29
|
+
return true;
|
|
30
|
+
var user = userRepo.getUser();
|
|
31
|
+
if (user.roles.includes('admin'))
|
|
32
|
+
return true;
|
|
33
|
+
return user.roles.includes(role);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* @param has (default) `all` - return true if user has all roles
|
|
37
|
+
* @param has `at least one` - return true if user has at least one role
|
|
38
|
+
*/
|
|
39
|
+
UserAuth.hasRoles = function (roles, has) {
|
|
40
|
+
if (has === void 0) { has = 'all'; }
|
|
41
|
+
var userRepo = UserAuth.getUserRepo();
|
|
42
|
+
if (!userRepo)
|
|
43
|
+
return true;
|
|
44
|
+
for (var _i = 0, roles_1 = roles; _i < roles_1.length; _i++) {
|
|
45
|
+
var role = roles_1[_i];
|
|
46
|
+
if (!UserAuth.hasRole(role)) {
|
|
47
|
+
if (has === 'all')
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
if (has === 'at least one')
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return has === 'all';
|
|
56
|
+
};
|
|
57
|
+
return UserAuth;
|
|
58
|
+
}());
|
|
59
|
+
exports.UserAuth = UserAuth;
|
|
60
|
+
//# sourceMappingURL=UserAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserAuth.js","sourceRoot":"","sources":["../../src/UserAuth.ts"],"names":[],"mappings":";;;AAAA,oDAAkE;AAClE,yCAAuC;AAWhC,IAAM,wBAAwB,GAAG,cAAM,OAAA,6BAAgB,CAAC,GAAG,EAAE,CAAC,MAAM,CAAkC,4CAA4C,CAAC,EAA5G,CAA4G,CAAC;AAA9I,QAAA,wBAAwB,4BAAsH;AAE3J;IAAA;IAuDA,CAAC;IApDkB,oBAAW,GAA1B;QACI,IAAI,CAAC,QAAQ,CAAC,QAAQ;YAClB,QAAQ,CAAC,QAAQ,GAAG,IAAA,gCAAwB,GAAE,CAAC;QAEnD,OAAO,QAAQ,CAAC,QAAQ,CAAC;IAC7B,CAAC;IAEM,mBAAU,GAAjB;QACI,IAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ;YACT,OAAO,IAAI,CAAC;QAEhB,IAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;QAChC,OAAO,IAAI,CAAC,KAAK,IAAI,qBAAS,CAAC,KAAK,CAAC;IACzC,CAAC;IAED;;OAEG;IACI,gBAAO,GAAd,UAAe,IAAY;QACvB,IAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ;YACT,OAAO,IAAI,CAAC;QAEhB,IAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;QAChC,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC;YAC5B,OAAO,IAAI,CAAC;QAEhB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IAED;;;OAGG;IACI,iBAAQ,GAAf,UAAgB,KAAe,EAAE,GAAiC;QAAjC,oBAAA,EAAA,WAAiC;QAC9D,IAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ;YACT,OAAO,IAAI,CAAC;QAEhB,KAAmB,UAAK,EAAL,eAAK,EAAL,mBAAK,EAAL,IAAK,EAAE;YAArB,IAAM,IAAI,cAAA;YACX,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;gBACzB,IAAI,GAAG,KAAK,KAAK;oBACb,OAAO,KAAK,CAAC;aACpB;iBAAM;gBACH,IAAI,GAAG,KAAK,cAAc;oBACtB,OAAO,IAAI,CAAC;aACnB;SACJ;QAED,OAAO,GAAG,KAAK,KAAK,CAAC;IACzB,CAAC;IACL,eAAC;AAAD,CAAC,AAvDD,IAuDC;AAvDY,4BAAQ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
export declare const guestUser: {
|
|
3
|
+
name: string;
|
|
4
|
+
email: string;
|
|
5
|
+
password: string;
|
|
6
|
+
emailVerified: boolean;
|
|
7
|
+
roles: string;
|
|
8
|
+
created: moment.Moment;
|
|
9
|
+
updated: moment.Moment;
|
|
10
|
+
id: string;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=guestUser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guestUser.d.ts","sourceRoot":"","sources":["../../src/guestUser.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAE3B,eAAO,MAAM,SAAS;;;;;;;;;CASrB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
exports.guestUser = void 0;
|
|
7
|
+
var moment_1 = __importDefault(require("moment"));
|
|
8
|
+
exports.guestUser = {
|
|
9
|
+
name: 'Guest',
|
|
10
|
+
email: 'guest',
|
|
11
|
+
password: 'guest',
|
|
12
|
+
emailVerified: false,
|
|
13
|
+
roles: '',
|
|
14
|
+
created: (0, moment_1.default)(),
|
|
15
|
+
updated: (0, moment_1.default)(),
|
|
16
|
+
id: 'guest',
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=guestUser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guestUser.js","sourceRoot":"","sources":["../../src/guestUser.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA2B;AAEd,QAAA,SAAS,GAAG;IACrB,IAAI,EAAE,OAAO;IACb,KAAK,EAAE,OAAO;IACd,QAAQ,EAAE,OAAO;IACjB,aAAa,EAAE,KAAK;IACpB,KAAK,EAAE,EAAE;IACT,OAAO,EAAE,IAAA,gBAAM,GAAE;IACjB,OAAO,EAAE,IAAA,gBAAM,GAAE;IACjB,EAAE,EAAE,OAAO;CACd,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Load Dependency Source Graphs */
|
|
2
|
+
|
|
3
|
+
import '@proteinjs/reflection';
|
|
4
|
+
import 'moment';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/** Generate Source Graph */
|
|
8
|
+
|
|
9
|
+
const sourceGraph = "{\"options\":{\"directed\":true,\"multigraph\":false,\"compound\":false},\"nodes\":[{\"v\":\"@proteinjs/user-auth/AuthenticatedUserRepo\",\"value\":{\"packageName\":\"@proteinjs/user-auth\",\"name\":\"AuthenticatedUserRepo\",\"filePath\":\"/home/runner/work/user/user/packages/auth/src/UserAuth.ts\",\"qualifiedName\":\"@proteinjs/user-auth/AuthenticatedUserRepo\",\"properties\":[],\"methods\":[{\"name\":\"getUser\",\"returnType\":{\"packageName\":\"@proteinjs/user-auth\",\"name\":\"AuthenticatedUser\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/user-auth/AuthenticatedUser\",\"typeParameters\":null,\"directParents\":null},\"isAsync\":false,\"isOptional\":false,\"isAbstract\":true,\"isStatic\":false,\"visibility\":\"public\",\"parameters\":[]}],\"typeParameters\":[],\"directParents\":[{\"packageName\":\"@proteinjs/reflection\",\"name\":\"Loadable\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/reflection/Loadable\",\"properties\":[],\"methods\":[],\"typeParameters\":[],\"directParents\":[]}],\"sourceType\":3}},{\"v\":\"@proteinjs/reflection/Loadable\"}],\"edges\":[{\"v\":\"@proteinjs/user-auth/AuthenticatedUserRepo\",\"w\":\"@proteinjs/reflection/Loadable\",\"value\":\"extends interface\"}]}";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/** Generate Source Links */
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const sourceLinks = {
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/** Load Source Graph and Links */
|
|
20
|
+
|
|
21
|
+
import { SourceRepository } from '@proteinjs/reflection';
|
|
22
|
+
SourceRepository.merge(sourceGraph, sourceLinks);
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export * from '../index';
|
package/index.ts
ADDED
package/jest.config.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
"roots": [
|
|
3
|
+
"<rootDir>/test"
|
|
4
|
+
],
|
|
5
|
+
"transform": {
|
|
6
|
+
"^.+\\.tsx?$": "ts-jest"
|
|
7
|
+
},
|
|
8
|
+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
|
|
9
|
+
"moduleFileExtensions": [
|
|
10
|
+
"ts",
|
|
11
|
+
"tsx",
|
|
12
|
+
"js",
|
|
13
|
+
"jsx",
|
|
14
|
+
"json",
|
|
15
|
+
"node"
|
|
16
|
+
],
|
|
17
|
+
"testEnvironment": "node"
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@proteinjs/user-auth",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "User auth",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/proteinjs/user.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/proteinjs/user/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/proteinjs/user#readme",
|
|
16
|
+
"author": "Brent Bahry",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"clean": "rm -rf dist/ generated/ node_modules/ package-lock.json",
|
|
20
|
+
"build": "reflection-build && tsc",
|
|
21
|
+
"watch": "reflection-watch"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@proteinjs/reflection": "1.0.2",
|
|
25
|
+
"moment": "2.29.4"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@proteinjs/reflection-build": "1.0.5",
|
|
29
|
+
"@types/jest": "29.5.5",
|
|
30
|
+
"@types/node": "14.0.13",
|
|
31
|
+
"jest": "29.7.0",
|
|
32
|
+
"ts-jest": "29.1.1",
|
|
33
|
+
"typescript": "5.2.2"
|
|
34
|
+
},
|
|
35
|
+
"main": "./dist/generated/index.js",
|
|
36
|
+
"types": "./dist/generated/index.d.ts",
|
|
37
|
+
"gitHead": "f4e1221865c6d1f7b8417477096917eaded88a97"
|
|
38
|
+
}
|
package/src/UserAuth.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Loadable, SourceRepository } from '@proteinjs/reflection'
|
|
2
|
+
import { guestUser } from './guestUser'
|
|
3
|
+
|
|
4
|
+
export interface AuthenticatedUser {
|
|
5
|
+
email: string;
|
|
6
|
+
roles: string[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface AuthenticatedUserRepo extends Loadable {
|
|
10
|
+
getUser(): AuthenticatedUser;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const getAuthenticatedUserRepo = () => SourceRepository.get().object<AuthenticatedUserRepo|undefined>('@proteinjs/user-auth/AuthenticatedUserRepo');
|
|
14
|
+
|
|
15
|
+
export class UserAuth {
|
|
16
|
+
private static userRepo?: AuthenticatedUserRepo;
|
|
17
|
+
|
|
18
|
+
private static getUserRepo() {
|
|
19
|
+
if (!UserAuth.userRepo)
|
|
20
|
+
UserAuth.userRepo = getAuthenticatedUserRepo();
|
|
21
|
+
|
|
22
|
+
return UserAuth.userRepo;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static isLoggedIn(): boolean {
|
|
26
|
+
const userRepo = UserAuth.getUserRepo();
|
|
27
|
+
if (!userRepo)
|
|
28
|
+
return true;
|
|
29
|
+
|
|
30
|
+
const user = userRepo.getUser();
|
|
31
|
+
return user.email != guestUser.email;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @return true if user has role or user has role 'admin'
|
|
36
|
+
*/
|
|
37
|
+
static hasRole(role: string): boolean {
|
|
38
|
+
const userRepo = UserAuth.getUserRepo();
|
|
39
|
+
if (!userRepo)
|
|
40
|
+
return true;
|
|
41
|
+
|
|
42
|
+
const user = userRepo.getUser();
|
|
43
|
+
if (user.roles.includes('admin'))
|
|
44
|
+
return true;
|
|
45
|
+
|
|
46
|
+
return user.roles.includes(role);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param has (default) `all` - return true if user has all roles
|
|
51
|
+
* @param has `at least one` - return true if user has at least one role
|
|
52
|
+
*/
|
|
53
|
+
static hasRoles(roles: string[], has: 'all'|'at least one' = 'all'): boolean {
|
|
54
|
+
const userRepo = UserAuth.getUserRepo();
|
|
55
|
+
if (!userRepo)
|
|
56
|
+
return true;
|
|
57
|
+
|
|
58
|
+
for (const role of roles) {
|
|
59
|
+
if (!UserAuth.hasRole(role)) {
|
|
60
|
+
if (has === 'all')
|
|
61
|
+
return false;
|
|
62
|
+
} else {
|
|
63
|
+
if (has === 'at least one')
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return has === 'all';
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/guestUser.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "./",
|
|
4
|
+
"target": "es5",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"outDir": "./dist/",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"noImplicitAny": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"jsx": "react"
|
|
17
|
+
}
|
|
18
|
+
}
|