@flusys/nestjs-auth 6.0.1 → 6.1.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 (83) hide show
  1. package/cjs/controllers/authentication.controller.spec.js +219 -0
  2. package/cjs/controllers/branch.controller.spec.js +134 -0
  3. package/cjs/controllers/company-selection.controller.spec.js +84 -0
  4. package/cjs/controllers/company.controller.spec.js +131 -0
  5. package/cjs/controllers/email-verification.controller.spec.js +142 -0
  6. package/cjs/controllers/user-permission.controller.spec.js +138 -0
  7. package/cjs/controllers/user.controller.spec.js +204 -0
  8. package/cjs/docs/auth-swagger.config.spec.js +130 -0
  9. package/cjs/entities/company-branch.entity.js +2 -3
  10. package/cjs/entities/index.spec.js +73 -0
  11. package/cjs/helpers/auth-response.mapper.js +46 -0
  12. package/cjs/helpers/auth-response.mapper.spec.js +139 -0
  13. package/cjs/helpers/index.js +19 -0
  14. package/cjs/helpers/token.helper.js +58 -0
  15. package/cjs/helpers/token.helper.spec.js +86 -0
  16. package/cjs/index.js +1 -0
  17. package/cjs/interceptors/clear-token.interceptor.spec.js +107 -0
  18. package/cjs/interceptors/set-token.interceptor.spec.js +165 -0
  19. package/cjs/modules/auth.module.spec.js +243 -0
  20. package/cjs/services/auth-config.service.spec.js +97 -0
  21. package/cjs/services/auth-datasource.provider.spec.js +161 -0
  22. package/cjs/services/auth-email.service.spec.js +404 -0
  23. package/cjs/services/authentication.service.js +37 -109
  24. package/cjs/services/authentication.service.spec.js +904 -0
  25. package/cjs/services/branch.service.js +30 -3
  26. package/cjs/services/branch.service.spec.js +154 -0
  27. package/cjs/services/company-selection-session.service.spec.js +90 -0
  28. package/cjs/services/company.service.js +34 -3
  29. package/cjs/services/company.service.spec.js +113 -0
  30. package/cjs/services/user-permission.service.js +35 -4
  31. package/cjs/services/user-permission.service.spec.js +202 -0
  32. package/cjs/services/user.service.spec.js +704 -0
  33. package/cjs/strategies/jwt.strategy.js +1 -1
  34. package/cjs/strategies/jwt.strategy.spec.js +166 -0
  35. package/cjs/validators/password.validator.spec.js +161 -0
  36. package/entities/company-branch.entity.d.ts +1 -1
  37. package/entities/pending-registration.entity.d.ts +2 -7
  38. package/fesm/controllers/authentication.controller.spec.js +215 -0
  39. package/fesm/controllers/branch.controller.spec.js +130 -0
  40. package/fesm/controllers/company-selection.controller.spec.js +80 -0
  41. package/fesm/controllers/company.controller.spec.js +127 -0
  42. package/fesm/controllers/email-verification.controller.spec.js +138 -0
  43. package/fesm/controllers/user-permission.controller.spec.js +134 -0
  44. package/fesm/controllers/user.controller.spec.js +200 -0
  45. package/fesm/docs/auth-swagger.config.spec.js +126 -0
  46. package/fesm/entities/company-branch.entity.js +2 -3
  47. package/fesm/entities/index.spec.js +69 -0
  48. package/fesm/helpers/auth-response.mapper.js +25 -0
  49. package/fesm/helpers/auth-response.mapper.spec.js +135 -0
  50. package/fesm/helpers/index.js +2 -0
  51. package/fesm/helpers/token.helper.js +40 -0
  52. package/fesm/helpers/token.helper.spec.js +82 -0
  53. package/fesm/index.js +1 -0
  54. package/fesm/interceptors/clear-token.interceptor.spec.js +103 -0
  55. package/fesm/interceptors/set-token.interceptor.spec.js +161 -0
  56. package/fesm/interfaces/authentication.interface.js +1 -1
  57. package/fesm/modules/auth.module.spec.js +239 -0
  58. package/fesm/services/auth-config.service.spec.js +93 -0
  59. package/fesm/services/auth-datasource.provider.spec.js +157 -0
  60. package/fesm/services/auth-email.service.spec.js +359 -0
  61. package/fesm/services/authentication.service.js +37 -109
  62. package/fesm/services/authentication.service.spec.js +859 -0
  63. package/fesm/services/branch.service.js +32 -5
  64. package/fesm/services/branch.service.spec.js +150 -0
  65. package/fesm/services/company-selection-session.service.spec.js +86 -0
  66. package/fesm/services/company.service.js +36 -5
  67. package/fesm/services/company.service.spec.js +109 -0
  68. package/fesm/services/user-permission.service.js +36 -5
  69. package/fesm/services/user-permission.service.spec.js +198 -0
  70. package/fesm/services/user.service.spec.js +659 -0
  71. package/fesm/strategies/jwt.strategy.js +1 -1
  72. package/fesm/strategies/jwt.strategy.spec.js +162 -0
  73. package/fesm/validators/password.validator.spec.js +157 -0
  74. package/helpers/auth-response.mapper.d.ts +22 -0
  75. package/helpers/index.d.ts +2 -0
  76. package/helpers/token.helper.d.ts +8 -0
  77. package/index.d.ts +1 -0
  78. package/interfaces/authentication.interface.d.ts +7 -0
  79. package/package.json +8 -3
  80. package/services/authentication.service.d.ts +1 -8
  81. package/services/branch.service.d.ts +7 -5
  82. package/services/company.service.d.ts +7 -4
  83. package/services/user-permission.service.d.ts +4 -1
@@ -25,6 +25,7 @@ const _companyentity = require("../entities/company.entity");
25
25
  const _pendingregistrationentity = require("../entities/pending-registration.entity");
26
26
  const _usercompanypermissionentity = require("../entities/user-company-permission.entity");
27
27
  const _enums1 = require("../enums");
28
+ const _helpers = require("../helpers");
28
29
  const _userenricherinterface = require("../interfaces/user-enricher.interface");
29
30
  const _authconfigservice = require("./auth-config.service");
30
31
  const _authdatasourceprovider = require("./auth-datasource.provider");
@@ -119,17 +120,9 @@ let AuthenticationService = class AuthenticationService {
119
120
  }
120
121
  return this.dataSourceProvider.getRepository(_appuserentity.AppUser);
121
122
  }
122
- async getPermissionRepository() {
123
+ async getCompanyScopedRepository(entity) {
123
124
  if (!this.isCompanyFeatureEnabled() || !this.dataSourceProvider) return null;
124
- return this.dataSourceProvider.getRepository(_usercompanypermissionentity.UserCompanyPermission);
125
- }
126
- async getCompanyRepository() {
127
- if (!this.isCompanyFeatureEnabled() || !this.dataSourceProvider) return null;
128
- return this.dataSourceProvider.getRepository(_companyentity.Company);
129
- }
130
- async getBranchRepository() {
131
- if (!this.isCompanyFeatureEnabled() || !this.dataSourceProvider) return null;
132
- return this.dataSourceProvider.getRepository(_companybranchentity.CompanyBranch);
125
+ return this.dataSourceProvider.getRepository(entity);
133
126
  }
134
127
  async login(email, password) {
135
128
  if (this.isEmailVerificationEnabled() && await this.authEmailService.hasPendingRegistration(email)) {
@@ -161,8 +154,8 @@ let AuthenticationService = class AuthenticationService {
161
154
  return await this.handleCompanyBasedLogin(user);
162
155
  }
163
156
  async handleCompanyBasedLogin(user) {
164
- const permissionRepository = await this.getPermissionRepository();
165
- const branchRepository = await this.getBranchRepository();
157
+ const permissionRepository = await this.getCompanyScopedRepository(_usercompanypermissionentity.UserCompanyPermission);
158
+ const branchRepository = await this.getCompanyScopedRepository(_companybranchentity.CompanyBranch);
166
159
  if (!permissionRepository || !branchRepository) {
167
160
  return this.generateLoginResponse(user);
168
161
  }
@@ -172,7 +165,7 @@ let AuthenticationService = class AuthenticationService {
172
165
  permissionType: _enums1.PermissionType.COMPANY
173
166
  }
174
167
  });
175
- const companyRepository = await this.getCompanyRepository();
168
+ const companyRepository = await this.getCompanyScopedRepository(_companyentity.Company);
176
169
  if (!companyRepository) {
177
170
  return this.generateLoginResponse(user);
178
171
  }
@@ -250,8 +243,8 @@ let AuthenticationService = class AuthenticationService {
250
243
  messageKey: _config.AUTH_MESSAGES.REGISTER_EMAIL_EXISTS
251
244
  });
252
245
  }
