source: Farmatiko/ClientApp/src/app/login/login.component.ts@ 472dd7e

Last change on this file since 472dd7e was de9d697, checked in by Mile Jankuloski <mile.jankuloski@…>, 3 years ago

Patch bugs, minor changes

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