registerService saves the user data and shows error messages too

master
Max 5 years ago
parent 21c83d8d01
commit 5e32897269

@ -15,10 +15,11 @@
</tr> </tr>
<tr> <tr>
<td>repeat password:</td> <td>repeat password:</td>
<td> <input #repeatpassword type="text" name="repeatpassword"><br></td> <td> <input #repeatpassword type="password" name="repeatpassword"><br></td>
</tr> </tr>
</table> </table>
<button type="registerbutton" (click)="onClickSubmit(username.value,email.value,password.value, repeatpassword.value)">Register</button> <button type="registerbutton" (click)="onClickSubmit(username.value,email.value,password.value, repeatpassword.value)">Register</button>
<p *ngIf="errorOccurred">{{errorMessage}}</p>
<br> <br>
<a href="/login">You are already part of greenvironment? - login</a> <a href="/login">You are already part of greenvironment? - login</a>
</div> </div>

@ -3,6 +3,7 @@ import {RegisterService} from '../../services/register/register.service';
import {Registration} from '../../models/registration'; import {Registration} from '../../models/registration';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {Md5} from 'ts-md5/dist/md5'; import {Md5} from 'ts-md5/dist/md5';
import { parseWebDriverCommand } from 'blocking-proxy/built/lib/webdriver_commands';
@Component({ @Component({
selector: 'registration', selector: 'registration',
@ -18,19 +19,33 @@ export class RegisterComponent implements OnInit {
this.registration = {username: null, passwordHash: null, email: null}; this.registration = {username: null, passwordHash: null, email: null};
} }
onClickSubmit(pUsername: string, pEmail: string, pPasswordHash: string,pPasswordHashRepeat: string ) { public registerError(error : any){
console.log('username: ' + pUsername); console.log(error.errors[0].message);
console.log('email: ' + pEmail); this.errorOccurred = true;
this.errorMessage = error.errors[0].message;
}
if(pPasswordHash == pPasswordHashRepeat){ onClickSubmit(pUsername: string, pEmail: string, pPasswordHash: string,pPasswordHashRepeat: string ) {
console.log('password same'); this.errorOccurred = false;
this.errorMessage = " ";
if(this.passwordSame(pPasswordHash,pPasswordHashRepeat)){
this.registration.username = pUsername this.registration.username = pUsername
this.registration.email = pEmail this.registration.email = pEmail
const md5 = new Md5(); const md5 = new Md5();
this.registration.passwordHash = md5.appendStr(pPasswordHash).end() as string this.registration.passwordHash = md5.appendStr(pPasswordHash).end() as string
this.registerService.register(this.registration, error => this.registerError(error.json()));
}
}
this.registerService.register(this.registration) passwordSame(pwd: string,pwd2: string){
} else{console.log('password NOT same'); } if(pwd == pwd2){
console.log('password same');
return true;
} else{
this.errorOccurred = true;
this.errorMessage = "not the same password";
return false;
}
} }
ngOnInit() {} ngOnInit() {}

@ -1,15 +1,18 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {Http, URLSearchParams, Headers} from '@angular/http'; import {Http, URLSearchParams, Headers} from '@angular/http';
import { Registration } from '../../models/registration'; import { Registration } from '../../models/registration';
import {Router} from '@angular/router';
import { DatasharingService } from '../datasharing.service';
import { User } from 'src/app/models/user';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class RegisterService { export class RegisterService {
constructor(private http: Http) { } constructor(private http: Http, private data: DatasharingService,private router: Router) { }
public register(registration: Registration) { public register(registration: Registration, errorCb: any) {
//let url = './graphql' //let url = './graphql'
let url = 'https://greenvironment.net/graphql' let url = 'https://greenvironment.net/graphql'
@ -17,12 +20,42 @@ export class RegisterService {
let headers = new Headers(); let headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
return this.http.post(url, this.buildJson(registration)).subscribe(response => console.log(response.text())); return this.http.post(url, this.buildJson(registration))
.subscribe(response => {
console.log(response.text());
this.registerSuccess();
this.updateUserInfo(response.json())
}, errorCb
);
}
public registerSuccess(){
console.log('alles supi dupi');
//do routing
this.router.navigateByUrl('');
}
public updateUserInfo(response : any){
const user: User = new User();
user.loggedIn = true;
user.userID = response.data.register.id;
user.username = response.data.register.name;
user.handle = response.data.register.handle;
user.email = response.data.register.email;
user.points = response.data.register.points;
user.level = response.data.register.level;
user.friendIDs = response.data.register.friends;
user.groupIDs = response.data.register.groups;
user.chatIDs = response.data.register.chats;
user.requestIDs = response.data.register.requests;
this.data.changeUserInfo(user)
} }
public buildJson(registration: Registration): any { public buildJson(registration: Registration): any {
const body = {query: `mutation($username: String, $email: String, $pwHash: String) { const body = {query: `mutation($username: String, $email: String, $pwHash: String) {
register(username: $username, email: $email, passwordHash: $pwHash) {id} register(username: $username, email: $email, passwordHash: $pwHash) {id, name, handle, points, level, friends{id}, groups{id},chats{id}}
}`, variables: { }`, variables: {
email: registration.email, email: registration.email,
pwHash: registration.passwordHash, pwHash: registration.passwordHash,

Loading…
Cancel
Save