Skip to content
Snippets Groups Projects
Commit 0cd8aba9 authored by David Noah Donges's avatar David Noah Donges
Browse files

Merge branch '55-login-screen-register-component-logic' into 'master'

Resolve "login screen - register component logic"

Closes #55

See merge request swtp-block-ws17/arsnova-angular-frontend!51
parents 3faadfd0 6b348869
Branches
Tags
1 merge request!51Resolve "login screen - register component logic"
Pipeline #13083 passed with stage
in 32 seconds
<form>
<form (ngSubmit)="register(userName.value, userPassword1.value, userPassword2.value)" fxLayout="column"
fxLayoutAlign="space-around" fxLayoutGap="10px">
<mat-form-field class="input-block">
<input matInput #email maxlength="256" placeholder="E-mail">
<mat-hint align="start"><strong></strong></mat-hint>
<input matInput #userName placeholder="E-mail" [formControl]="usernameFormControl" [errorStateMatcher]="matcher">
<mat-error *ngIf="usernameFormControl.hasError('email') && !usernameFormControl.hasError('required')">Please enter a
<strong>valid</strong> email address.
</mat-error>
<mat-error *ngIf="usernameFormControl.hasError('required')">Email address is <strong>required</strong>.</mat-error>
</mat-form-field>
<mat-form-field class="input-block">
<input matInput type="password" #passwd maxlength="256" placeholder="Password">
<mat-hint align="start"><strong></strong></mat-hint>
<mat-hint align="end">{{passwd.value.length}} / 256</mat-hint>
<input matInput type="password" #userPassword1 placeholder="Password" [formControl]="password1FormControl"
[errorStateMatcher]="matcher">
<mat-error *ngIf="password1FormControl.hasError('required')">Password is <strong>required</strong>.</mat-error>
</mat-form-field>
<mat-form-field class="input-block">
<input matInput type="password" #passwd2 maxlength="256" placeholder="Verify password">
<mat-hint align="start"><strong></strong></mat-hint>
<mat-hint align="end">{{passwd2.value.length}} / 256</mat-hint>
<input matInput type="password" #userPassword2 placeholder="Verify password" [formControl]="password2FormControl"
[errorStateMatcher]="matcher">
<mat-error *ngIf="password2FormControl.hasError('required')">Password is <strong>required</strong>.</mat-error>
<mat-error *ngIf="password2FormControl.hasError('passwordIsEqual') && !password2FormControl.hasError('required')">
Passwords do <strong>not</strong> match.
</mat-error>
</mat-form-field>
<div *ngIf="passwd.value === passwd2.value && passwd.value.length !== 0">
<mat-icon>check_circle</mat-icon>
</div>
<div *ngIf="passwd.value !== passwd2.value && passwd.value.length !== 0">
<mat-icon>clear</mat-icon>
</div>
<button mat-raised-button color="primary" (click)="save(email.value, passwd.value, passwd2.value)">Register</button>
<button mat-raised-button color="primary" type="submit">Register</button>
</form>
import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { ErrorStateMatcher, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { FormControl, FormGroupDirective, NgForm, Validators } from '@angular/forms';
import { AuthenticationService } from '../authentication.service';
import { NotificationService } from '../notification.service';
import { Router } from '@angular/router';
export class RegisterErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const isSubmitted = form && form.submitted;
return (control && control.invalid && (control.dirty || control.touched || isSubmitted));
}
}
function validatePassword(password1: FormControl) {
return (formControl: FormControl) => {
const password1Value = password1.value;
const password2Value = formControl.value;
if (password1Value !== password2Value) {
return {
passwordIsEqual: {
isEqual: false
}
};
} else {
return null;
}
};
}
@Component({
selector: 'app-register',
......@@ -7,9 +35,16 @@ import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {
check = 0;
constructor(public dialogRef: MatDialogRef<RegisterComponent>,
usernameFormControl = new FormControl('', [Validators.required, Validators.email]);
password1FormControl = new FormControl('', [Validators.required]);
password2FormControl = new FormControl('', [Validators.required, validatePassword(this.password1FormControl)]);
matcher = new RegisterErrorStateMatcher();
constructor(public authenticationService: AuthenticationService,
public notificationService: NotificationService,
public dialogRef: MatDialogRef<RegisterComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
}
......@@ -20,12 +55,20 @@ export class RegisterComponent implements OnInit {
ngOnInit() {
}
save(email: string, password1: string, password2: string): void {
// console.log(email, password1, password2);
if (password1 === password2) {
console.log(true);
register(username: string, password1: string, password2: string): void {
if (!this.usernameFormControl.hasError('required') && !this.usernameFormControl.hasError('email') &&
!this.password1FormControl.hasError('required') &&
!this.password2FormControl.hasError('required') && !this.password2FormControl.hasError('passwordIsEqual')) {
this.authenticationService.register(username, password1).subscribe(result => {
if (result) {
this.notificationService.show('Successfully registered. Please check your mail!');
this.dialogRef.close();
} else {
this.notificationService.show('Oops! Something went wrong on our side...');
}
});
} else {
console.log(false);
this.notificationService.show('Please fit the requirements shown above.');
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment