@roadmanjs/auth 0.0.22 → 0.0.23

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.
@@ -1,2 +1,3 @@
1
1
  export * from './firebaseToken';
2
2
  export * from './isAuth';
3
+ export * from './isAdmin';
@@ -16,4 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./firebaseToken"), exports);
18
18
  __exportStar(require("./isAuth"), exports);
19
+ __exportStar(require("./isAdmin"), exports);
20
+ // export * from './isOwner';
19
21
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/middlewares/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,2CAAyB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/middlewares/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,2CAAyB;AACzB,4CAA0B;AAC1B,6BAA6B"}
@@ -0,0 +1,2 @@
1
+ import { MiddlewareFn } from 'type-graphql';
2
+ export declare const isAdmin: MiddlewareFn;
@@ -0,0 +1,26 @@
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.isAdmin = void 0;
7
+ const user_1 = require("../user");
8
+ const get_1 = __importDefault(require("lodash/get"));
9
+ const logs_1 = require("@roadmanjs/logs");
10
+ const isAdmin = async ({ context }, next) => {
11
+ try {
12
+ const userId = (0, get_1.default)(context, 'payload.userId', '');
13
+ const user = await user_1.UserModel.findById(userId);
14
+ if (user && user.admin) {
15
+ (0, logs_1.log)(`user is admin`, user.id);
16
+ return next();
17
+ }
18
+ throw new Error('not admin');
19
+ }
20
+ catch (err) {
21
+ console.log(err);
22
+ return null;
23
+ }
24
+ };
25
+ exports.isAdmin = isAdmin;
26
+ //# sourceMappingURL=isAdmin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isAdmin.js","sourceRoot":"","sources":["../../src/middlewares/isAdmin.ts"],"names":[],"mappings":";;;;;;AACA,kCAAkC;AAClC,qDAA8B;AAC9B,0CAAoC;AAE7B,MAAM,OAAO,GAAiB,KAAK,EAAE,EAAC,OAAO,EAAM,EAAE,IAAI,EAAE,EAAE;IAChE,IAAI;QACA,MAAM,MAAM,GAAG,IAAA,aAAI,EAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,CAAC,CAAC;QACnD,MAAM,IAAI,GAAG,MAAM,gBAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC9C,IAAI,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE;YACpB,IAAA,UAAG,EAAC,eAAe,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;YAC9B,OAAO,IAAI,EAAE,CAAC;SACjB;QACD,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,CAAC;KAChC;IAAC,OAAO,GAAG,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,OAAO,IAAI,CAAC;KACf;AACL,CAAC,CAAC;AAbW,QAAA,OAAO,WAalB"}
@@ -0,0 +1,2 @@
1
+ import { MiddlewareFn } from 'type-graphql';
2
+ export declare const isOwner: MiddlewareFn;
@@ -0,0 +1,30 @@
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.isOwner = void 0;
7
+ const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
8
+ const jsonwebtoken_1 = require("jsonwebtoken");
9
+ // TODO apply contentModel, it's id
10
+ // TODO finish comparator
11
+ const isOwner = async ({ context }, next) => {
12
+ const authorization = context.req.headers['authorization'];
13
+ if (!authorization) {
14
+ throw new Error('No authorization');
15
+ }
16
+ try {
17
+ const token = authorization.split(' ')[1];
18
+ const verified = (0, jsonwebtoken_1.verify)(token, process.env.ACCESS_TOKEN_SECRET);
19
+ if ((0, isEmpty_1.default)(verified && verified.userId)) {
20
+ throw new Error('No authorization, please try again');
21
+ }
22
+ }
23
+ catch (err) {
24
+ console.log(err);
25
+ return null;
26
+ }
27
+ return next();
28
+ };
29
+ exports.isOwner = isOwner;
30
+ //# sourceMappingURL=isOwner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isOwner.js","sourceRoot":"","sources":["../../src/middlewares/isOwner.ts"],"names":[],"mappings":";;;;;;AACA,6DAAqC;AACrC,+CAAoC;AAEpC,mCAAmC;AACnC,yBAAyB;AAClB,MAAM,OAAO,GAAiB,KAAK,EAAE,EAAC,OAAO,EAAM,EAAE,IAAI,EAAE,EAAE;IAChE,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC;IAE3D,IAAI,CAAC,aAAa,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;KACvC;IAED,IAAI;QACA,MAAM,KAAK,GAAG,aAAa,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1C,MAAM,QAAQ,GAAQ,IAAA,qBAAM,EAAC,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAoB,CAAC,CAAC;QAEtE,IAAI,IAAA,iBAAO,EAAC,QAAQ,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE;YACtC,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;SACzD;KACJ;IAAC,OAAO,GAAG,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,OAAO,IAAI,CAAC;KACf;IACD,OAAO,IAAI,EAAE,CAAC;AAClB,CAAC,CAAC;AAnBW,QAAA,OAAO,WAmBlB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roadmanjs/auth",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "description": " An auth roadman using a UserType model in Couchbase and firebase-auth",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",