@proteinjs/user-auth 1.0.7 → 1.0.9
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/.eslintrc.js +20 -0
- package/.prettierignore +4 -0
- package/.prettierrc +8 -0
- package/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/src/UserAuth.d.ts.map +1 -1
- package/dist/src/UserAuth.js +17 -8
- package/dist/src/UserAuth.js.map +1 -1
- package/dist/src/guestUser.d.ts.map +1 -1
- package/dist/src/guestUser.js.map +1 -1
- package/generated/index.ts +4 -9
- package/index.ts +2 -2
- package/jest.config.js +8 -17
- package/package.json +42 -36
- package/src/UserAuth.ts +56 -48
- package/src/guestUser.ts +10 -10
- package/tsconfig.json +17 -17
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', 'prettier'],
|
|
3
|
+
parser: '@typescript-eslint/parser',
|
|
4
|
+
plugins: ['@typescript-eslint', 'prettier'],
|
|
5
|
+
root: true,
|
|
6
|
+
ignorePatterns: ['**/dist/*', '**/node_modules/*', '*.md'],
|
|
7
|
+
|
|
8
|
+
rules: {
|
|
9
|
+
'prettier/prettier': ['warn'],
|
|
10
|
+
curly: ['warn'],
|
|
11
|
+
'eol-last': ['warn', 'always'],
|
|
12
|
+
'keyword-spacing': ['warn', { before: true }],
|
|
13
|
+
'no-undef': 'off',
|
|
14
|
+
'@typescript-eslint/no-unused-vars': 'off',
|
|
15
|
+
'@typescript-eslint/no-var-requires': 'off',
|
|
16
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
17
|
+
'@typescript-eslint/prefer-as-const': 'off',
|
|
18
|
+
'@typescript-eslint/ban-types': 'off',
|
|
19
|
+
},
|
|
20
|
+
};
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,23 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.0.9](https://github.com/proteinjs/user/compare/@proteinjs/user-auth@1.0.8...@proteinjs/user-auth@1.0.9) (2024-05-10)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* add .md file type to lint ignore files ([372c51f](https://github.com/proteinjs/user/commit/372c51fdc0a48c8559321862e3b7cebe05e4955d))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [1.0.8](https://github.com/proteinjs/user/compare/@proteinjs/user-auth@1.0.7...@proteinjs/user-auth@1.0.8) (2024-05-10)
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
- add linting and lint all files ([71defcd](https://github.com/proteinjs/user/commit/71defcd78dc479d2eef1f624c746c879f4e31daa))
|
|
22
|
+
|
|
6
23
|
## 1.0.1 (2024-04-19)
|
|
7
24
|
|
|
8
25
|
**Note:** Version bump only for package @proteinjs/user-auth
|
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.
|
package/dist/generated/index.js
CHANGED
|
@@ -18,7 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
require("@proteinjs/reflection");
|
|
19
19
|
require("moment");
|
|
20
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\":\"/
|
|
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
22
|
/** Generate Source Links */
|
|
23
23
|
var sourceLinks = {};
|
|
24
24
|
/** Load Source Graph and Links */
|
|
@@ -1 +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,
|
|
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.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,kDAAgC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UserAuth.d.ts","sourceRoot":"","sources":["../../src/UserAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAoB,MAAM,uBAAuB,
|
|
1
|
+
{"version":3,"file":"UserAuth.d.ts","sourceRoot":"","sources":["../../src/UserAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAoB,MAAM,uBAAuB,CAAC;AAGnE,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,MAAM,WAAW,qBAAsB,SAAQ,QAAQ;IACrD,OAAO,IAAI,iBAAiB,CAAC;CAC9B;AAED,eAAO,MAAM,wBAAwB,yCAC2E,CAAC;AAEjH,qBAAa,QAAQ;IACnB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAwB;IAEhD,OAAO,CAAC,MAAM,CAAC,WAAW;IAQ1B,MAAM,CAAC,UAAU,IAAI,OAAO;IAU5B;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAcrC;;;OAGG;IACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,GAAG,GAAE,KAAK,GAAG,cAAsB,GAAG,OAAO;CAoB/E"}
|
package/dist/src/UserAuth.js
CHANGED
|
@@ -3,20 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.UserAuth = exports.getAuthenticatedUserRepo = void 0;
|
|
4
4
|
var reflection_1 = require("@proteinjs/reflection");
|
|
5
5
|
var guestUser_1 = require("./guestUser");
|
|
6
|
-
var getAuthenticatedUserRepo = function () {
|
|
6
|
+
var getAuthenticatedUserRepo = function () {
|
|
7
|
+
return reflection_1.SourceRepository.get().object('@proteinjs/user-auth/AuthenticatedUserRepo');
|
|
8
|
+
};
|
|
7
9
|
exports.getAuthenticatedUserRepo = getAuthenticatedUserRepo;
|
|
8
10
|
var UserAuth = /** @class */ (function () {
|
|
9
11
|
function UserAuth() {
|
|
10
12
|
}
|
|
11
13
|
UserAuth.getUserRepo = function () {
|
|
12
|
-
if (!UserAuth.userRepo)
|
|
14
|
+
if (!UserAuth.userRepo) {
|
|
13
15
|
UserAuth.userRepo = (0, exports.getAuthenticatedUserRepo)();
|
|
16
|
+
}
|
|
14
17
|
return UserAuth.userRepo;
|
|
15
18
|
};
|
|
16
19
|
UserAuth.isLoggedIn = function () {
|
|
17
20
|
var userRepo = UserAuth.getUserRepo();
|
|
18
|
-
if (!userRepo)
|
|
21
|
+
if (!userRepo) {
|
|
19
22
|
return true;
|
|
23
|
+
}
|
|
20
24
|
var user = userRepo.getUser();
|
|
21
25
|
return user.email != guestUser_1.guestUser.email;
|
|
22
26
|
};
|
|
@@ -25,11 +29,13 @@ var UserAuth = /** @class */ (function () {
|
|
|
25
29
|
*/
|
|
26
30
|
UserAuth.hasRole = function (role) {
|
|
27
31
|
var userRepo = UserAuth.getUserRepo();
|
|
28
|
-
if (!userRepo)
|
|
32
|
+
if (!userRepo) {
|
|
29
33
|
return true;
|
|
34
|
+
}
|
|
30
35
|
var user = userRepo.getUser();
|
|
31
|
-
if (user.roles.includes('admin'))
|
|
36
|
+
if (user.roles.includes('admin')) {
|
|
32
37
|
return true;
|
|
38
|
+
}
|
|
33
39
|
return user.roles.includes(role);
|
|
34
40
|
};
|
|
35
41
|
/**
|
|
@@ -39,17 +45,20 @@ var UserAuth = /** @class */ (function () {
|
|
|
39
45
|
UserAuth.hasRoles = function (roles, has) {
|
|
40
46
|
if (has === void 0) { has = 'all'; }
|
|
41
47
|
var userRepo = UserAuth.getUserRepo();
|
|
42
|
-
if (!userRepo)
|
|
48
|
+
if (!userRepo) {
|
|
43
49
|
return true;
|
|
50
|
+
}
|
|
44
51
|
for (var _i = 0, roles_1 = roles; _i < roles_1.length; _i++) {
|
|
45
52
|
var role = roles_1[_i];
|
|
46
53
|
if (!UserAuth.hasRole(role)) {
|
|
47
|
-
if (has === 'all')
|
|
54
|
+
if (has === 'all') {
|
|
48
55
|
return false;
|
|
56
|
+
}
|
|
49
57
|
}
|
|
50
58
|
else {
|
|
51
|
-
if (has === 'at least one')
|
|
59
|
+
if (has === 'at least one') {
|
|
52
60
|
return true;
|
|
61
|
+
}
|
|
53
62
|
}
|
|
54
63
|
}
|
|
55
64
|
return has === 'all';
|
package/dist/src/UserAuth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UserAuth.js","sourceRoot":"","sources":["../../src/UserAuth.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"UserAuth.js","sourceRoot":"","sources":["../../src/UserAuth.ts"],"names":[],"mappings":";;;AAAA,oDAAmE;AACnE,yCAAwC;AAWjC,IAAM,wBAAwB,GAAG;IACtC,OAAA,6BAAgB,CAAC,GAAG,EAAE,CAAC,MAAM,CAAoC,4CAA4C,CAAC;AAA9G,CAA8G,CAAC;AADpG,QAAA,wBAAwB,4BAC4E;AAEjH;IAAA;IA8DA,CAAC;IA3DgB,oBAAW,GAA1B;QACE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE;YACtB,QAAQ,CAAC,QAAQ,GAAG,IAAA,gCAAwB,GAAE,CAAC;SAChD;QAED,OAAO,QAAQ,CAAC,QAAQ,CAAC;IAC3B,CAAC;IAEM,mBAAU,GAAjB;QACE,IAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE;YACb,OAAO,IAAI,CAAC;SACb;QAED,IAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;QAChC,OAAO,IAAI,CAAC,KAAK,IAAI,qBAAS,CAAC,KAAK,CAAC;IACvC,CAAC;IAED;;OAEG;IACI,gBAAO,GAAd,UAAe,IAAY;QACzB,IAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE;YACb,OAAO,IAAI,CAAC;SACb;QAED,IAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;QAChC,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;YAChC,OAAO,IAAI,CAAC;SACb;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAED;;;OAGG;IACI,iBAAQ,GAAf,UAAgB,KAAe,EAAE,GAAmC;QAAnC,oBAAA,EAAA,WAAmC;QAClE,IAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE;YACb,OAAO,IAAI,CAAC;SACb;QAED,KAAmB,UAAK,EAAL,eAAK,EAAL,mBAAK,EAAL,IAAK,EAAE;YAArB,IAAM,IAAI,cAAA;YACb,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;gBAC3B,IAAI,GAAG,KAAK,KAAK,EAAE;oBACjB,OAAO,KAAK,CAAC;iBACd;aACF;iBAAM;gBACL,IAAI,GAAG,KAAK,cAAc,EAAE;oBAC1B,OAAO,IAAI,CAAC;iBACb;aACF;SACF;QAED,OAAO,GAAG,KAAK,KAAK,CAAC;IACvB,CAAC;IACH,eAAC;AAAD,CAAC,AA9DD,IA8DC;AA9DY,4BAAQ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guestUser.d.ts","sourceRoot":"","sources":["../../src/guestUser.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,
|
|
1
|
+
{"version":3,"file":"guestUser.d.ts","sourceRoot":"","sources":["../../src/guestUser.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,eAAO,MAAM,SAAS;;;;;;;;;CASrB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guestUser.js","sourceRoot":"","sources":["../../src/guestUser.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"guestUser.js","sourceRoot":"","sources":["../../src/guestUser.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA4B;AAEf,QAAA,SAAS,GAAG;IACvB,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;CACZ,CAAC"}
|
package/generated/index.ts
CHANGED
|
@@ -3,23 +3,18 @@
|
|
|
3
3
|
import '@proteinjs/reflection';
|
|
4
4
|
import 'moment';
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
/** Generate Source Graph */
|
|
8
7
|
|
|
9
|
-
const sourceGraph =
|
|
10
|
-
|
|
8
|
+
const sourceGraph =
|
|
9
|
+
'{"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"}]}';
|
|
11
10
|
|
|
12
11
|
/** Generate Source Links */
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
const sourceLinks = {
|
|
16
|
-
};
|
|
17
|
-
|
|
13
|
+
const sourceLinks = {};
|
|
18
14
|
|
|
19
15
|
/** Load Source Graph and Links */
|
|
20
16
|
|
|
21
17
|
import { SourceRepository } from '@proteinjs/reflection';
|
|
22
18
|
SourceRepository.merge(sourceGraph, sourceLinks);
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
export * from '../index';
|
|
20
|
+
export * from '../index';
|
package/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './src/UserAuth'
|
|
2
|
-
export * from './src/guestUser'
|
|
1
|
+
export * from './src/UserAuth';
|
|
2
|
+
export * from './src/guestUser';
|
package/jest.config.js
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"ts",
|
|
11
|
-
"tsx",
|
|
12
|
-
"js",
|
|
13
|
-
"jsx",
|
|
14
|
-
"json",
|
|
15
|
-
"node"
|
|
16
|
-
],
|
|
17
|
-
"testEnvironment": "node"
|
|
18
|
-
}
|
|
2
|
+
roots: ['<rootDir>/test'],
|
|
3
|
+
transform: {
|
|
4
|
+
'^.+\\.tsx?$': 'ts-jest',
|
|
5
|
+
},
|
|
6
|
+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
|
|
7
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
8
|
+
testEnvironment: 'node',
|
|
9
|
+
};
|
package/package.json
CHANGED
|
@@ -1,37 +1,43 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
2
|
+
"name": "@proteinjs/user-auth",
|
|
3
|
+
"version": "1.0.9",
|
|
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/",
|
|
20
|
+
"build": "reflection-build && tsc",
|
|
21
|
+
"watch": "reflection-watch"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@proteinjs/reflection": "1.1.1",
|
|
25
|
+
"moment": "2.29.4"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@proteinjs/reflection-build": "1.0.16",
|
|
29
|
+
"@types/jest": "29.5.5",
|
|
30
|
+
"@types/node": "14.0.13",
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "7.8.0",
|
|
32
|
+
"@typescript-eslint/parser": "7.8.0",
|
|
33
|
+
"eslint": "8.57.0",
|
|
34
|
+
"eslint-config-prettier": "9.1.0",
|
|
35
|
+
"eslint-plugin-prettier": "5.1.3",
|
|
36
|
+
"jest": "29.7.0",
|
|
37
|
+
"ts-jest": "29.1.1",
|
|
38
|
+
"typescript": "5.2.2"
|
|
39
|
+
},
|
|
40
|
+
"main": "./dist/generated/index.js",
|
|
41
|
+
"types": "./dist/generated/index.d.ts",
|
|
42
|
+
"gitHead": "64f1b34c95269d295ba43797c42c43d1c44433ea"
|
|
43
|
+
}
|
package/src/UserAuth.ts
CHANGED
|
@@ -1,70 +1,78 @@
|
|
|
1
|
-
import { Loadable, SourceRepository } from '@proteinjs/reflection'
|
|
2
|
-
import { guestUser } from './guestUser'
|
|
1
|
+
import { Loadable, SourceRepository } from '@proteinjs/reflection';
|
|
2
|
+
import { guestUser } from './guestUser';
|
|
3
3
|
|
|
4
4
|
export interface AuthenticatedUser {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
email: string;
|
|
6
|
+
roles: string[];
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface AuthenticatedUserRepo extends Loadable {
|
|
10
|
-
|
|
10
|
+
getUser(): AuthenticatedUser;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export const getAuthenticatedUserRepo = () =>
|
|
13
|
+
export const getAuthenticatedUserRepo = () =>
|
|
14
|
+
SourceRepository.get().object<AuthenticatedUserRepo | undefined>('@proteinjs/user-auth/AuthenticatedUserRepo');
|
|
14
15
|
|
|
15
16
|
export class UserAuth {
|
|
16
|
-
|
|
17
|
+
private static userRepo?: AuthenticatedUserRepo;
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return UserAuth.userRepo;
|
|
19
|
+
private static getUserRepo() {
|
|
20
|
+
if (!UserAuth.userRepo) {
|
|
21
|
+
UserAuth.userRepo = getAuthenticatedUserRepo();
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (!userRepo)
|
|
28
|
-
return true;
|
|
24
|
+
return UserAuth.userRepo;
|
|
25
|
+
}
|
|
29
26
|
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
static isLoggedIn(): boolean {
|
|
28
|
+
const userRepo = UserAuth.getUserRepo();
|
|
29
|
+
if (!userRepo) {
|
|
30
|
+
return true;
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
static hasRole(role: string): boolean {
|
|
38
|
-
const userRepo = UserAuth.getUserRepo();
|
|
39
|
-
if (!userRepo)
|
|
40
|
-
return true;
|
|
33
|
+
const user = userRepo.getUser();
|
|
34
|
+
return user.email != guestUser.email;
|
|
35
|
+
}
|
|
41
36
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
37
|
+
/**
|
|
38
|
+
* @return true if user has role or user has role 'admin'
|
|
39
|
+
*/
|
|
40
|
+
static hasRole(role: string): boolean {
|
|
41
|
+
const userRepo = UserAuth.getUserRepo();
|
|
42
|
+
if (!userRepo) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
const user = userRepo.getUser();
|
|
47
|
+
if (user.roles.includes('admin')) {
|
|
48
|
+
return true;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
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;
|
|
51
|
+
return user.roles.includes(role);
|
|
52
|
+
}
|
|
57
53
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
54
|
+
/**
|
|
55
|
+
* @param has (default) `all` - return true if user has all roles
|
|
56
|
+
* @param has `at least one` - return true if user has at least one role
|
|
57
|
+
*/
|
|
58
|
+
static hasRoles(roles: string[], has: 'all' | 'at least one' = 'all'): boolean {
|
|
59
|
+
const userRepo = UserAuth.getUserRepo();
|
|
60
|
+
if (!userRepo) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
67
63
|
|
|
68
|
-
|
|
64
|
+
for (const role of roles) {
|
|
65
|
+
if (!UserAuth.hasRole(role)) {
|
|
66
|
+
if (has === 'all') {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
if (has === 'at least one') {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
69
74
|
}
|
|
70
|
-
|
|
75
|
+
|
|
76
|
+
return has === 'all';
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/guestUser.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import moment from 'moment'
|
|
1
|
+
import moment from 'moment';
|
|
2
2
|
|
|
3
3
|
export const guestUser = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
4
|
+
name: 'Guest',
|
|
5
|
+
email: 'guest',
|
|
6
|
+
password: 'guest',
|
|
7
|
+
emailVerified: false,
|
|
8
|
+
roles: '',
|
|
9
|
+
created: moment(),
|
|
10
|
+
updated: moment(),
|
|
11
|
+
id: 'guest',
|
|
12
|
+
};
|
package/tsconfig.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
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
|
+
}
|