diff --git a/registration.php b/registration.php index 267d144..87c6dd4 100644 --- a/registration.php +++ b/registration.php @@ -1,20 +1,28 @@ getTimestamp(); -$ +$returnData = array(); +$date = new DateTime(); -switch( $key ){ - case "validation": - - break; - case "create": - - break; - default: - echo "false"; +$passwordSalt = "sexfamemoney$U046qKlL$moneyfamesex"; + + +$hashedPassword = crypt( $password, $passwordSalt ); +$hashedId = crypt( $id, $passwordSalt ); + +/* +For login: +if (hash_equals($hashed_password, crypt($user_input, $hashed_password))) { + echo "Password verified!"; } +*/ + +$returnData["password"] = $hashed_password; +$returnData["id"] = $hashedId; + +echo( json_encode( $returnData ) ); ?> \ No newline at end of file diff --git a/registrationCreation.php b/registrationCreation.php new file mode 100644 index 0000000..796332d --- /dev/null +++ b/registrationCreation.php @@ -0,0 +1,67 @@ +getTimestamp() . $userName . $date->getTimestamp(); + +$passwordSalt = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; + + +$hashedPassword = crypt( $password, $passwordSalt ); +$hashedId = crypt( $id, $passwordSalt ); + +/* +For login: +if (hash_equals($hashed_password, crypt($user_input, $hashed_password))) { + echo "Password verified!"; +} +*/ + +$returnData["password"] = $hashedPassword; +$returnData["id"] = $hashedId; + +//echo( json_encode( $returnData ) ); + +$url = 'http://192.241.155.78/api.php/users/'; +$fields = array( + 'firstName' => urlencode( $firstName ), + 'lastName' => urlencode( $lastName ), + 'email' => urlencode( $email ), + 'userName' => urlencode( $userName ), + 'password' => urlencode( $hashedPassword ), + 'id' => urlencode( $hashedId ) +); + +//url-ify the data for the POST +//foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } +//rtrim($fields_string, '&'); + +//open connection +$ch = curl_init( $url ); + +//set the url, number of POST vars, POST data +//curl_setopt($ch,CURLOPT_URL, $url); +curl_setopt($ch, CURLOPT_POST, 1); + +curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); + +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + +//execute post +$result = curl_exec( $ch ); +//close connection +curl_close( $ch ); + +echo( $result ); +exit(); + +?> \ No newline at end of file diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 577ab94..b8a1eb8 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -27,6 +27,7 @@ import { RegisterComponent } from './register/register.component'; import { RegistrationService } from './registration.service'; import { UsernameValidator } from './validators/username.validator' +import { EmailValidator } from './validators/email.validator' @@ -53,7 +54,7 @@ import { UsernameValidator } from './validators/username.validator' MatPaginatorModule, MatFormFieldModule ], - providers: [GamesService, RegistrationService, UsernameValidator], + providers: [GamesService, RegistrationService, UsernameValidator, EmailValidator], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/register/register.component.ts b/src/app/register/register.component.ts index c300231..3f657e2 100644 --- a/src/app/register/register.component.ts +++ b/src/app/register/register.component.ts @@ -4,6 +4,7 @@ import { FormBuilder, FormGroup, FormControl, Validators, AbstractControl } from import { RegistrationService } from '../registration.service'; import { UsernameValidator } from '../validators/username.validator' +import { EmailValidator } from '../validators/email.validator' @Component({ @@ -18,7 +19,10 @@ export class RegisterComponent implements OnInit { constructor( private registrationService: RegistrationService, - private usernameValidator: UsernameValidator + private usernameValidator: UsernameValidator, + private emailValidator: EmailValidator, + public formBuilder: FormBuilder + ){ } ngOnInit() { @@ -27,83 +31,47 @@ export class RegisterComponent implements OnInit { } buildForm(){ - const formGroup = {}; - formGroup["firstName"] = new FormControl( "", this.mapValidators({ - required: true, - min: "3", - max: "50" - }) ); - formGroup["lastName"] = new FormControl( "", this.mapValidators({ - required: true, - min: "3", - max: "50" - }) ); - formGroup["email"] = new FormControl( "", this.mapValidators({ - required: true, - email: true, - max: "100" - }) ); - formGroup["userName"] = new FormControl( "", this.mapValidators({ - ValidateUserName: true, - required: true, - min: "5", - max: "25" - }) ); - formGroup["password"] = new FormControl( "", this.mapValidators({ - required: true, - min: "7", - max: "25" - }) ); - - this.form = new FormGroup(formGroup); + this.form = this.formBuilder.group({ + firstName: ['', Validators.compose([ + Validators.maxLength(50), + Validators.minLength(3), + Validators.required + ])], + lastName: ['', Validators.compose([ + Validators.maxLength(50), + Validators.minLength(3), + Validators.required + ])], + email: ['', Validators.compose([ + Validators.maxLength(100), + Validators.email, + Validators.required + ]), + this.emailValidator.checkEmail.bind(this.emailValidator) + ], + userName: ['', Validators.compose([ + Validators.maxLength(25), + Validators.minLength(5), + Validators.required + ]), + this.usernameValidator.checkUsername.bind(this.usernameValidator) + ], + password: ['', Validators.compose([ + Validators.maxLength(25), + Validators.minLength(5), + Validators.required + ])] + }); } - private mapValidators(validators) { - const formValidators = []; + onSubmit( form ){ + this.registrationService.createNewUser( form ).subscribe( data => { + console.log(data); + //this.router.navigateByUrl("/login"); + }); - for( var key in validators ){ - if( key == "required" ) { - if( validators.required ){ - formValidators.push( Validators.required ); - } - }else if( key == "email" ) { - if( validators.email ){ - formValidators.push( Validators.email ); - } - }else if( key == "min" ) { - formValidators.push(Validators.min(validators[key])); - }else if( key == "max" ) { - formValidators.push(Validators.max(validators[key])); - }else if( key == "ValidateUserName" ){ - formValidators.push( this.usernameValidator.checkUsername.bind( this.usernameValidator ) ); - } - } - return formValidators; } - - - -/* - validateUserName( control: AbstractControl ){ - - this.registrationService.validateUserName( control.value ).subscribe( data => { - console.log( data ); - if( control.value != data.username ){ - return{ - validUserName: true - }; - } - return null; - - }); - - } - */ - - - - -} +} \ No newline at end of file diff --git a/src/app/registration.service.ts b/src/app/registration.service.ts index 04a6386..e699ad2 100644 --- a/src/app/registration.service.ts +++ b/src/app/registration.service.ts @@ -1,31 +1,53 @@ import { Injectable } from '@angular/core'; -import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; +const httpOptions = { + headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }) +} + @Injectable() export class RegistrationService { APIURL = "http://192.241.155.78/api.php"; + registrationUrl = "http://192.241.155.78/registrationCreation.php"; + + /* Needed for post, could be moved to local method variable */ + params; constructor( private http: HttpClient ){ } - /* validateUserName( userName ): Observable { - return this.http.get( "http://www.pugludos.com/registration.php?key=validation&type=username&value=" + userName ) + return this.http.get( this.APIURL + "/users?filter=userName,eq," + userName + "&transform=1" ) .map(res => { return( res ); }); } - */ - validateUserName( userName ): Observable { - return this.http.get( this.APIURL + "/users?filter=userName,cs," + userName + "&transform=1" ) + validateEmail( email ): Observable { + return this.http.get( this.APIURL + "/users?filter=email,eq," + email + "&transform=1" ) + .map(res => { + return( + res + ); + }); + } + + createNewUser( userData ): Observable{ + + + /* needed for content-type x-www */ + this.params = new HttpParams({ + fromObject: userData + }); + + return this.http.post( this.registrationUrl, this.params, httpOptions ) .map(res => { console.log(res) return( @@ -34,4 +56,5 @@ export class RegistrationService { }); } + } diff --git a/src/app/validators/email.validator.ts b/src/app/validators/email.validator.ts new file mode 100644 index 0000000..7edd489 --- /dev/null +++ b/src/app/validators/email.validator.ts @@ -0,0 +1,38 @@ +import { Injectable } from '@angular/core'; +import { FormControl } from '@angular/forms'; +import { RegistrationService } from '../registration.service'; + +@Injectable() +export class EmailValidator { + + debouncer: any; + + constructor( + private registrationService: RegistrationService + ){ } + + checkEmail( control: FormControl ): any{ + clearTimeout(this.debouncer); + + return new Promise(resolve => { + this.debouncer = setTimeout(() => { + this.registrationService.validateEmail(control.value).subscribe((res) => { + console.log(res.users.length) + if(res.users.length === 0){ + resolve(null); + }else{ + resolve({'emailInUse': true}); + } +/* + if(res.ok){ + resolve(null); + } + }, (err) => { + resolve({'usernameInUse': true}); + */ + + }); + }, 1000); + }); + } +} \ No newline at end of file diff --git a/src/app/validators/username.validator.ts b/src/app/validators/username.validator.ts index e79afa0..76fd3a0 100644 --- a/src/app/validators/username.validator.ts +++ b/src/app/validators/username.validator.ts @@ -17,11 +17,20 @@ export class UsernameValidator { return new Promise(resolve => { this.debouncer = setTimeout(() => { this.registrationService.validateUserName(control.value).subscribe((res) => { + console.log(res.users.length) + if(res.users.length === 0){ + resolve(null); + }else{ + resolve({'usernameInUse': true}); + } +/* if(res.ok){ resolve(null); } }, (err) => { resolve({'usernameInUse': true}); + */ + }); }, 1000); });