@nest-boot/auth 4.0.0-beta.8 → 4.0.0

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.
Files changed (32) hide show
  1. package/dist/auth.constants.d.ts +2 -0
  2. package/dist/auth.constants.js +6 -0
  3. package/dist/auth.constants.js.map +1 -0
  4. package/dist/auth.guard.d.ts +2 -2
  5. package/dist/auth.guard.js +19 -13
  6. package/dist/auth.guard.js.map +1 -1
  7. package/dist/auth.middleware.js +10 -11
  8. package/dist/auth.middleware.js.map +1 -1
  9. package/dist/auth.service.d.ts +4 -4
  10. package/dist/auth.service.js +11 -10
  11. package/dist/auth.service.js.map +1 -1
  12. package/dist/decorators/current-entity.decorator.js +2 -2
  13. package/dist/decorators/current-entity.decorator.js.map +1 -1
  14. package/dist/decorators/current-personal-access-token.decorator.d.ts +1 -1
  15. package/dist/decorators/current-personal-access-token.decorator.js +4 -4
  16. package/dist/decorators/current-personal-access-token.decorator.js.map +1 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.js +1 -0
  19. package/dist/index.js.map +1 -1
  20. package/dist/interfaces/{personal-access-token.interface.d.ts → access-token.interface.d.ts} +1 -1
  21. package/dist/interfaces/{auth-payload.interface.js → access-token.interface.js} +1 -1
  22. package/dist/interfaces/access-token.interface.js.map +1 -0
  23. package/dist/interfaces/auth-module-options.interface.d.ts +2 -4
  24. package/dist/interfaces/index.d.ts +1 -2
  25. package/dist/interfaces/index.js +1 -2
  26. package/dist/interfaces/index.js.map +1 -1
  27. package/dist/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +13 -11
  29. package/dist/interfaces/auth-payload.interface.d.ts +0 -6
  30. package/dist/interfaces/auth-payload.interface.js.map +0 -1
  31. package/dist/interfaces/personal-access-token.interface.js +0 -3
  32. package/dist/interfaces/personal-access-token.interface.js.map +0 -1
@@ -0,0 +1,2 @@
1
+ export declare const AUTH_ENTITY = "AUTH_ENTITY";
2
+ export declare const AUTH_ACCESS_TOKEN = "AUTH_ACCESS_TOKEN";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AUTH_ACCESS_TOKEN = exports.AUTH_ENTITY = void 0;
4
+ exports.AUTH_ENTITY = "AUTH_ENTITY";
5
+ exports.AUTH_ACCESS_TOKEN = "AUTH_ACCESS_TOKEN";
6
+ //# sourceMappingURL=auth.constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.constants.js","sourceRoot":"","sources":["../src/auth.constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,WAAW,GAAG,aAAa,CAAC;AAC5B,QAAA,iBAAiB,GAAG,mBAAmB,CAAC"}
@@ -1,5 +1,4 @@
1
1
  import { EntityManager } from "@mikro-orm/core";
2
- import { RequestContext } from "@nest-boot/request-context";
3
2
  import { CanActivate, ExecutionContext } from "@nestjs/common";
4
3
  import { Reflector } from "@nestjs/core";
5
4
  import { AuthModuleOptions } from "./interfaces";
@@ -8,6 +7,7 @@ export declare class AuthGuard implements CanActivate {
8
7
  private readonly reflector;
9
8
  private readonly entityManager;
10
9
  constructor(options: AuthModuleOptions, reflector: Reflector, entityManager: EntityManager);
10
+ private t;
11
11
  canActivate(executionContext: ExecutionContext): Promise<boolean>;
12
- getPermissions(ctx: RequestContext): Promise<string[]>;
12
+ getPermissions(): Promise<string[]>;
13
13
  }
@@ -17,10 +17,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.AuthGuard = void 0;
19
19
  const core_1 = require("@mikro-orm/core");
20
+ const i18n_1 = require("@nest-boot/i18n");
20
21
  const request_context_1 = require("@nest-boot/request-context");
21
22
  const common_1 = require("@nestjs/common");
22
23
  const core_2 = require("@nestjs/core");
23
24
  const lodash_1 = __importDefault(require("lodash"));
