@solidxai/core 0.1.4 → 0.1.5-beta.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 (209) hide show
  1. package/.claude/settings.local.json +8 -0
  2. package/dist/constants/error-messages.d.ts +1 -0
  3. package/dist/constants/error-messages.d.ts.map +1 -1
  4. package/dist/constants/error-messages.js +1 -0
  5. package/dist/constants/error-messages.js.map +1 -1
  6. package/dist/constants.d.ts +3 -3
  7. package/dist/constants.d.ts.map +1 -1
  8. package/dist/constants.js +12 -12
  9. package/dist/constants.js.map +1 -1
  10. package/dist/controllers/otp-authentication.controller.d.ts +1 -4
  11. package/dist/controllers/otp-authentication.controller.d.ts.map +1 -1
  12. package/dist/controllers/otp-authentication.controller.js +1 -1
  13. package/dist/controllers/role-metadata.controller.d.ts +1 -0
  14. package/dist/controllers/role-metadata.controller.d.ts.map +1 -1
  15. package/dist/controllers/role-metadata.controller.js +15 -0
  16. package/dist/controllers/role-metadata.controller.js.map +1 -1
  17. package/dist/dtos/create-email-template.dto.d.ts.map +1 -1
  18. package/dist/dtos/create-email-template.dto.js.map +1 -1
  19. package/dist/dtos/create-list-of-values.dto.d.ts.map +1 -1
  20. package/dist/dtos/create-list-of-values.dto.js.map +1 -1
  21. package/dist/dtos/create-menu-item-metadata.dto.d.ts.map +1 -1
  22. package/dist/dtos/create-menu-item-metadata.dto.js.map +1 -1
  23. package/dist/dtos/create-role-metadata.dto.d.ts.map +1 -1
  24. package/dist/dtos/create-role-metadata.dto.js.map +1 -1
  25. package/dist/dtos/create-scheduled-job.dto.d.ts.map +1 -1
  26. package/dist/dtos/create-scheduled-job.dto.js.map +1 -1
  27. package/dist/dtos/create-security-rule.dto.d.ts.map +1 -1
  28. package/dist/dtos/create-security-rule.dto.js.map +1 -1
  29. package/dist/dtos/create-sms-template.dto.d.ts.map +1 -1
  30. package/dist/dtos/create-sms-template.dto.js.map +1 -1
  31. package/dist/dtos/create-view-metadata.dto.d.ts.map +1 -1
  32. package/dist/dtos/create-view-metadata.dto.js.map +1 -1
  33. package/dist/dtos/otp-sign-in.dto.d.ts +1 -1
  34. package/dist/dtos/otp-sign-in.dto.d.ts.map +1 -1
  35. package/dist/dtos/otp-sign-in.dto.js +2 -2
  36. package/dist/dtos/otp-sign-in.dto.js.map +1 -1
  37. package/dist/dtos/otp-sign-up.dto.d.ts +2 -2
  38. package/dist/dtos/otp-sign-up.dto.d.ts.map +1 -1
  39. package/dist/dtos/otp-sign-up.dto.js +2 -2
  40. package/dist/dtos/otp-sign-up.dto.js.map +1 -1
  41. package/dist/dtos/resolve-s3-url.dto.d.ts +2 -5
  42. package/dist/dtos/resolve-s3-url.dto.d.ts.map +1 -1
  43. package/dist/dtos/resolve-s3-url.dto.js +1 -13
  44. package/dist/dtos/resolve-s3-url.dto.js.map +1 -1
  45. package/dist/dtos/sign-up.dto.d.ts.map +1 -1
  46. package/dist/dtos/sign-up.dto.js.map +1 -1
  47. package/dist/dtos/update-email-template.dto.d.ts.map +1 -1
  48. package/dist/dtos/update-email-template.dto.js.map +1 -1
  49. package/dist/dtos/update-list-of-values.dto.d.ts.map +1 -1
  50. package/dist/dtos/update-list-of-values.dto.js.map +1 -1
  51. package/dist/dtos/update-menu-item-metadata.dto.d.ts.map +1 -1
  52. package/dist/dtos/update-menu-item-metadata.dto.js.map +1 -1
  53. package/dist/dtos/update-scheduled-job.dto.d.ts.map +1 -1
  54. package/dist/dtos/update-scheduled-job.dto.js.map +1 -1
  55. package/dist/dtos/update-security-rule.dto.d.ts.map +1 -1
  56. package/dist/dtos/update-security-rule.dto.js.map +1 -1
  57. package/dist/dtos/update-sms-template.dto.d.ts.map +1 -1
  58. package/dist/dtos/update-sms-template.dto.js.map +1 -1
  59. package/dist/dtos/update-view-metadata.dto.d.ts.map +1 -1
  60. package/dist/dtos/update-view-metadata.dto.js.map +1 -1
  61. package/dist/entities/user.entity.d.ts +1 -0
  62. package/dist/entities/user.entity.d.ts.map +1 -1
  63. package/dist/entities/user.entity.js +6 -1
  64. package/dist/entities/user.entity.js.map +1 -1
  65. package/dist/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.d.ts +2 -0
  66. package/dist/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.d.ts.map +1 -1
  67. package/dist/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.js +33 -23
  68. package/dist/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.js.map +1 -1
  69. package/dist/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.d.ts +3 -0
  70. package/dist/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.d.ts.map +1 -1
  71. package/dist/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.js +36 -23
  72. package/dist/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.js.map +1 -1
  73. package/dist/helpers/security.helper.js +1 -0
  74. package/dist/helpers/security.helper.js.map +1 -1
  75. package/dist/index.d.ts +0 -4
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +0 -4
  78. package/dist/index.js.map +1 -1
  79. package/dist/repository/solid-base.repository.d.ts +10 -1
  80. package/dist/repository/solid-base.repository.d.ts.map +1 -1
  81. package/dist/repository/solid-base.repository.js +109 -0
  82. package/dist/repository/solid-base.repository.js.map +1 -1
  83. package/dist/seeders/module-metadata-seeder.service.d.ts +2 -0
  84. package/dist/seeders/module-metadata-seeder.service.d.ts.map +1 -1
  85. package/dist/seeders/module-metadata-seeder.service.js +141 -71
  86. package/dist/seeders/module-metadata-seeder.service.js.map +1 -1
  87. package/dist/seeders/permission-metadata-seeder.service.d.ts +1 -1
  88. package/dist/seeders/permission-metadata-seeder.service.d.ts.map +1 -1
  89. package/dist/seeders/permission-metadata-seeder.service.js +1 -1
  90. package/dist/seeders/permission-metadata-seeder.service.js.map +1 -1
  91. package/dist/seeders/seed-data/solid-core-metadata.json +12 -25
  92. package/dist/services/authentication.service.d.ts +22 -8
  93. package/dist/services/authentication.service.d.ts.map +1 -1
  94. package/dist/services/authentication.service.js +228 -214
  95. package/dist/services/authentication.service.js.map +1 -1
  96. package/dist/services/crud-helper.service.d.ts +4 -0
  97. package/dist/services/crud-helper.service.d.ts.map +1 -1
  98. package/dist/services/crud-helper.service.js +66 -32
  99. package/dist/services/crud-helper.service.js.map +1 -1
  100. package/dist/services/crud.service.d.ts.map +1 -1
  101. package/dist/services/crud.service.js +7 -4
  102. package/dist/services/crud.service.js.map +1 -1
  103. package/dist/services/field-metadata.service.d.ts.map +1 -1
  104. package/dist/services/field-metadata.service.js.map +1 -1
  105. package/dist/services/file/disk-file.service.d.ts +0 -2
  106. package/dist/services/file/disk-file.service.d.ts.map +1 -1
  107. package/dist/services/file/disk-file.service.js +7 -16
  108. package/dist/services/file/disk-file.service.js.map +1 -1
  109. package/dist/services/file/index.d.ts +1 -0
  110. package/dist/services/file/index.d.ts.map +1 -1
  111. package/dist/services/file/index.js +1 -0
  112. package/dist/services/file/index.js.map +1 -1
  113. package/dist/services/file/storage-path-builder.d.ts +17 -0
  114. package/dist/services/file/storage-path-builder.d.ts.map +1 -0
  115. package/dist/{seeders/sms-template-seeder.service.js → services/file/storage-path-builder.js} +45 -35
  116. package/dist/services/file/storage-path-builder.js.map +1 -0
  117. package/dist/services/media.service.d.ts +1 -1
  118. package/dist/services/media.service.d.ts.map +1 -1
  119. package/dist/services/media.service.js +45 -6
  120. package/dist/services/media.service.js.map +1 -1
  121. package/dist/services/mediaStorageProviders/file-s3-storage-provider.js.map +1 -1
  122. package/dist/services/mediaStorageProviders/file-storage-provider.d.ts.map +1 -1
  123. package/dist/services/mediaStorageProviders/file-storage-provider.js +46 -7
  124. package/dist/services/mediaStorageProviders/file-storage-provider.js.map +1 -1
  125. package/dist/services/module-metadata.service.d.ts +4 -6
  126. package/dist/services/module-metadata.service.d.ts.map +1 -1
  127. package/dist/services/module-metadata.service.js +16 -14
  128. package/dist/services/module-metadata.service.js.map +1 -1
  129. package/dist/services/setting.service.d.ts +3 -2
  130. package/dist/services/setting.service.d.ts.map +1 -1
  131. package/dist/services/setting.service.js +7 -4
  132. package/dist/services/setting.service.js.map +1 -1
  133. package/dist/services/settings/default-settings-provider.service.d.ts +24 -2
  134. package/dist/services/settings/default-settings-provider.service.d.ts.map +1 -1
  135. package/dist/services/settings/default-settings-provider.service.js +8 -6
  136. package/dist/services/settings/default-settings-provider.service.js.map +1 -1
  137. package/dist/solid-core.module.d.ts +3 -1
  138. package/dist/solid-core.module.d.ts.map +1 -1
  139. package/dist/solid-core.module.js +45 -9
  140. package/dist/solid-core.module.js.map +1 -1
  141. package/dist/testing/adapters/ui/playwright-adapter.d.ts.map +1 -1
  142. package/dist/testing/adapters/ui/playwright-adapter.js +35 -2
  143. package/dist/testing/adapters/ui/playwright-adapter.js.map +1 -1
  144. package/package.json +8 -2
  145. package/src/constants/error-messages.ts +1 -0
  146. package/src/constants.ts +3 -3
  147. package/src/controllers/role-metadata.controller.ts +26 -18
  148. package/src/dtos/create-email-template.dto.ts +7 -0
  149. package/src/dtos/create-list-of-values.dto.ts +7 -0
  150. package/src/dtos/create-menu-item-metadata.dto.ts +12 -1
  151. package/src/dtos/create-role-metadata.dto.ts +9 -0
  152. package/src/dtos/create-scheduled-job.dto.ts +14 -0
  153. package/src/dtos/create-security-rule.dto.ts +6 -0
  154. package/src/dtos/create-sms-template.dto.ts +6 -0
  155. package/src/dtos/create-view-metadata.dto.ts +11 -0
  156. package/src/dtos/otp-sign-in.dto.ts +3 -3
  157. package/src/dtos/otp-sign-up.dto.ts +3 -3
  158. package/src/dtos/resolve-s3-url.dto.ts +2 -12
  159. package/src/dtos/sign-up.dto.ts +0 -2
  160. package/src/dtos/update-email-template.dto.ts +6 -0
  161. package/src/dtos/update-list-of-values.dto.ts +8 -0
  162. package/src/dtos/update-menu-item-metadata.dto.ts +12 -0
  163. package/src/dtos/update-scheduled-job.dto.ts +15 -0
  164. package/src/dtos/update-security-rule.dto.ts +7 -0
  165. package/src/dtos/update-sms-template.dto.ts +32 -32
  166. package/src/dtos/update-view-metadata.dto.ts +12 -0
  167. package/src/entities/user.entity.ts +3 -0
  168. package/src/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.ts +43 -32
  169. package/src/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.ts +45 -33
  170. package/src/helpers/security.helper.ts +1 -1
  171. package/src/index.ts +0 -4
  172. package/src/repository/solid-base.repository.ts +172 -1
  173. package/src/seeders/module-metadata-seeder.service.ts +188 -126
  174. package/src/seeders/permission-metadata-seeder.service.ts +1 -4
  175. package/src/seeders/seed-data/solid-core-metadata.json +12 -25
  176. package/src/services/authentication.service.ts +268 -266
  177. package/src/services/crud-helper.service.ts +79 -36
  178. package/src/services/crud.service.ts +9 -4
  179. package/src/services/field-metadata.service.ts +0 -71
  180. package/src/services/file/disk-file.service.ts +8 -18
  181. package/src/services/file/index.ts +1 -0
  182. package/src/services/file/storage-path-builder.ts +56 -0
  183. package/src/services/media.service.ts +13 -7
  184. package/src/services/mediaStorageProviders/file-s3-storage-provider.ts +1 -1
  185. package/src/services/mediaStorageProviders/file-storage-provider.ts +13 -8
  186. package/src/services/module-metadata.service.ts +18 -15
  187. package/src/services/setting.service.ts +5 -3
  188. package/src/services/settings/default-settings-provider.service.ts +5 -3
  189. package/src/solid-core.module.ts +16 -12
  190. package/src/testing/adapters/ui/playwright-adapter.ts +1 -1
  191. package/dist/passport-strategies/local.strategy.d.ts +0 -15
  192. package/dist/passport-strategies/local.strategy.d.ts.map +0 -1
  193. package/dist/passport-strategies/local.strategy.js +0 -44
  194. package/dist/passport-strategies/local.strategy.js.map +0 -1
  195. package/dist/seeders/email-template-seeder.service.d.ts +0 -10
  196. package/dist/seeders/email-template-seeder.service.d.ts.map +0 -1
  197. package/dist/seeders/email-template-seeder.service.js +0 -84
  198. package/dist/seeders/email-template-seeder.service.js.map +0 -1
  199. package/dist/seeders/sms-template-seeder.service.d.ts +0 -10
  200. package/dist/seeders/sms-template-seeder.service.d.ts.map +0 -1
  201. package/dist/seeders/sms-template-seeder.service.js.map +0 -1
  202. package/dist/seeders/user-seeder.service.d.ts +0 -10
  203. package/dist/seeders/user-seeder.service.d.ts.map +0 -1
  204. package/dist/seeders/user-seeder.service.js +0 -44
  205. package/dist/seeders/user-seeder.service.js.map +0 -1
  206. package/src/passport-strategies/local.strategy.ts +0 -28
  207. package/src/seeders/email-template-seeder.service.ts +0 -49
  208. package/src/seeders/sms-template-seeder.service.ts +0 -50
  209. package/src/seeders/user-seeder.service.ts +0 -33
