From 0e4ceac959db9a6ee4a9249d48ca081256d10fe8 Mon Sep 17 00:00:00 2001 From: Christopher Koch Date: Wed, 25 Apr 2018 20:12:10 -0400 Subject: [PATCH] validation --- phpComponets/api.php | 2 +- src/app/app.module.ts | 2 - src/app/creditcardvalidator.directive.ts | 31 ------------- src/app/creditcardvalidatorx.directive.ts | 56 ----------------------- src/app/games.service.ts | 2 +- src/app/register/register.component.ts | 5 -- src/app/registration.service.ts | 5 +- src/app/validators/username.validator.ts | 20 +++----- 8 files changed, 12 insertions(+), 111 deletions(-) delete mode 100644 src/app/creditcardvalidator.directive.ts delete mode 100644 src/app/creditcardvalidatorx.directive.ts diff --git a/phpComponets/api.php b/phpComponets/api.php index 12b665f..ca4f8b8 100644 --- a/phpComponets/api.php +++ b/phpComponets/api.php @@ -2717,7 +2717,7 @@ class PHP_CRUD_API { 'dbengine'=>'MySQL', 'hostname'=>'localhost', 'username'=>'lazyp_workadmin', - 'password'=>'ZGmH0HQPo5ocbOuN', + 'password'=>'GH5fZF0iCtLnHLrz', 'database'=>'LudosData', 'charset'=>'utf8mb4' )); diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 01d4886..577ab94 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -26,8 +26,6 @@ import { LoginComponent } from './login/login.component'; import { RegisterComponent } from './register/register.component'; import { RegistrationService } from './registration.service'; - -//import { CreditCardValidator } from './creditcardvalidator.directive' import { UsernameValidator } from './validators/username.validator' diff --git a/src/app/creditcardvalidator.directive.ts b/src/app/creditcardvalidator.directive.ts deleted file mode 100644 index c65f234..0000000 --- a/src/app/creditcardvalidator.directive.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Directive, forwardRef } from '@angular/core'; -import { NG_VALIDATORS, AbstractControl, ValidationErrors, Validator, FormControl } from '@angular/forms'; - -@Directive({ - selector: '[validCreditCard]', - providers: [ - { provide: NG_VALIDATORS, useExisting: CreditCardValidator, multi: true } - ] -}) -export class CreditCardValidator implements Validator { - - validate(c: FormControl): ValidationErrors | null { - return CreditCardValidator.validateCcNumber(c); - } - - static validateCcNumber(control: FormControl): ValidationErrors | null { - if (!(control.value.startsWith('37') - || control.value.startsWith('4') - || control.value.startsWith('5')) - ) { - // Return error if card is not Amex, Visa or Mastercard - return { creditCard: 'Your credit card number is not from a supported credit card provider' }; - } else if (control.value.length !== 16) { - console.log(control.value) - // Return error if length is not 16 digits - return { creditCard: 'A credit card number must be 16-digit long' }; - } - // If no error, return null - return null; - } -} \ No newline at end of file diff --git a/src/app/creditcardvalidatorx.directive.ts b/src/app/creditcardvalidatorx.directive.ts deleted file mode 100644 index baffce0..0000000 --- a/src/app/creditcardvalidatorx.directive.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { Directive, forwardRef } from '@angular/core'; -import { NG_VALIDATORS, AbstractControl, ValidationErrors, Validator, FormControl } from '@angular/forms'; -import { RegistrationService } from './registration.service'; - -@Directive({ - selector: '[validCreditCard]', - providers: [ - { provide: NG_VALIDATORS, useExisting: CreditCardValidatorX, multi: true } - ] -}) -export class CreditCardValidatorX implements Validator { - - constructor( - private registrationService2: RegistrationService - ){ } - - validate(c: FormControl): ValidationErrors | null { - return CreditCardValidatorX.validateCcNumber(c); - } - - - validateUserName( control: FormControl ){ - - this.registrationService2.validateUserName( control.value ).subscribe( data => { - console.log( data ); - if( control.value != data.username ){ - return{ - validUserName: true - }; - } - return null; - - }); - - } - - static validateCcNumber(control: FormControl): ValidationErrors | null { - if (!(control.value.startsWith('37') - || control.value.startsWith('4') - || control.value.startsWith('5')) - ) { - // Return error if card is not Amex, Visa or Mastercard - return { creditCard: 'Your credit card number is not from a supported credit card provider' }; - } else if (control.value.length !== 16) { - console.log(control.value) - // Return error if length is not 16 digits - return { creditCard: 'A credit card number must be 16-digit long' }; - } - // If no error, return null - return null; - } - - - - -} \ No newline at end of file diff --git a/src/app/games.service.ts b/src/app/games.service.ts index 0bd85e0..b401793 100644 --- a/src/app/games.service.ts +++ b/src/app/games.service.ts @@ -20,7 +20,7 @@ const httpOptionsPut = { @Injectable() export class GamesService { - APIURL = "http://192.241.155.78/ludosdata/api.php"; + APIURL = "http://192.241.155.78/api.php"; constructor( private http: HttpClient diff --git a/src/app/register/register.component.ts b/src/app/register/register.component.ts index 0ad7f4b..c300231 100644 --- a/src/app/register/register.component.ts +++ b/src/app/register/register.component.ts @@ -6,8 +6,6 @@ import { RegistrationService } from '../registration.service'; import { UsernameValidator } from '../validators/username.validator' -//import { ValidateUserName } from '../validators/username.validator'; - @Component({ selector: 'app-register', templateUrl: './register.component.html', @@ -17,7 +15,6 @@ export class RegisterComponent implements OnInit { form: any; loading = false; - //userNameValidator = new UserNameValidator(null); constructor( private registrationService: RegistrationService, @@ -79,8 +76,6 @@ export class RegisterComponent implements OnInit { }else if( key == "max" ) { formValidators.push(Validators.max(validators[key])); }else if( key == "ValidateUserName" ){ - //formValidators.push( this.validateUserName ); - //formValidators.push( CreditCardValidator.validateCcNumber ); formValidators.push( this.usernameValidator.checkUsername.bind( this.usernameValidator ) ); } } diff --git a/src/app/registration.service.ts b/src/app/registration.service.ts index e2a4fa1..04a6386 100644 --- a/src/app/registration.service.ts +++ b/src/app/registration.service.ts @@ -7,7 +7,7 @@ import 'rxjs/add/operator/map'; @Injectable() export class RegistrationService { - APIURL = "http://192.241.155.78/ludosdata/api.php"; + APIURL = "http://192.241.155.78/api.php"; constructor( private http: HttpClient @@ -25,8 +25,9 @@ export class RegistrationService { */ validateUserName( userName ): Observable { - return this.http.get( this.APIURL + "/users?filter=email,cs," + userName + "&transform=1" ) + return this.http.get( this.APIURL + "/users?filter=userName,cs," + userName + "&transform=1" ) .map(res => { + console.log(res) return( res ); diff --git a/src/app/validators/username.validator.ts b/src/app/validators/username.validator.ts index ecd0d53..e79afa0 100644 --- a/src/app/validators/username.validator.ts +++ b/src/app/validators/username.validator.ts @@ -4,21 +4,18 @@ import { RegistrationService } from '../registration.service'; @Injectable() export class UsernameValidator { - + debouncer: any; + + constructor( + private registrationService: RegistrationService + ){ } - constructor(private registrationService: RegistrationService){ - - } - - checkUsername(control: FormControl): any { - + checkUsername( control: FormControl ): any{ clearTimeout(this.debouncer); - + return new Promise(resolve => { - this.debouncer = setTimeout(() => { - this.registrationService.validateUserName(control.value).subscribe((res) => { if(res.ok){ resolve(null); @@ -26,10 +23,7 @@ export class UsernameValidator { }, (err) => { resolve({'usernameInUse': true}); }); - }, 1000); - }); } - } \ No newline at end of file