source: Farmatiko/ClientApp/src/app/login/login.component.ts@ 1f4846d

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

Jwt token auth interceptors, services and guards

  • Property mode set to 100644
File size: 2.0 KB
Line 
1import { Component, OnInit, OnDestroy } from '@angular/core';
2import { FormGroup, FormControl, Validators } from '@angular/forms';
3import { Router, ActivatedRoute } from '@angular/router';
4import { Subscription } from 'rxjs';
5import { first } from 'rxjs/operators';
6import { AuthService } from '../shared/services/auth.service';
7import { finalize } from 'rxjs/operators';
8
9@Component({
10 selector: 'app-login',
11 templateUrl: './login.component.html',
12 styleUrls: ['./login.component.css']
13})
14export class LoginComponent implements OnInit, OnDestroy {
15 busy = false;
16 loginForm: FormGroup;
17 username = '';
18 password = '';
19 loginError = false;
20 private subscription: Subscription;
21
22 constructor(private authService: AuthService,private router: Router, private route: ActivatedRoute) {
23 this.loginForm = new FormGroup({
24 username: new FormControl('', [Validators.required, Validators.email]),
25 password: new FormControl('', [Validators.required])
26 });
27 }
28 ngOnDestroy(): void {
29 this.subscription?.unsubscribe();
30 }
31
32 ngOnInit(): void {
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 });
43 }
44
45 loginPharmacyHead() {
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(
55 () => {
56 this.router.navigate(['/dashboard']);
57 },
58 () => {
59 this.loginError = true;
60 }
61 );
62 this.loginForm.reset();
63 }
64}
Note: See TracBrowser for help on using the repository browser.