@@ -93,23 +93,21 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
93
93
  relations: { roles: true }
94
94
  });
95
95
  }
96
- async validateUserAndRehashPasswordIfRequired(signInDto) {
97
- const user = await this.resolveUser(signInDto.username, signInDto.email);
98
- if (!user) {
99
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_CREDENTIALS);
100
- }
96
+ async validateUserForPasswordLogin(user, password) {
101
97
  if (!user.active) {
102
98
  throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_ACTIVE);
103
99
  }
104
- const isEqual = await this.hashingService.compare(signInDto.password, user.password, user.passwordSchemeVersion);
100
+ this.checkAccountBlocked(user);
101
+ const isEqual = await this.hashingService.compare(password, user.password, user.passwordSchemeVersion);
105
102
  if (!isEqual) {
103
+ await this.incrementFailedAttempts(user);
106
104
  throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_CREDENTIALS);
107
105
  }
106
+ }
107
+ async rehashPasswordIfRequired(user, password) {
108
108
  if (this.hashingService.needsRehash(user.password, user.passwordSchemeVersion)) {
109
- const rehashedUser = await this.updatePasswordDetails(user, signInDto.password);
110
- return rehashedUser;
109
+ await this.updatePasswordDetails(user, password);
111
110
  }
112
- return user;
113
111
  }
114
112
  async signUp(signUpDto, activeUser = null) {
115
113
  try {
@@ -258,56 +256,61 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
258
256
  }
259
257
  }
260
258
  async otpInitiateRegistration(signUpDto) {
259
+ const isPasswordlessRegistrationEnabled = await this.isPasswordlessRegistrationEnabled();
260
+ if (!isPasswordlessRegistrationEnabled) {
261
+ throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.PASSWORDLESS_REGISTRATION_DISABLED);
262
+ }
263
+ const validationSource = this.resolvePasswordlessValidationSource();
264
+ this.validateOtpRegistrationInput(signUpDto, validationSource);
265
+ const existingUser = await this.findExistingRegistrationUser(signUpDto);
266
+ if ((0, class_validator_1.isNotEmpty)(existingUser) && existingUser.active) {
267
+ throw new common_1.ConflictException(error_messages_1.ERROR_MESSAGES.USER_ALREADY_EXISTS);
268
+ }
261
269
  try {
262
- const isPasswordlessRegistrationEnabled = await this.isPasswordlessRegistrationEnabled();
263
- if (!isPasswordlessRegistrationEnabled) {
264
- throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.PASSWORDLESS_REGISTRATION_DISABLED);
265
- }
266
- if ((0, class_validator_1.isEmpty)(signUpDto.mobile) && (0, class_validator_1.isEmpty)(signUpDto.email)) {
267
- throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.REGISTRATION_REQUIRES_CONTACT);
268
- }
269
- if (signUpDto.validationSources.includes(constants_1.TransactionalRegistrationValidationSource.EMAIL) && (0, class_validator_1.isEmpty)(signUpDto.email)) {
270
- throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.EMAIL_REQUIRED_FOR_VALIDATION);
271
- }
272
- if (signUpDto.validationSources.includes(constants_1.TransactionalRegistrationValidationSource.MOBILE) && (0, class_validator_1.isEmpty)(signUpDto.mobile)) {
273
- throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.MOBILE_REQUIRED_FOR_VALIDATION);
274
- }
275
- const existingUser = await this.userRepository.findOne({
276
- where: [
277
- { email: signUpDto.email, },
278
- { mobile: signUpDto.mobile, },
279
- { username: signUpDto.username, }
280
- ]
281
- });
282
- if ((0, class_validator_1.isNotEmpty)(existingUser) && existingUser.active) {
283
- throw new common_1.ConflictException(error_messages_1.ERROR_MESSAGES.USER_ALREADY_EXISTS);
284
- }
285
- let passwordlessRegistrationValidateWhat = this.settingService.getConfigValue('passwordlessRegistrationValidateWhat');
286
- if (!Array.isArray(passwordlessRegistrationValidateWhat)) {
287
- passwordlessRegistrationValidateWhat = [passwordlessRegistrationValidateWhat];
288
- }
289
- const finalRegistrationVerificationSources = this.calculateVerificationSources(passwordlessRegistrationValidateWhat, signUpDto);
290
- let user = existingUser;
291
- if ((0, class_validator_1.isEmpty)(user)) {
292
- user = this.createUser(signUpDto);
293
- await this.populateVerificationTokens(finalRegistrationVerificationSources, user);
294
- await this.userRepository.save(user);
295
- await this.userService.addRoleToUser(user.username, this.settingService.getConfigValue('defaultRole'));
296
- }
297
- else {
298
- await this.populateVerificationTokens(finalRegistrationVerificationSources, user);
299
- await this.userRepository.save(user);
300
- }
301
- await this.notifyUserOnOtpInitiateRegistration(user, finalRegistrationVerificationSources);
302
- return { message: success_messages_1.SUCCESS_MESSAGES.OTP_SENT_SUCCESS_REGISTRATION };
270
+ const user = await this.upsertUserWithRegistrationVerificationTokens(existingUser, signUpDto, validationSource);
271
+ await this.notifyUserOnOtpInitiateRegistration(user, validationSource);
303
272
  }
