[1f4846d] | 1 | import { Component, OnInit, OnDestroy } from '@angular/core';
|
---|
[ee137aa] | 2 | import { FormGroup, FormControl, Validators } from '@angular/forms';
|
---|
| 3 | import { Router, ActivatedRoute } from '@angular/router';
|
---|
[1f4846d] | 4 | import { Subscription } from 'rxjs';
|
---|
[993189e] | 5 | import { first } from 'rxjs/operators';
|
---|
[1f4846d] | 6 | import { AuthService } from '../shared/services/auth.service';
|
---|
| 7 | import { finalize } from 'rxjs/operators';
|
---|
[785b8bd] | 8 |
|
---|
| 9 | @Component({
|
---|
| 10 | selector: 'app-login',
|
---|
| 11 | templateUrl: './login.component.html',
|
---|
| 12 | styleUrls: ['./login.component.css']
|
---|
| 13 | })
|
---|
[1f4846d] | 14 | export class LoginComponent implements OnInit, OnDestroy {
|
---|
| 15 | busy = false;
|
---|
[785b8bd] | 16 | loginForm: FormGroup;
|
---|
[1f4846d] | 17 | username = '';
|
---|
| 18 | password = '';
|
---|
| 19 | loginError = false;
|
---|
| 20 | private subscription: Subscription;
|
---|
[785b8bd] | 21 |
|
---|
[993189e] | 22 | constructor(private authService: AuthService,private router: Router, private route: ActivatedRoute) {
|
---|
[785b8bd] | 23 | this.loginForm = new FormGroup({
|
---|
[1f4846d] | 24 | username: new FormControl('', [Validators.required, Validators.email]),
|
---|
[785b8bd] | 25 | password: new FormControl('', [Validators.required])
|
---|
| 26 | });
|
---|
| 27 | }
|
---|
[1f4846d] | 28 | ngOnDestroy(): void {
|
---|
| 29 | this.subscription?.unsubscribe();
|
---|
| 30 | }
|
---|
[785b8bd] | 31 |
|
---|
| 32 | ngOnInit(): void {
|
---|
[1f4846d] | 33 | this.subscription = this.authService.user$.subscribe((x) => {
|
---|
| 34 | if (this.route.snapshot.url[0].path === 'login') {
|
---|
| 35 | const accessToken = localStorage.getItem('access_token');
|
---|
| 36 | const refreshToken = localStorage.getItem('refresh_token');
|
---|
| 37 | if (x && accessToken && refreshToken) {
|
---|
| 38 | const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '';
|
---|
| 39 | this.router.navigate([returnUrl]);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | });
|
---|
[785b8bd] | 43 | }
|
---|
| 44 |
|
---|
[ee137aa] | 45 | loginPharmacyHead() {
|
---|
[1f4846d] | 46 | if (!this.username || !this.password) {
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
| 49 | this.busy = true;
|
---|
| 50 | const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '';
|
---|
| 51 | this.authService
|
---|
| 52 | .login(this.username, this.password)
|
---|
| 53 | .pipe(finalize(() => (this.busy = false)))
|
---|
| 54 | .subscribe(
|
---|
[ad60966] | 55 | (data) => {
|
---|
[1db5673] | 56 | if(data.role === 'Admin') {
|
---|
[ad60966] | 57 | this.router.navigate(['/admin']);
|
---|
| 58 | }
|
---|
| 59 | else {
|
---|
| 60 | this.router.navigate(['/dashboard']);
|
---|
| 61 | }
|
---|
[1f4846d] | 62 | },
|
---|
| 63 | () => {
|
---|
| 64 | this.loginError = true;
|
---|
| 65 | }
|
---|
| 66 | );
|
---|
[993189e] | 67 | this.loginForm.reset();
|
---|
[ee137aa] | 68 | }
|
---|
[785b8bd] | 69 | }
|
---|