import { Component, OnInit } from '@angular/core'; import { LoginService } from '../core/auth/login.service'; import { Router, ActivatedRoute } from '@angular/router'; import { Validators, FormGroup, FormBuilder } from '@angular/forms'; import { first } from 'rxjs/operators'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'], }) export class LoginComponent implements OnInit { loginForm: FormGroup; returnUrl: string; error: string; loading = false; submitted = false; constructor( private loginService: LoginService, private router: Router, private route: ActivatedRoute, private formBuilder: FormBuilder ) { if (this.loginService.currentHoodAdminValue) { this.router.navigate(['/dashboard']); } } ngOnInit(): void { this.loginForm = this.formBuilder.group({ email: ['', Validators.required], password: ['', Validators.required], }); this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/dashboard'; } onSubmit() { this.submitted = true; if (this.loginForm.invalid) { return; } this.loading = true; this.loginService .login( this.loginForm.controls.email.value, this.loginForm.controls.password.value ) .pipe(first()) .subscribe( (data) => { this.router.navigate([this.returnUrl]); }, (error) => { this.error = 'Wrong credentials! Try again.'; this.loading = false; } ); } }