304
273
  catch (err) {
305
- const pgUniqueViolationErrorCode = '23505';
306
- if (err.code === pgUniqueViolationErrorCode) {
274
+ if (err.code === '23505') {
307
275
  throw new common_1.ConflictException(error_messages_1.ERROR_MESSAGES.USER_ALREADY_EXISTS);
308
276
  }
309
277
  throw err;
310
278
  }
279
+ return { message: success_messages_1.SUCCESS_MESSAGES.OTP_SENT_SUCCESS_REGISTRATION };
280
+ }
281
+ validateOtpRegistrationInput(signUpDto, validationSource) {
282
+ if (validationSource === constants_1.PasswordlessRegistrationValidateWhatSources.EMAIL && (0, class_validator_1.isEmpty)(signUpDto.email)) {
283
+ throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.EMAIL_REQUIRED_FOR_VALIDATION);
284
+ }
285
+ if (validationSource === constants_1.PasswordlessRegistrationValidateWhatSources.MOBILE && (0, class_validator_1.isEmpty)(signUpDto.mobile)) {
286
+ throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.MOBILE_REQUIRED_FOR_VALIDATION);
287
+ }
288
+ }
289
+ async findExistingRegistrationUser(signUpDto) {
290
+ return this.userRepository.findOne({
291
+ where: [
292
+ { email: signUpDto.email },
293
+ { mobile: signUpDto.mobile },
294
+ { username: signUpDto.username },
295
+ ]
296
+ });
297
+ }
298
+ resolvePasswordlessValidationSource() {
299
+ return this.settingService.getConfigValue('passwordlessRegistrationValidateWhat');
300
+ }
301
+ async upsertUserWithRegistrationVerificationTokens(existingUser, signUpDto, validationSource) {
302
+ let user = existingUser;
303
+ if ((0, class_validator_1.isEmpty)(user)) {
304
+ user = this.createUser(signUpDto);
305
+ await this.assignRegistrationOtp(validationSource, user);
306
+ await this.userRepository.save(user);
307
+ await this.userService.addRoleToUser(user.username, this.settingService.getConfigValue('defaultRole'));
308
+ }
309
+ else {
310
+ await this.assignRegistrationOtp(validationSource, user);
311
+ await this.userRepository.save(user);
312
+ }
313
+ return user;
311
314
  }
312
315
  createUser(signUpDto) {
313
316
  const user = new user_entity_1.User();
@@ -318,44 +321,36 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
318
321
  user.lastLoginProvider = LoginProvider.OTP;
319
322
  return user;
320
323
  }
