138 lines
4.0 KiB
TypeScript
138 lines
4.0 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { catchError } from 'rxjs/operators';
|
|
import { AuthErrorMessages } from 'src/app/models/auth/auth-error-messages.enum';
|
|
import { AuthUserDTO } from 'src/app/models/auth/auth-user.dto';
|
|
import { AuthUserAtrErrors } from 'src/app/models/auth/auth-user-atr-errors';
|
|
import { ErrorDTO } from 'src/app/models/error/error-dto';
|
|
import { ServiceErrorCode } from 'src/app/models/error/service-error-code.enum';
|
|
import { AuthService } from 'src/app/services/auth/auth.service';
|
|
import { SpinnerService } from 'src/app/services/spinner/spinner.service';
|
|
|
|
@Component({
|
|
selector: 'app-registration',
|
|
templateUrl: './registration.component.html',
|
|
styleUrls: ['./registration.component.scss']
|
|
})
|
|
export class RegistrationComponent implements OnInit {
|
|
|
|
loginForm: FormGroup;
|
|
submitted = false;
|
|
authUserAtrErrors: AuthUserAtrErrors;
|
|
repeatErrors = {
|
|
email: false,
|
|
password: false
|
|
};
|
|
|
|
showEMailConfirmation = false;
|
|
showEMailConfirmationError = false;
|
|
|
|
constructor(
|
|
private authService: AuthService,
|
|
private formBuilder: FormBuilder,
|
|
private router: Router,
|
|
private spinnerService: SpinnerService,
|
|
private route: ActivatedRoute
|
|
) {
|
|
this.spinnerService.showSpinner();
|
|
this.authService.isUserLoggedInAsync().then(res => {
|
|
if (res) {
|
|
this.router.navigate(['/home']);
|
|
}
|
|
this.spinnerService.hideSpinner();
|
|
});
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.initLoginForm();
|
|
this.resetStateFlags();
|
|
this.confirmEMail();
|
|
}
|
|
|
|
resetStateFlags(): void {
|
|
this.authUserAtrErrors = new AuthUserAtrErrors();
|
|
this.repeatErrors = {
|
|
email: false,
|
|
password: false
|
|
};
|
|
}
|
|
|
|
login(): void {
|
|
this.router.navigate(['/auth/login']);
|
|
}
|
|
|
|
initLoginForm(): void {
|
|
this.loginForm = this.formBuilder.group({
|
|
firstName: [null, Validators.required],
|
|
lastName: [null, Validators.required],
|
|
email: [null, [Validators.required, Validators.email]],
|
|
emailRepeat: [null, [Validators.required, Validators.email]],
|
|
password: [null, [Validators.required, Validators.minLength(8)]],
|
|
passwordRepeat: [null, [Validators.required, Validators.minLength(8)]]
|
|
});
|
|
}
|
|
|
|
register(): void {
|
|
this.submitted = true;
|
|
this.resetStateFlags();
|
|
|
|
// stop here if form is invalid
|
|
if (this.loginForm.invalid) {
|
|
return;
|
|
}
|
|
|
|
if (this.loginForm.value.email !== this.loginForm.value.emailRepeat) {
|
|
this.repeatErrors.email = true;
|
|
return;
|
|
}
|
|
|
|
if (this.loginForm.value.password !== this.loginForm.value.passwordRepeat) {
|
|
this.repeatErrors.password = true;
|
|
return;
|
|
}
|
|
|
|
this.spinnerService.showSpinner();
|
|
const user: AuthUserDTO = {
|
|
firstName: this.loginForm.value.firstName,
|
|
lastName: this.loginForm.value.lastName,
|
|
eMail: this.loginForm.value.email,
|
|
password: this.loginForm.value.password
|
|
};
|
|
|
|
this.authService.register(user)
|
|
.pipe(catchError(error => {
|
|
if (error.error !== null) {
|
|
const err: ErrorDTO = error.error;
|
|
|
|
if (err.errorCode === ServiceErrorCode.InvalidUser && err.message === AuthErrorMessages.UserAlreadyExists) {
|
|
this.authUserAtrErrors.email.wrongData = true;
|
|
}
|
|
}
|
|
this.spinnerService.hideSpinner();
|
|
throw error;
|
|
}))
|
|
.subscribe(resp => {
|
|
this.spinnerService.hideSpinner();
|
|
this.router.navigate(['/auth/login']);
|
|
});
|
|
}
|
|
|
|
confirmEMail(): void {
|
|
const id = this.route.snapshot.params.id;
|
|
if (id) {
|
|
this.spinnerService.showSpinner();
|
|
this.authService.confirmEMail(id)
|
|
.pipe(catchError(error => {
|
|
this.router.navigate(['/auth/login']);
|
|
this.spinnerService.hideSpinner();
|
|
throw error;
|
|
}))
|
|
.subscribe(resp => {
|
|
this.spinnerService.hideSpinner();
|
|
this.router.navigate(['/auth/login']);
|
|
});
|
|
}
|
|
}
|
|
}
|