import { Component, OnInit, OnDestroy } from '@angular/core'; import { FormGroup, FormControl, Validators } from '@angular/forms'; import { Router, ActivatedRoute } from '@angular/router'; import { Subscription } from 'rxjs'; import { first } from 'rxjs/operators'; import { AuthService } from '../shared/services/auth.service'; import { finalize } from 'rxjs/operators'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit, OnDestroy { busy = false; loginForm: FormGroup; username = ''; password = ''; loginError = false; private subscription: Subscription; constructor(private authService: AuthService,private router: Router, private route: ActivatedRoute) { this.loginForm = new FormGroup({ username: new FormControl('', [Validators.required, Validators.email]), password: new FormControl('', [Validators.required]) }); } ngOnDestroy(): void { this.subscription?.unsubscribe(); } ngOnInit(): void { this.subscription = this.authService.user$.subscribe((x) => { if (this.route.snapshot.url[0].path === 'login') { const accessToken = localStorage.getItem('access_token'); const refreshToken = localStorage.getItem('refresh_token'); if (x && accessToken && refreshToken) { const returnUrl = this.route.snapshot.queryParams['returnUrl'] || ''; this.router.navigate([returnUrl]); } } }); } loginPharmacyHead() { if (!this.username || !this.password) { return; } this.busy = true; const returnUrl = this.route.snapshot.queryParams['returnUrl'] || ''; this.authService .login(this.username, this.password) .pipe(finalize(() => (this.busy = false))) .subscribe( () => { this.router.navigate(['/dashboard']); }, () => { this.loginError = true; } ); this.loginForm.reset(); } }