25
+ const auth_constants_1 = require("./auth.constants");
24
26
  const auth_module_definition_1 = require("./auth.module-definition");
25
27
  let AuthGuard = class AuthGuard {
26
28
  constructor(options, reflector, entityManager) {
@@ -29,38 +31,41 @@ let AuthGuard = class AuthGuard {
29
31
  this.entityManager = entityManager;
30
32
  this.reflector = reflector;
31
33
  }
34
+ t(key) {
35
+ var _a, _b;
36
+ return (_b = (_a = request_context_1.RequestContext.get(i18n_1.I18N)) === null || _a === void 0 ? void 0 : _a.t(key, { ns: "auth" })) !== null && _b !== void 0 ? _b : key;
37
+ }
32
38
  async canActivate(executionContext) {
33
39
  var _a;
34
40
  if (!["http", "graphql"].includes(executionContext.getType())) {
35
41
  return true;
36
42
  }
37
- const ctx = request_context_1.RequestContext.get();
38
43
  const requireAuth = this.reflector.get(auth_module_definition_1.REQUIRE_AUTH_METADATA_KEY, executionContext.getHandler());
39
44
  if (!((_a = requireAuth !== null && requireAuth !== void 0 ? requireAuth : this.options.defaultRequireAuth) !== null && _a !== void 0 ? _a : false)) {
40
45
  return true;
41
46
  }
42
- const authPayload = ctx.get("auth");
43
- if (typeof authPayload === "undefined") {
44
- return false;
47
+ const accessToken = request_context_1.RequestContext.get(auth_constants_1.AUTH_ACCESS_TOKEN);
48
+ if (typeof accessToken === "undefined") {
49
+ throw new common_1.UnauthorizedException(this.t("The access token is invalid."));
45
50
  }
46
51
  const permissions = this.reflector.get(auth_module_definition_1.PERMISSIONS_METADATA_KEY, executionContext.getHandler());
47
52
  if (typeof permissions === "undefined" ||
48
- lodash_1.default.intersection(await this.getPermissions(ctx), permissions).length > 0) {
49
- authPayload.personalAccessToken.lastUsedAt = new Date();
53
+ lodash_1.default.intersection(await this.getPermissions(), permissions).length > 0) {
54
+ accessToken.lastUsedAt = new Date();
50
55
  await this.entityManager.flush();
51
56
  return true;
52
57
  }
53
- return false;
58
+ throw new common_1.ForbiddenException(this.t("Permission denied."));
54
59
  }
55
- async getPermissions(ctx) {
60
+ async getPermissions() {
56
61
  let permissions = [];
57
- const payload = ctx.get("auth");
58
- if (typeof payload !== "undefined") {
59
- if (typeof payload.entity.permissions !== "undefined") {
60
- permissions = permissions.concat(payload.entity.permissions);
62
+ const entity = request_context_1.RequestContext.get(auth_constants_1.AUTH_ENTITY);
63
+ if (typeof entity !== "undefined") {
64
+ if (typeof entity.permissions !== "undefined") {
65
+ permissions = permissions.concat(entity.permissions);
61
66
  }
62
67
  else {
63
- const newPermissions = await core_1.Reference.create(payload.entity).load("permissions");
68
+ const newPermissions = await core_1.Reference.create(entity).load("permissions");
64
69
  if (typeof newPermissions !== "undefined") {
65
70
  permissions = permissions.concat(newPermissions);
66
71
  }
@@ -70,6 +75,7 @@ let AuthGuard = class AuthGuard {
70
75
  }
71
76
  };
72
77
  AuthGuard = __decorate([
78
+ (0, common_1.Injectable)(),
73
79
  __param(0, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
74
80
  __metadata("design:paramtypes", [Object, core_2.Reflector,
75
81
  core_1.EntityManager])
@@ -1 +1 @@
1
- {"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../src/auth.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,0CAA2D;AAC3D,gEAA4D;AAC5D,2CAAuE;AACvE,uCAAyC;AACzC,oDAAuB;AAEvB,qEAIkC;AAG3B,IAAM,SAAS,GAAf,MAAM,SAAS;IACpB,YAEmB,OAA0B,EAC1B,SAAoB,EACpB,aAA4B;QAF5B,YAAO,GAAP,OAAO,CAAmB;QAC1B,cAAS,GAAT,SAAS,CAAW;QACpB,kBAAa,GAAb,aAAa,CAAe;QAE7C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAEM,KAAK,CAAC,WAAW,CACtB,gBAAkC;;QAElC,IAAI,CAAC,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,EAAE;YAC7D,OAAO,IAAI,CAAC;SACb;QAGD,MAAM,GAAG,GAAG,gCAAc,CAAC,GAAG,EAAE,CAAC;QAGjC,MAAM,WAAW,GAAG,IAA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GAAG,MAAM,gBAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CACxD,aAAa,CACd,CAAC;gBAEF,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE;oBACzC,WAAW,GAAG,WAAW,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;iBAClD;aACF;SACF;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;CACF,CAAA;AAlFY,SAAS;IADrB,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,6CAAoB,CAAC,CAAA;6CAED,gBAAS;QACL,oBAAa;GALpC,SAAS,CAkFrB;AAlFY,8BAAS"}
@@ -16,6 +16,7 @@ exports.AuthMiddleware = void 0;
16
16
  const core_1 = require("@mikro-orm/core");
17
17
  const request_context_1 = require("@nest-boot/request-context");
18
18
  const common_1 = require("@nestjs/common");
19
+ const auth_constants_1 = require("./auth.constants");
19
20
  const auth_module_definition_1 = require("./auth.module-definition");
20
21
  const auth_service_1 = require("./auth.service");
21
22
  let AuthMiddleware = class AuthMiddleware {
@@ -25,22 +26,20 @@ let AuthMiddleware = class AuthMiddleware {
25
26
  this.authService = authService;
26
27
  }
27
28
  async use(req, res, next) {
28
- const accessToken = this.extractAccessToken(req);
29
- if (accessToken !== null) {
30
- const personalAccessToken = await this.authService.getToken(accessToken);
31
- if (personalAccessToken !== null) {
29
+ const accessTokenString = this.extractAccessToken(req);
30
+ if (accessTokenString !== null) {
31
+ const accessToken = await this.authService.getToken(accessTokenString);
32
+ if (accessToken !== null) {
33
+ request_context_1.RequestContext.set(auth_constants_1.AUTH_ACCESS_TOKEN, accessToken);
32
34
  const entity = await this.entityManager
33
- .getRepository(personalAccessToken.entityName)
34
- .findOne(personalAccessToken.entityId);
35
+ .getRepository(accessToken.entityName)
36
+ .findOne(accessToken.entityId);
35
37
  if (entity !== null) {
36
- request_context_1.RequestContext.set("auth", {
37
- entity,
38
- personalAccessToken,
39
- });
38
+ request_context_1.RequestContext.set(auth_constants_1.AUTH_ENTITY, entity);
40
39
  }
41
40
  }
42
41
  }
43
- return next();
42
+ next();
44
43
  }
45
44
  extractAccessToken(req) {
46
45
  var _a;
@@ -1 +1 @@
1
- {"version":3,"file":"auth.middleware.js","sourceRoot":"","sources":["../src/auth.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,0CAAgD;AAChD,gEAA4D;AAC5D,2CAA2E;AAG3E,qEAAgE;AAChE,iDAA6C;AAItC,IAAM,cAAc,GAApB,MAAM,cAAc;IACzB,YAEmB,OAA0B,EAC1B,aAA4B,EAC5B,WAAwB;QAFxB,YAAO,GAAP,OAAO,CAAmB;QAC1B,kBAAa,GAAb,aAAa,CAAe;QAC5B,gBAAW,GAAX,WAAW,CAAa;IACxC,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,GAAY,EAAE,GAAa,EAAE,IAAgB;QACrD,MAAM,WAAW,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC;QAEjD,IAAI,WAAW,KAAK,IAAI,EAAE;YACxB,MAAM,mBAAmB,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;YAEzE,IAAI,mBAAmB,KAAK,IAAI,EAAE;gBAChC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,aAAa;qBACpC,aAAa,CAAC,mBAAmB,CAAC,UAAU,CAAC;qBAC7C,OAAO,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;gBAGzC,IAAI,MAAM,KAAK,IAAI,EAAE;oBACnB,gCAAc,CAAC,GAAG,CAAc,MAAM,EAAE;wBACtC,MAAM;wBACN,mBAAmB;qBACpB,CAAC,CAAC;iBACJ;aACF;SACF;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC;IAEO,kBAAkB,CAAC,GAAY;;QACrC,MAAM,mBAAmB,GAAG,GAAG,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;QACrD,IAAI,OAAO,mBAAmB,KAAK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aded } from "@mikro-orm/core";
2
- import { AuthModuleOptions, PersonalAccessTokenInterface } from "./interfaces";
2
+ import { AccessTokenInterface, AuthModuleOptions } from "./interfaces";
3
3
  export declare class AuthService {
4
4
  private readonly options;
5
5
  private readonly entityManager;
6
- private readonly personalAccessTokenEntity;
6
+ private readonly accessTokenRepository;
7
7
  constructor(options: AuthModuleOptions, entityManager: EntityManager);
8
- createToken(entity: AnyEntity, name: string, expiresIn?: string | number): Promise<PersonalAccessTokenInterface>;
9
- getToken(token?: string): Promise<Loaded<PersonalAccessTokenInterface> | null>;
8
+ createToken(entity: AnyEntity, name: string, expiresIn?: string | number): Promise<AccessTokenInterface>;
9
+ getToken(token?: string): Promise<Loaded<AccessTokenInterface> | null>;
10
10
  verifyToken(token?: string): Promise<boolean>;
11
11
  revokeToken(token?: string): Promise<void>;
12
12
  }
@@ -19,40 +19,41 @@ const crypto_1 = require("crypto");
19
19
  const auth_module_definition_1 = require("./auth.module-definition");
20
20
  const ms = require("ms");
21
21
  const request_context_1 = require("@nest-boot/request-context");
22
+ const auth_constants_1 = require("./auth.constants");
22
23
  let AuthService = class AuthService {
23
24
  constructor(options, entityManager) {
24
25
  this.options = options;
25
26
  this.entityManager = entityManager;
26
- this.personalAccessTokenEntity = this.entityManager.getRepository(this.options.personalAccessTokenEntity);
27
+ this.accessTokenRepository = this.entityManager.getRepository(this.options.accessTokenEntityClass);
27
28
  }
28
29
  async createToken(entity, name, expiresIn) {
29
30
  const token = (0, crypto_1.randomBytes)(32).toString("hex");
30
31
  const _expiresIn = expiresIn !== null && expiresIn !== void 0 ? expiresIn : this.options.expiresIn;
31
32
  const _expiresInMs = typeof _expiresIn === "string" ? ms(_expiresIn) : _expiresIn;
32
- const personalAccessToken = this.personalAccessTokenEntity.create({
33
+ const accessToken = this.accessTokenRepository.create({
33
34
  name,
34
35
  token,
35
36
  entityId: entity.id,
36
37
  entityName: entity.constructor.name,
37
38
  expiresAt: _expiresInMs != null ? new Date(Date.now() + _expiresInMs) : undefined,
38
39
  });
39
- await this.personalAccessTokenEntity.persistAndFlush(personalAccessToken);
40
- return personalAccessToken;
40
+ await this.accessTokenRepository.persistAndFlush(accessToken);
41
+ return accessToken;
41
42
  }
42
43
  async getToken(token) {
43
- var _a, _b;
44
+ var _a;
44
45
  if (typeof token === "undefined") {
45
- return ((_b = (_a = request_context_1.RequestContext.get("auth")) === null || _a === void 0 ? void 0 : _a.personalAccessToken) !== null && _b !== void 0 ? _b : null);
46
+ return ((_a = request_context_1.RequestContext.get(auth_constants_1.AUTH_ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : null);
46
47
  }
47
- return await this.personalAccessTokenEntity.findOne({ token });
48
+ return await this.accessTokenRepository.findOne({ token });
48
49
  }
49
50
  async verifyToken(token) {
50
51
  return (await this.getToken(token)) !== null;
51
52
  }
52
53
  async revokeToken(token) {
53
- const personalAccessToken = await this.getToken(token);
54
- if (personalAccessToken !== null) {
55
- await this.personalAccessTokenEntity.remove(personalAccessToken).flush();
54
+ const accessToken = await this.getToken(token);
55
+ if (accessToken !== null) {
56
+ await this.accessTokenRepository.remove(accessToken).flush();
56
57
  }
57
58
  }
58
59
  };
@@ -1 +1 @@
1
- {"version":3,"file":"auth.service.js","sourceRoot":"","sources":["../src/auth.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,0CAKyB;AACzB,2CAAoD;AACpD,mCAAqC;AAErC,qEAAgE;AAMhE,yBAA0B;AAC1B,gEAA4D;AAGrD,IAAM,WAAW,GAAjB,MAAM,WAAW;IAGtB,YAEmB,OAA0B,EAC1B,aAA4B;QAD5B,YAAO,GAAP,OAAO,CAAmB;QAC1B,kBAAa,GAAb,aAAa,CAAe;QAE7C,IAAI,CAAC,yBAAyB,GAAG,IAAI,CAAC,aAAa,CAAC,aAAa,CAC/D,IAAI,CAAC,OAAO,CAAC,yBAAyB,CACvC,CAAC;IACJ,CAAC;IAQD,KAAK,CAAC,WAAW,CACf,MAAiB,EACjB,IAAY,EACZ,SAA2B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ineProperty(exports, "__esModule", { value: true });
3
3
  exports.CurrentUser = exports.CurrentEntity = void 0;
4
4
  const request_context_1 = require("@nest-boot/request-context");
5
5
  const common_1 = require("@nestjs/common");
6
+ const auth_constants_1 = require("../auth.constants");
6
7
  exports.CurrentEntity = (0, common_1.createParamDecorator)(() => {
7
- var _a;
8
- return (_a = request_context_1.RequestContext.get("auth")) === null || _a === void 0 ? void 0 : _a.entity;
8
+ return request_context_1.RequestContext.get(auth_constants_1.AUTH_ENTITY);
9
9
  });
10
10
  exports.CurrentUser = exports.CurrentEntity;
11
11
  //# sourceMappingURL=current-entity.decorator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"current-entity.decorator.js","sourceRoot":"","sources":["../../src/decorators/current-entity.decorator.ts"],"names":[],"mappings":";;;AAAA,gEAA4D;AAC5D,2CAAsD;AAIzC,QAAA,aAAa,GAAG,IAAA,6BAAoB,EAAC,GAAG,EAAE;;IACrD,OAAO,MAAA,gCAAc,CAAC,GAAG,CAAc,MAAM,CAAC,0CAAE,MAAM,CAAC;AACzD,CAAC,CAAC,CAAC;AAEU,QAAA,WAAW,GAAG,qBAAa,CAAC"}
1
+ {"version":3,"file":"current-entity.decorator.js","sourceRoot":"","sources":["../../src/decorators/current-entity.decorator.ts"],"names":[],"mappings":";;;AAAA,gEAA4D;AAC5D,2CAAsD;AAEtD,sDAAgD;AAEnC,QAAA,aAAa,GAAG,IAAA,6BAAoB,EAAC,GAAG,EAAE;IACrD,OAAO,gCAAc,CAAC,GAAG,CAAC,4BAAW,CAAC,CAAC;AACzC,CAAC,CAAC,CAAC;AAEU,QAAA,WAAW,GAAG,qBAAa,CAAC"}
@@ -1 +1 @@
1
- export declare const CurrentPersonalAccessToken: (...dataOrPipes: any[]) => ParameterDecorator;
1
+ export declare const CurrentAccessToken: (...dataOrPipes: any[]) => ParameterDecorator;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CurrentPersonalAccessToken = void 0;
3
+ exports.CurrentAccessToken = void 0;
4
4
  const request_context_1 = require("@nest-boot/request-context");
5
5
  const common_1 = require("@nestjs/common");
6
- exports.CurrentPersonalAccessToken = (0, common_1.createParamDecorator)(() => {
7
- var _a;
8
- return (_a = request_context_1.RequestContext.get("auth")) === null || _a === void 0 ? void 0 : _a.personalAccessToken;
6
+ const auth_constants_1 = require("../auth.constants");
7
+ exports.CurrentAccessToken = (0, common_1.createParamDecorator)(() => {
8
+ return request_context_1.RequestContext.get(auth_constants_1.AUTH_ACCESS_TOKEN);
9
9
  });
10
10
  //# sourceMappingURL=current-personal-access-token.decorator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"current-personal-access-token.decorator.js","sourceRoot":"","sources":["../../src/decorators/current-personal-access-token.decorator.ts"],"names":[],"mappings":";;;AAAA,gEAA4D;AAC5D,2CAAsD;AAIzC,QAAA,0BAA0B,GAAG,IAAA,6BAAoB,EAAC,GAAG,EAAE;;IAClE,OAAO,MAAA,gCAAc,CAAC,GAAG,CAAc,MAAM,CAAC,0CAAE,mBAAmB,CAAC;AACtE,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"current-personal-access-token.decorator.js","sourceRoot":"","sources":["../../src/decorators/current-personal-access-token.decorator.ts"],"names":[],"mappings":";;;AAAA,gEAA4D;AAC5D,2CAAsD;AAEtD,sDAAsD;AAGzC,QAAA,kBAAkB,GAAG,IAAA,6BAAoB,EAAC,GAAG,EAAE;IAC1D,OAAO,gCAAc,CAAC,GAAG,CAAuB,kCAAiB,CAAC,CAAC;AACrE,CAAC,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./auth.constants";
1
2
  export * from "./auth.guard";
2
3
  export * from "./auth.middleware";
3
4
  export * from "./auth.module";
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ 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("./auth.constants"), exports);
17
18
  __exportStar(require("./auth.guard"), exports);
18
19
  __exportStar(require("./auth.middleware"), exports);
19
20
  __exportStar(require("./auth.module"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,oDAAkC;AAClC,gDAA8B;AAC9B,iDAA+B;AAC/B,+CAA6B;AAC7B,+CAA6B;AAC7B,0CAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,+CAA6B;AAC7B,oDAAkC;AAClC,gDAA8B;AAC9B,iDAA+B;AAC/B,+CAA6B;AAC7B,+CAA6B;AAC7B,0CAAwB"}
@@ -1,4 +1,4 @@
1
- export interface PersonalAccessTokenInterface {
1
+ export interface AccessTokenInterface {
2
2
  id: string;
3
3
  name: string;
4
4
  token: string;
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=auth-payload.interface.js.map
3
+ //# sourceMappingURL=access-token.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"access-token.interface.js","sourceRoot":"","sources":["../../src/interfaces/access-token.interface.ts"],"names":[],"mappings":""}
@@ -1,10 +1,8 @@
1
- /// <reference types="node" />
2
1
  import { EntityClass } from "@mikro-orm/core";
3
2
  import { RouteInfo, Type } from "@nestjs/common/interfaces";
4
- import { PersonalAccessTokenInterface } from "./personal-access-token.interface";
3
+ import { AccessTokenInterface } from "./access-token.interface";
5
4
  export interface AuthModuleOptions {
6
- personalAccessTokenEntity: EntityClass<PersonalAccessTokenInterface>;
7
- secret: string | Buffer;
5
+ accessTokenEntityClass: EntityClass<AccessTokenInterface>;
8
6
  expiresIn?: string | number;
9
7
  defaultRequireAuth?: boolean;
10
8
  includeRoutes?: Array<string | Type<any> | RouteInfo>;
@@ -1,4 +1,3 @@
1
+ export * from "./access-token.interface";
1
2
  export * from "./auth-module-options.interface";
2
- export * from "./auth-payload.interface";
3
3
  export * from "./has-permission.interface";
4
- export * from "./personal-access-token.interface";
@@ -14,8 +14,7 @@ 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("./access-token.interface"), exports);
17
18
  __exportStar(require("./auth-module-options.interface"), exports);
18
- __exportStar(require("./auth-payload.interface"), exports);
19
19
  __exportStar(require("./has-permission.interface"), exports);
20
- __exportStar(require("./personal-access-token.interface"), exports);
21
20
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kEAAgD;AAChD,2DAAyC;AACzC,6DAA2C;AAC3C,oEAAkD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAyC;AACzC,kEAAgD;AAChD,6DAA2C"}