From 60ed67d004acb78041efa9e2a2d20cd83611e803 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 30 Oct 2019 09:06:02 +0300 Subject: [PATCH] feat(account): implement dynamic password validation to register page #2039 --- .../components/register/register.component.ts | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts b/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts index 01b0377def..e01287e1cb 100644 --- a/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts +++ b/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts @@ -1,6 +1,6 @@ -import { ConfigState, GetAppConfiguration } from '@abp/ng.core'; +import { ConfigState, GetAppConfiguration, ABP } from '@abp/ng.core'; import { ToasterService } from '@abp/ng.theme.shared'; -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Navigate } from '@ngxs/router-plugin'; import { Store } from '@ngxs/store'; @@ -10,13 +10,14 @@ import { catchError, finalize, switchMap, take, tap } from 'rxjs/operators'; import snq from 'snq'; import { RegisterRequest } from '../../models'; import { AccountService } from '../../services/account.service'; +import { PasswordRules, validatePassword } from '@ngx-validate/core'; const { maxLength, minLength, required, email } = Validators; @Component({ selector: 'abp-register', templateUrl: './register.component.html', }) -export class RegisterComponent { +export class RegisterComponent implements OnInit { form: FormGroup; inProgress: boolean; @@ -30,10 +31,38 @@ export class RegisterComponent { ) { this.oauthService.configure(this.store.selectSnapshot(ConfigState.getOne('environment')).oAuthConfig); this.oauthService.loadDiscoveryDocument(); + } + + ngOnInit() { + const passwordRules: ABP.Dictionary = this.store.selectSnapshot( + ConfigState.getSettings('Identity.Password'), + ); + const passwordRulesArr = [] as PasswordRules; + let requiredLength = 1; + + if ((passwordRules['Abp.Identity.Password.RequireDigit'] || '').toLowerCase() === 'true') { + passwordRulesArr.push('number'); + } + + if ((passwordRules['Abp.Identity.Password.RequireLowercase'] || '').toLowerCase() === 'true') { + passwordRulesArr.push('small'); + } + + if ((passwordRules['Abp.Identity.Password.RequireUppercase'] || '').toLowerCase() === 'true') { + passwordRulesArr.push('capital'); + } + + if (+(passwordRules['Abp.Identity.Password.RequiredUniqueChars'] || 0) > 0) { + passwordRulesArr.push('special'); + } + + if (Number.isInteger(+passwordRules['Abp.Identity.Password.RequiredLength'])) { + requiredLength = +passwordRules['Abp.Identity.Password.RequiredLength']; + } this.form = this.fb.group({ username: ['', [required, maxLength(255)]], - password: ['', [required, maxLength(32)]], + password: ['', [required, validatePassword(passwordRulesArr), minLength(requiredLength), maxLength(32)]], email: ['', [required, email]], }); }