import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { AuthService } from 'src/app/services/auth/auth.service'; import { AuthUserDTO } from 'src/app/models/auth/auth-user.dto'; import { Router } from '@angular/router'; import { catchError } from 'rxjs/operators'; import { ErrorDTO } from 'src/app/models/error/error-dto'; import { AuthErrorMessages } from 'src/app/models/auth/auth-error-messages.enum'; import { ServiceErrorCode } from 'src/app/models/error/service-error-code.enum'; import { AuthUserAtrErrors } from 'src/app/models/auth/auth-user-atr-errors'; import { SpinnerService } from 'src/app/services/spinner/spinner.service'; import { ThemeService } from 'src/app/services/theme/theme.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'] }) export class LoginComponent implements OnInit { loginForm: FormGroup; submitted = false; authUserAtrErrors: AuthUserAtrErrors; constructor( private authService: AuthService, private formBuilder: FormBuilder, private router: Router, private spinnerService: SpinnerService, private themeService: ThemeService ) { } ngOnInit(): void { this.spinnerService.showSpinner(); this.authService.isUserLoggedInAsync().then(result => { if (result) { this.router.navigate(['/home']); } this.initLoginForm(); this.resetStateFlags(); this.spinnerService.hideSpinner(); }); } resetStateFlags(): void { this.authUserAtrErrors = new AuthUserAtrErrors(); } initLoginForm(): void { this.loginForm = this.formBuilder.group({ email: [null, [Validators.required, Validators.email]], password: [null, [Validators.required, Validators.minLength(8)]] }); } register(): void { this.router.navigate(['/auth/register']); } forgotPassword(): void { this.router.navigate(['/auth/forgot-password']); } login(): void { this.submitted = true; this.resetStateFlags(); if (this.loginForm.invalid) { return; } this.spinnerService.showSpinner(); const user: AuthUserDTO = { firstName: '', lastName: '', eMail: this.loginForm.value.email, password: this.loginForm.value.password }; this.authService.login(user) .pipe(catchError(error => { if (error.error !== null) { const err: ErrorDTO = error.error; if (err.errorCode === ServiceErrorCode.InvalidData && err.message === AuthErrorMessages.UserIsEmpty) { this.authUserAtrErrors.email.required = true; this.authUserAtrErrors.password.required = true; } else if (err.errorCode === ServiceErrorCode.InvalidUser && err.message === AuthErrorMessages.UserNotFound) { this.authUserAtrErrors.email.wrongData = true; } else if (err.errorCode === ServiceErrorCode.InvalidUser && err.message === AuthErrorMessages.WrongPassword) { this.authUserAtrErrors.password.wrongData = true; } else if (err.errorCode === ServiceErrorCode.InvalidUser && err.message === AuthErrorMessages.EMailNotConfirmed) { this.authUserAtrErrors.email.notConfirmed = true; } error.error = null; } this.spinnerService.hideSpinner(); throw error; })) .subscribe(token => { this.authService.saveToken(token); this.themeService.loadTheme(); this.themeService.loadMenu(); this.spinnerService.hideSpinner(); this.router.navigate(['/home']); }); } }