321
- calculateVerificationSources(configuredRegistrationValidationSources, signUpDto) {
322
- const finalRegistrationValidationSources = configuredRegistrationValidationSources.filter((source) => source !== constants_1.RegistrationValidationSource.TRANSACTIONAL);
323
- if (configuredRegistrationValidationSources.includes(constants_1.RegistrationValidationSource.TRANSACTIONAL)) {
324
- finalRegistrationValidationSources.push(...signUpDto.validationSources);
325
- }
326
- return finalRegistrationValidationSources;
327
- }
328
- async populateVerificationTokens(finalRegistrationValidationSources, user) {
329
- if (finalRegistrationValidationSources.length === 0) {
324
+ async assignRegistrationOtp(passwordlessRegistrationValidateWhat, user) {
325
+ if (!passwordlessRegistrationValidateWhat) {
330
326
  throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.VALIDATION_SOURCE_REQUIRED);
331
327
  }
332
- if (finalRegistrationValidationSources.includes(constants_1.TransactionalRegistrationValidationSource.EMAIL)) {
328
+ const autoLoginUserOnRegistration = this.settingService.getConfigValue('autoLoginUserOnRegistration');
329
+ if (passwordlessRegistrationValidateWhat === constants_1.PasswordlessRegistrationValidateWhatSources.EMAIL) {
333
330
  const { token, expiresAt } = await this.otp();
334
331
  user.emailVerificationTokenOnRegistration = token;
335
332
  user.emailVerificationTokenOnRegistrationExpiresAt = expiresAt;
336
- const autoLoginUserOnRegistration = this.settingService.getConfigValue('autoLoginUserOnRegistration');
337
333
  if (autoLoginUserOnRegistration) {
338
334
  user.emailVerificationTokenOnLogin = token;
339
335
  user.emailVerificationTokenOnLoginExpiresAt = expiresAt;
340
336
  }
341
337
  }
342
- if (finalRegistrationValidationSources.includes(constants_1.TransactionalRegistrationValidationSource.MOBILE)) {
338
+ if (passwordlessRegistrationValidateWhat === constants_1.PasswordlessRegistrationValidateWhatSources.MOBILE) {
343
339
  const { token, expiresAt } = await this.otp();
344
340
  user.mobileVerificationTokenOnRegistration = token;
345
341
  user.mobileVerificationTokenOnRegistrationExpiresAt = expiresAt;
346
- const autoLoginUserOnRegistration = this.settingService.getConfigValue('autoLoginUserOnRegistration');
347
342
  if (autoLoginUserOnRegistration) {
348
343
  user.mobileVerificationTokenOnLogin = token;
349
344
  user.mobileVerificationTokenOnLoginExpiresAt = expiresAt;
350
345
  }
351
346
  }
352
347
  }
353
- async notifyUserOnOtpInitiateRegistration(user, registrationValidationSources) {
348
+ async notifyUserOnOtpInitiateRegistration(user, registrationValidationSource) {
354
349
  const companyLogo = await this.getCompanyLogo();
355
350
  const dummyOtp = this.settingService.getConfigValue('dummyOtp');
356
351
  if (dummyOtp)
357
352
  return;
358
- if (registrationValidationSources.includes(constants_1.RegistrationValidationSource.EMAIL)) {
353
+ if (registrationValidationSource === constants_1.PasswordlessLoginValidateWhatSources.EMAIL) {
359
354
  const mailService = this.mailServiceFactory.getMailService();
360
355
  mailService.sendEmailUsingTemplate(user.email, 'otp-on-register', {
361
356
  solidAppName: this.settingService.getConfigValue('appTitle'),
@@ -366,7 +361,7 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
366
361
  companyLogoUrl: companyLogo
367
362
  }, this.settingService.getConfigValue('shouldQueueEmails'), null, null, 'user', user.id);
368
363
  }
369
- if (registrationValidationSources.includes(constants_1.RegistrationValidationSource.MOBILE)) {
364
+ if (registrationValidationSource === constants_1.PasswordlessLoginValidateWhatSources.MOBILE) {
370
365
  const smsService = this.smsFactory.getSmsService();
371
366
  smsService.sendSMSUsingTemplate(user.mobile, 'otp-on-register', {
372
367
  solidAppName: this.settingService.getConfigValue('appTitle'),
@@ -383,67 +378,65 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
383
378
  if (!isPasswordlessRegistrationEnabled) {
384
379
  throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.PASSWORDLESS_REGISTRATION_DISABLED);
385
380
  }
386
- if (confirmSignUpDto.type === constants_1.RegistrationValidationSource.EMAIL) {
387
- const user = await this.userRepository.findOne({
388
- where: {
389
- email: confirmSignUpDto.identifier,
390
- }
391
- });
392
- if (!user) {
393
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
394
- }
395
- if (user.emailVerificationTokenOnRegistration !== confirmSignUpDto.otp) {
396
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
397
- }
398
- if (user.emailVerificationTokenOnRegistrationExpiresAt < new Date()) {
399
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.OTP_EXPIRED);
400
- }
381
+ const { type, identifier, otp } = confirmSignUpDto;
382
+ if (type !== constants_1.PasswordlessRegistrationValidateWhatSources.EMAIL &&
383
+ type !== constants_1.PasswordlessRegistrationValidateWhatSources.MOBILE) {
384
+ throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.INVALID_VERIFICATION_TYPE);
385
+ }
386
+ const user = await this.findUserByRegistrationIdentifier(type, identifier);
387
+ this.validateRegistrationOtp(user, otp, type);
388
+ this.clearRegistrationOtp(user, type);
389
+ user.active = this.settingService.getConfigValue('activateUserOnRegistration') &&
390
+ await this.areAllPasswordlessRegistrationValidationSourcesVerified(user);
391
+ const savedUser = await this.userRepository.save(user);
392
+ this.triggerRegistrationEvent(savedUser);
393
+ return { active: savedUser.active, message: `User registration verified for ${type}` };
394
+ }
395
+ async findUserByRegistrationIdentifier(type, identifier) {
396
+ const where = type === constants_1.PasswordlessRegistrationValidateWhatSources.EMAIL
397
+ ? { email: identifier }
398
+ : { mobile: identifier };
399
+ const user = await this.userRepository.findOne({ where });
400
+ if (!user) {
401
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
402
+ }
403
+ return user;
404
+ }
405
+ validateRegistrationOtp(user, otp, type) {
406
+ const isEmail = type === constants_1.PasswordlessRegistrationValidateWhatSources.EMAIL;
407
+ const token = isEmail ? user.emailVerificationTokenOnRegistration : user.mobileVerificationTokenOnRegistration;
408
+ const expiresAt = isEmail ? user.emailVerificationTokenOnRegistrationExpiresAt : user.mobileVerificationTokenOnRegistrationExpiresAt;
409
+ if (token !== otp) {
410
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
411
+ }
412
+ if (expiresAt < new Date()) {
413
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.OTP_EXPIRED);
414
+ }
415
+ }
416
+ clearRegistrationOtp(user, type) {
417
+ if (type === constants_1.PasswordlessRegistrationValidateWhatSources.EMAIL) {
401
418
  user.emailVerifiedOnRegistrationAt = new Date();
402
419
  user.emailVerificationTokenOnRegistration = null;
403
420
  user.emailVerificationTokenOnRegistrationExpiresAt = null;
404
- user.active = this.settingService.getConfigValue('activateUserOnRegistration') && await this.areRegistrationValidationSourcesVerified(user);
405
- const savedUser = await this.userRepository.save(user);
406
- this.triggerRegistrationEvent(savedUser);
407
- return { active: savedUser.active, message: `User registration verified for ${confirmSignUpDto.type}` };
408
421
  }
409
- else if (confirmSignUpDto.type === constants_1.RegistrationValidationSource.MOBILE) {
410
- const user = await this.userRepository.findOne({
411
- where: {
412
- mobile: confirmSignUpDto.identifier,
413
- }
414
- });
415
- if (!user) {
416
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
417
- }
418
- if (user.mobileVerificationTokenOnRegistration !== confirmSignUpDto.otp) {
419
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
420
- }
421
- if (user.mobileVerificationTokenOnRegistrationExpiresAt < new Date()) {
422
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
423
- }
422
+ else {
424
423
  user.mobileVerifiedOnRegistrationAt = new Date();
425
424
  user.mobileVerificationTokenOnRegistration = null;
426
425
  user.mobileVerificationTokenOnRegistrationExpiresAt = null;
427
- user.active = this.settingService.getConfigValue('activateUserOnRegistration') && await this.areRegistrationValidationSourcesVerified(user);
428
- const savedUser = await this.userRepository.save(user);
429
- this.triggerRegistrationEvent(savedUser);
430
- return { active: savedUser.active, message: `User registration verified for ${confirmSignUpDto.type}` };
431
426
  }
432
- throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.INVALID_VERIFICATION_TYPE);
433
427
  }
434
428
  triggerRegistrationEvent(savedUser) {
435
429
  const event = new interfaces_1.EventDetails(interfaces_1.EventType.USER_REGISTERED, savedUser);
436
430
  this.eventEmitter.emit(interfaces_1.EventType.USER_REGISTERED, event);
437
431
  }
438
- async areRegistrationValidationSourcesVerified(user) {
439
- const passwordlessRegistrationValidateWhat = this.settingService.getConfigValue('passwordlessRegistrationValidateWhat');
440
- const registrationValidationSources = passwordlessRegistrationValidateWhat;
441
- if (registrationValidationSources.includes(constants_1.RegistrationValidationSource.EMAIL)) {
432
+ async areAllPasswordlessRegistrationValidationSourcesVerified(user) {
433
+ const registrationValidationSource = this.resolvePasswordlessValidationSource();
434
+ if (registrationValidationSource === constants_1.PasswordlessLoginValidateWhatSources.EMAIL) {
442
435
  if (!user.emailVerifiedOnRegistrationAt) {
443
436
  return false;
444
437
  }
445
438
  }
446
- if (registrationValidationSources.includes(constants_1.RegistrationValidationSource.MOBILE)) {
439
+ if (registrationValidationSource === constants_1.PasswordlessLoginValidateWhatSources.MOBILE) {
447
440
  if (!user.mobileVerifiedOnRegistrationAt) {
448
441
  return false;
449
442
  }
@@ -461,7 +454,13 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
461
454
  };
462
455
  }
463
456
  async signIn(signInDto) {
464
- const user = await this.validateUserAndRehashPasswordIfRequired(signInDto);
457
+ const user = await this.resolveUser(signInDto.username, signInDto.email);
458
+ if (!user) {
459
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_CREDENTIALS);
460
+ }
461
+ await this.validateUserForPasswordLogin(user, signInDto.password);
462
+ await this.rehashPasswordIfRequired(user, signInDto.password);
463
+ await this.resetFailedAttempts(user);
465
464
  const tokens = await this.generateTokens(user);
466
465
  await this.userActivityHistoryService.logEvent('login', user);
467
466
  return {
@@ -501,63 +500,67 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
501
500
  if (!isPasswordlessRegistrationEnabled) {
502
501
  throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.PASSWORDLESS_REGISTRATION_DISABLED);
503
502
  }
504
- if (signInDto.type === constants_1.RegistrationValidationSource.EMAIL) {
505
- const user = await this.userRepository.findOne({
506
- where: [
507
- { username: signInDto.identifier },
508
- { email: signInDto.identifier },
509
- ]
510
- });
511
- if (!user) {
512
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
513
- }
514
- if (!user.active) {
515
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_INACTIVE);
503
+ const type = this.resolveLoginType(signInDto);
504
+ const user = await this.findUserForLogin(type, signInDto.identifier);
505
+ await this.assignLoginOtp(user, type);
506
+ this.notifyUserOnOtpInititateLogin(user, type);
507
+ return this.buildLoginOtpResponse(user, type);
508
+ }
509
+ resolveLoginType(signInDto) {
510
+ const setting = this.settingService.getConfigValue('passwordlessLoginValidateWhat');
511
+ if (setting === constants_1.PasswordlessLoginValidateWhatSources.SELECTABLE) {
512
+ if (signInDto.type !== constants_1.PasswordlessLoginValidateWhatSources.EMAIL &&
513
+ signInDto.type !== constants_1.PasswordlessLoginValidateWhatSources.MOBILE) {
514
+ throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.INVALID_VERIFICATION_TYPE);
516
515
  }
517
- const { token, expiresAt } = await this.otp();
516
+ return signInDto.type;
517
+ }
518
+ if (setting === constants_1.PasswordlessLoginValidateWhatSources.EMAIL ||
519
+ setting === constants_1.PasswordlessLoginValidateWhatSources.MOBILE) {
520
+ return setting;
521
+ }
522
+ throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.INVALID_VERIFICATION_TYPE);
523
+ }
524
+ async findUserForLogin(type, identifier, options = {}) {
525
+ const typeWhere = type === constants_1.PasswordlessLoginValidateWhatSources.EMAIL
526
+ ? { email: identifier }
527
+ : { mobile: identifier };
528
+ const user = await this.userRepository.findOne({
529
+ where: [{ username: identifier }, typeWhere],
530
+ ...(options.withRoles ? { relations: { roles: true } } : {}),
531
+ });
532
+ if (!user) {
533
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
534
+ }
535
+ if (!user.active) {
536
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_INACTIVE);
537
+ }
538
+ return user;
539
+ }
540
+ async assignLoginOtp(user, type) {
541
+ const { token, expiresAt } = await this.otp();
542
+ if (type === constants_1.PasswordlessLoginValidateWhatSources.EMAIL) {
518
543
  user.emailVerificationTokenOnLogin = token;
519
544
  user.emailVerificationTokenOnLoginExpiresAt = expiresAt;
520
- await this.userRepository.save(user);
521
- this.notifyUserOnOtpInititateLogin(user, constants_1.RegistrationValidationSource.EMAIL);
522
- return {
523
- message: success_messages_1.SUCCESS_MESSAGES.OTP_SENT_SUCCESS_LOGIN,
524
- user: {
525
- email: this.maskEmail(user.email)
526
- }
527
- };
528
545
  }
529
- else if (signInDto.type === constants_1.RegistrationValidationSource.MOBILE) {
530
- const user = await this.userRepository.findOne({
531
- where: [
532
- { username: signInDto.identifier },
533
- { mobile: signInDto.identifier },
534
- ]
535
- });
536
- if (!user) {
537
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
538
- }
539
- const { token, expiresAt } = await this.otp();
546
+ else {
540
547
  user.mobileVerificationTokenOnLogin = token;
541
548
  user.mobileVerificationTokenOnLoginExpiresAt = expiresAt;
542
- await this.userRepository.save(user);
543
- this.notifyUserOnOtpInititateLogin(user, constants_1.RegistrationValidationSource.MOBILE);
544
- return {
545
- message: success_messages_1.SUCCESS_MESSAGES.OTP_SENT_SUCCESS_LOGIN,
546
- user: {
547
- mobile: this.maskMobile(user.mobile)
548
- }
549
- };
550
- }
551
- else {
552
- throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.INVALID_VERIFICATION_TYPE);
553
549
  }
550
+ await this.userRepository.save(user);
551
+ }
552
+ buildLoginOtpResponse(user, type) {
553
+ const maskedIdentifier = type === constants_1.PasswordlessLoginValidateWhatSources.EMAIL
554
+ ? { email: this.maskEmail(user.email) }
555
+ : { mobile: this.maskMobile(user.mobile) };
556
+ return { message: success_messages_1.SUCCESS_MESSAGES.OTP_SENT_SUCCESS_LOGIN, user: maskedIdentifier };
554
557
  }
555
558
  async notifyUserOnOtpInititateLogin(user, loginType) {
556
559
  const companyLogo = await this.getCompanyLogo();
557
560
  const dummyOtp = this.settingService.getConfigValue('dummyOtp');
558
561
  if (dummyOtp)
559
562
  return;
560
- if (loginType === constants_1.RegistrationValidationSource.EMAIL) {
563
+ if (loginType === constants_1.PasswordlessLoginValidateWhatSources.EMAIL) {
561
564
  const mailService = this.mailServiceFactory.getMailService();
562
565
  mailService.sendEmailUsingTemplate(user.email, 'otp-on-login', {
563
566
  solidAppName: this.settingService.getConfigValue('appTitle'),
@@ -568,7 +571,7 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
568
571
  companyLogoUrl: companyLogo
569
572
  }, this.settingService.getConfigValue('shouldQueueEmails'), null, null, 'user', user.id);
570
573
  }
571
- if (loginType === constants_1.RegistrationValidationSource.MOBILE) {
574
+ if (loginType === constants_1.PasswordlessLoginValidateWhatSources.MOBILE) {
572
575
  const smsService = this.smsFactory.getSmsService();
573
576
  smsService.sendSMSUsingTemplate(user.mobile, 'otp-on-login', {
574
577
  solidAppName: this.settingService.getConfigValue('appTitle'),
@@ -585,69 +588,53 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
585
588
  if (!isPasswordlessRegistrationEnabled) {
586
589
  throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.PASSWORDLESS_REGISTRATION_DISABLED);
587
590
  }
588
- if (confirmSignInDto.type === constants_1.RegistrationValidationSource.EMAIL) {
589
- const user = await this.userRepository.findOne({
590
- where: [
591
- { username: confirmSignInDto.identifier },
592
- { email: confirmSignInDto.identifier },
593
- ],
594
- relations: {
595
- roles: true
596
- }
597
- });
598
- if (!user) {
599
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
600
- }
601
- if (!user.active) {
602
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_INACTIVE);
603
- }
604
- if (user.emailVerificationTokenOnLogin !== confirmSignInDto.otp) {
605
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
606
- }
607
- if (user.emailVerificationTokenOnLoginExpiresAt < new Date()) {
608
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
609
- }
591
+ const { type, identifier, otp } = confirmSignInDto;
592
+ if (type !== constants_1.PasswordlessLoginValidateWhatSources.EMAIL &&
593
+ type !== constants_1.PasswordlessLoginValidateWhatSources.MOBILE) {
594
+ throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.INVALID_VERIFICATION_TYPE);
595
+ }
596
+ const user = await this.findUserForLogin(type, identifier, { withRoles: true });
597
+ this.checkAccountBlocked(user);
598
+ try {
599
+ this.validateLoginOtp(user, otp, type);
600
+ }
601
+ catch (e) {
602
+ await this.incrementFailedAttempts(user);
603
+ throw e;
604
+ }
605
+ this.clearLoginOtp(user, type);
606
+ user.failedLoginAttempts = 0;
607
+ await this.userRepository.save(user);
608
+ return this.buildLoginTokenResponse(user);
609
+ }
610
+ validateLoginOtp(user, otp, type) {
611
+ const isEmail = type === constants_1.PasswordlessLoginValidateWhatSources.EMAIL;
612
+ const token = isEmail ? user.emailVerificationTokenOnLogin : user.mobileVerificationTokenOnLogin;
613
+ const expiresAt = isEmail ? user.emailVerificationTokenOnLoginExpiresAt : user.mobileVerificationTokenOnLoginExpiresAt;
614
+ if (token !== otp) {
615
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
616
+ }
617
+ if (expiresAt < new Date()) {
618
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.OTP_EXPIRED);
619
+ }
620
+ }
621
+ clearLoginOtp(user, type) {
622
+ if (type === constants_1.PasswordlessLoginValidateWhatSources.EMAIL) {
610
623
  user.emailVerifiedOnLoginAt = new Date();
611
624
  user.emailVerificationTokenOnLogin = null;
612
625
  user.emailVerificationTokenOnLoginExpiresAt = null;
613
- await this.userRepository.save(user);
614
- const { accessToken, refreshToken } = await this.generateTokens(user);
615
- const { id, username, email, mobile, lastLoginProvider } = user;
616
- const roles = user.roles.map((role) => role.name);
617
- return { accessToken, refreshToken, user: { id, username, email, mobile, lastLoginProvider, roles } };
618
626
  }
619
- else if (confirmSignInDto.type === constants_1.RegistrationValidationSource.MOBILE) {
620
- const user = await this.userRepository.findOne({
621
- where: [
622
- { username: confirmSignInDto.identifier },
623
- { mobile: confirmSignInDto.identifier },
624
- ],
625
- relations: {
626
- roles: true
627
- }
628
- });
629
- if (!user) {
630
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_ACTIVE);
631
- }
632
- if (!user.active) {
633
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_INACTIVE);
634
- }
635
- if (user.mobileVerificationTokenOnLogin !== confirmSignInDto.otp) {
636
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
637
- }
638
- if (user.mobileVerificationTokenOnLoginExpiresAt < new Date()) {
639
- throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.INVALID_OTP);
640
- }
627
+ else {
641
628
  user.mobileVerifiedOnLoginAt = new Date();
642
629
  user.mobileVerificationTokenOnLogin = null;
643
630
  user.mobileVerificationTokenOnLoginExpiresAt = null;
644
- await this.userRepository.save(user);
645
- const { accessToken, refreshToken } = await this.generateTokens(user);
646
- const { id, username, email, mobile, lastLoginProvider } = user;
647
- const roles = user.roles.map((role) => role.name);
648
- return { accessToken, refreshToken, user: { id, username, email, mobile, lastLoginProvider, roles } };
649
631
  }
650
- throw new common_1.BadRequestException(error_messages_1.ERROR_MESSAGES.INVALID_VERIFICATION_TYPE);
632
+ }
633
+ async buildLoginTokenResponse(user) {
634
+ const { accessToken, refreshToken } = await this.generateTokens(user);
635
+ const { id, username, email, mobile, lastLoginProvider } = user;
636
+ const roles = user.roles.map((role) => role.name);
637
+ return { accessToken, refreshToken, user: { id, username, email, mobile, lastLoginProvider, roles } };
651
638
  }
652
639
  async changePassword(changePasswordDto, activeUser) {
653
640
  const user = await this.userRepository.findOne({
@@ -911,7 +898,18 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
911
898
  roles: true
912
899
  }
913
900
  });
914
- await this.validateUserUsingGoogle(user);
901
+ if (!user) {
902
+ throw new common_1.UnauthorizedException(error_messages_1.ERROR_MESSAGES.USER_NOT_FOUND);
903
+ }
904
+ this.checkAccountBlocked(user);
905
+ try {
906
+ await this.validateUserUsingGoogle(user);
907
+ }
908
+ catch (e) {
909
+ await this.incrementFailedAttempts(user);
910
+ throw e;
911
+ }
912
+ await this.resetFailedAttempts(user);
915
913
  const tokens = await this.generateTokens(user);
916
914
  return {
917
915
  user: {
@@ -927,6 +925,22 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
927
925
  async isPasswordlessRegistrationEnabled() {
928
926
  return this.settingService.getConfigValue('passwordLessAuth');
929
927
  }
928
+ checkAccountBlocked(user) {
929
+ const maxFailedAttempts = this.settingService.getConfigValue('maxFailedLoginAttempts');
930
+ if (maxFailedAttempts > 0 && user.failedLoginAttempts >= maxFailedAttempts) {
931
+ throw new common_1.ForbiddenException(error_messages_1.ERROR_MESSAGES.ACCOUNT_BLOCKED);
932
+ }
933
+ }
934
+ async incrementFailedAttempts(user) {
935
+ user.failedLoginAttempts += 1;
936
+ await this.userRepository.save(user);
937
+ }
938
+ async resetFailedAttempts(user) {
939
+ if (user.failedLoginAttempts === 0)
940
+ return;
941
+ user.failedLoginAttempts = 0;
942
+ await this.userRepository.save(user);
943
+ }
930
944
  async logout(refreshToken) {
931
945
  try {
932
946
  const payload = this.jwtService.decode(refreshToken);