253
- const permissionRepository = await this.getPermissionRepository();
254
- const companyRepository = await this.getCompanyRepository();
246
+ const permissionRepository = await this.getCompanyScopedRepository(_usercompanypermissionentity.UserCompanyPermission);
247
+ const companyRepository = await this.getCompanyScopedRepository(_companyentity.Company);
255
248
  if (!permissionRepository || !companyRepository) {
256
249
  throw new _common.BadRequestException({
257
250
  message: 'Company feature repositories not available',
@@ -385,9 +378,9 @@ let AuthenticationService = class AuthenticationService {
385
378
  }
386
379
  }
387
380
  async processCompanyRegistration(user, data, throwOnUnavailable = false) {
388
- const companyRepository = await this.getCompanyRepository();
389
- const branchRepository = await this.getBranchRepository();
390
- const permissionRepository = await this.getPermissionRepository();
381
+ const companyRepository = await this.getCompanyScopedRepository(_companyentity.Company);
382
+ const branchRepository = await this.getCompanyScopedRepository(_companybranchentity.CompanyBranch);
383
+ const permissionRepository = await this.getCompanyScopedRepository(_usercompanypermissionentity.UserCompanyPermission);
391
384
  if (!companyRepository || !branchRepository || !permissionRepository) {
392
385
  if (throwOnUnavailable) throw new _common.BadRequestException({
393
386
  message: 'Company feature repositories not available',
@@ -523,9 +516,9 @@ let AuthenticationService = class AuthenticationService {
523
516
  messageKey: _config.USER_MESSAGES.NOT_FOUND
524
517
  });
525
518
  }
526
- const permissionRepository = await this.getPermissionRepository();
527
- const companyRepository = await this.getCompanyRepository();
528
- const branchRepository = await this.getBranchRepository();
519
+ const permissionRepository = await this.getCompanyScopedRepository(_usercompanypermissionentity.UserCompanyPermission);
520
+ const companyRepository = await this.getCompanyScopedRepository(_companyentity.Company);
521
+ const branchRepository = await this.getCompanyScopedRepository(_companybranchentity.CompanyBranch);
529
522
  if (!permissionRepository || !companyRepository || !branchRepository) {
530
523
  throw new _common.BadRequestException({
531
524
  message: 'Repositories not available',
@@ -615,8 +608,8 @@ let AuthenticationService = class AuthenticationService {
615
608
  });
616
609
  }
617
610
  if (payload.companyId) {
618
- const companyRepository = await this.getCompanyRepository();
619
- const permissionRepository = await this.getPermissionRepository();
611
+ const companyRepository = await this.getCompanyScopedRepository(_companyentity.Company);
612
+ const permissionRepository = await this.getCompanyScopedRepository(_usercompanypermissionentity.UserCompanyPermission);
620
613
  if (companyRepository) {
621
614
  const company = await companyRepository.findOne({
622
615
  where: {
@@ -647,7 +640,7 @@ let AuthenticationService = class AuthenticationService {
647
640
  }
648
641
  }
649
642
  if (payload.branchId) {
650
- const branchRepository = await this.getBranchRepository();
643
+ const branchRepository = await this.getCompanyScopedRepository(_companybranchentity.CompanyBranch);
651
644
  if (branchRepository) {
652
645
  const branch = await branchRepository.findOne({
653
646
  where: {
@@ -680,7 +673,7 @@ let AuthenticationService = class AuthenticationService {
680
673
  }
681
674
  }
682
675
  }
683
- return this.generateTokens({
676
+ return (0, _helpers.generateTokens)(this.jwtService, this.authConfig, {
684
677
  id: user.id,
685
678
  email: user.email,
686
679
  type: 'access',
@@ -729,7 +722,7 @@ let AuthenticationService = class AuthenticationService {
729
722
  const revokedAt = Math.floor(Date.now() / 1000);
730
723
  const cacheKey = `${_config.TOKEN_REVOKED_PREFIX}:${userId}`;
731
724
  const refreshTokenExpiry = this.authConfig.getRefreshTokenExpiration();
732
- const ttlMs = this.parseExpiryToMs(refreshTokenExpiry);
725
+ const ttlMs = (0, _helpers.parseExpiryToMs)(refreshTokenExpiry);
733
726
  await this.cacheManager.set(cacheKey, revokedAt, ttlMs);
734
727
  }
735
728
  async isTokenRevoked(userId, tokenIssuedAt) {
@@ -743,28 +736,10 @@ let AuthenticationService = class AuthenticationService {
743
736
  }
744
737
  return tokenIssuedAt < revokedAt;
745
738
  }
746
- parseExpiryToMs(expiry) {
747
- const match = expiry.match(/^(\d+)([smhd])$/);
748
- if (!match) return 7 * 24 * 60 * 60 * 1000;
749
- const value = parseInt(match[1], 10);
750
- const unit = match[2];
751
- switch(unit){
752
- case 's':
753
- return value * 1000;
754
- case 'm':
755
- return value * 60 * 1000;
756
- case 'h':
757
- return value * 60 * 60 * 1000;
758
- case 'd':
759
- return value * 24 * 60 * 60 * 1000;
760
- default:
761
- return 7 * 24 * 60 * 60 * 1000;
762
- }
763
- }
764
739
  async getUserCompanies(userId) {
765
740
  this.ensureCompanyFeatureEnabled();
766
- const permissionRepository = await this.getPermissionRepository();
767
- const companyRepository = await this.getCompanyRepository();
741
+ const permissionRepository = await this.getCompanyScopedRepository(_usercompanypermissionentity.UserCompanyPermission);
742
+ const companyRepository = await this.getCompanyScopedRepository(_companyentity.Company);
768
743
  if (!permissionRepository || !companyRepository) {
769
744
  throw new _common.BadRequestException({
770
745
  message: 'Repositories not available',
@@ -790,8 +765,8 @@ let AuthenticationService = class AuthenticationService {
790
765
  }
791
766
  async getUserBranches(userId, companyId) {
792
767
  this.ensureCompanyFeatureEnabled();
793
- const permissionRepository = await this.getPermissionRepository();
794
- const branchRepository = await this.getBranchRepository();
768
+ const permissionRepository = await this.getCompanyScopedRepository(_usercompanypermissionentity.UserCompanyPermission);
769
+ const branchRepository = await this.getCompanyScopedRepository(_companybranchentity.CompanyBranch);
795
770
  if (!permissionRepository || !branchRepository) {
796
771
  throw new _common.BadRequestException({
797
772
  message: 'Repositories not available',
@@ -831,7 +806,7 @@ let AuthenticationService = class AuthenticationService {
831
806
  }).orderBy('b.serial', 'ASC').addOrderBy('b.name', 'ASC').getMany();
832
807
  }
833
808
  async getCompanyById(companyId) {
834
- const companyRepository = await this.getCompanyRepository();
809
+ const companyRepository = await this.getCompanyScopedRepository(_companyentity.Company);
835
810
  if (!companyRepository) {
836
811
  return null;
837
812
  }
@@ -842,7 +817,7 @@ let AuthenticationService = class AuthenticationService {
842
817
  });
843
818
  }
844
819
  async getBranchById(branchId) {
845
- const branchRepository = await this.getBranchRepository();
820
+ const branchRepository = await this.getCompanyScopedRepository(_companybranchentity.CompanyBranch);
846
821
  if (!branchRepository) {
847
822
  return null;
848
823
  }
@@ -864,7 +839,7 @@ let AuthenticationService = class AuthenticationService {
864
839
  messageKey: _config.USER_MESSAGES.NOT_FOUND
865
840
  });
866
841
  const response = {
867
- user: this.toUserInfo(user)
842
+ user: (0, _helpers.toUserInfo)(user)
868
843
  };
869
844
  if (this.isCompanyFeatureEnabled()) {
870
845
  if (!companyId) {
@@ -878,14 +853,14 @@ let AuthenticationService = class AuthenticationService {
878
853
  message: 'Company not found.',
879
854
  messageKey: _config.AUTH_MESSAGES.COMPANY_NOT_FOUND
880
855
  });
881
- response.company = this.toCompanyInfo(company);
856
+ response.company = (0, _helpers.toCompanyInfo)(company);
882
857
  if (branchId) {
883
858
  const branch = await this.getBranchById(branchId);
884
859
  if (!branch) throw new _common.UnauthorizedException({
885
860
  message: 'Branch not found.',
886
861
  messageKey: _config.AUTH_MESSAGES.BRANCH_NOT_FOUND
887
862
  });
888
- response.branch = this.toBranchInfo(branch);
863
+ response.branch = (0, _helpers.toBranchInfo)(branch);
889
864
  }
890
865
  }
891
866
  return response;
@@ -915,55 +890,8 @@ let AuthenticationService = class AuthenticationService {
915
890
  await userRepository.save(user);
916
891
  return user;
917
892
  }
918
- toUserInfo(user) {
919
- return {
920
- id: user.id,
921
- email: user.email,
922
- name: user.name ?? undefined,
923
- phone: user.phone ?? null,
924
- profilePictureId: user.profilePictureId ?? null
925
- };
926
- }
927
- toCompanyInfo(company) {
928
- return {
929
- id: company.id,
930
- name: company.name,
931
- slug: company.slug,
932
- logoId: company.logoId ?? null
933
- };
934
- }
935
- toBranchInfo(branch) {
936
- return {
937
- id: branch.id,
938
- name: branch.name,
939
- slug: branch.slug,
940
- logoId: branch.logoId ?? null
941
- };
942
- }
943
- generateTokens(payload) {
944
- const accessToken = this.jwtService.sign({
945
- ...payload,
946
- type: 'access'
947
- }, {
948
- secret: this.authConfig.getJwtSecret(),
949
- expiresIn: this.authConfig.getJwtExpiration(),
950
- algorithm: 'HS256'
951
- });
952
- const refreshToken = this.jwtService.sign({
953
- ...payload,
954
- type: 'refresh'
955
- }, {
956
- secret: this.authConfig.getRefreshTokenSecret(),
957
- expiresIn: this.authConfig.getRefreshTokenExpiration(),
958
- algorithm: 'HS256'
959
- });
960
- return {
961
- accessToken,
962
- refreshToken
963
- };
964
- }
965
893
  generateLoginResponse(user) {
966
- const { accessToken, refreshToken } = this.generateTokens({
894
+ const { accessToken, refreshToken } = (0, _helpers.generateTokens)(this.jwtService, this.authConfig, {
967
895
  id: user.id,
968
896
  email: user.email,
969
897
  type: 'access'
@@ -972,11 +900,11 @@ let AuthenticationService = class AuthenticationService {
972
900
  return {
973
901
  accessToken,
974
902
  refreshToken,
975
- user: this.toUserInfo(user)
903
+ user: (0, _helpers.toUserInfo)(user)
976
904
  };
977
905
  }
978
906
  generateLoginResponseWithCompany(user, company, branch) {
979
- const { accessToken, refreshToken } = this.generateTokens({
907
+ const { accessToken, refreshToken } = (0, _helpers.generateTokens)(this.jwtService, this.authConfig, {
980
908
  id: user.id,
981
909
  email: user.email,
982
910
  type: 'access',
@@ -987,9 +915,9 @@ let AuthenticationService = class AuthenticationService {
987
915
  return {
988
916
  accessToken,
989
917
  refreshToken,
990
- user: this.toUserInfo(user),
991
- company: this.toCompanyInfo(company),
992
- branch: branch ? this.toBranchInfo(branch) : undefined
918
+ user: (0, _helpers.toUserInfo)(user),
919
+ company: (0, _helpers.toCompanyInfo)(company),
920
+ branch: branch ? (0, _helpers.toBranchInfo)(branch) : undefined
993
921
  };
994
922
  }
995
923
  sendLoginNotification(userId, companyId) {
@@ -1014,10 +942,10 @@ let AuthenticationService = class AuthenticationService {
1014
942
  }
1015
943
  const sessionId = await this.sessionService.createSession(user.id);
1016
944
  const companiesWithBranches = companies.map((company)=>({
1017
- ...this.toCompanyInfo(company),
945
+ ...(0, _helpers.toCompanyInfo)(company),
1018
946
  isActive: company.isActive,
1019
947
  branches: branches.filter((b)=>b.companyId === company.id).map((b)=>({
1020
- ...this.toBranchInfo(b),
948
+ ...(0, _helpers.toBranchInfo)(b),
1021
949
  isActive: b.isActive
1022
950
  }))
1023
951
  }));
@@ -1025,7 +953,7 @@ let AuthenticationService = class AuthenticationService {
1025
953
  requiresSelection: true,
1026
954
  sessionId,
1027
955
  expiresAt: new Date(Date.now() + 5 * 60 * 1000),
1028
- user: this.toUserInfo(user),
956
+ user: (0, _helpers.toUserInfo)(user),
1029
957
  companies: companiesWithBranches
1030
958
  };
1031
959
  }