Changeset 1f4846d for Farmatiko/ClientApp/src/app/login
- Timestamp:
- 11/01/20 00:55:08 (4 years ago)
- Branches:
- master
- Children:
- d23bf72
- Parents:
- 993189e
- Location:
- Farmatiko/ClientApp/src/app/login
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Farmatiko/ClientApp/src/app/login/login.component.html
r993189e r1f4846d 6 6 <div class="example-container"> 7 7 <mat-form-field> 8 <input matInput placeholder="Email" [(ngModel)]="this. email" formControlName="email">8 <input matInput placeholder="Email" [(ngModel)]="this.username" formControlName="email"> 9 9 </mat-form-field> 10 10 11 11 <mat-form-field> 12 <input matInput type="password" placeholder="Password" [(ngModel)]="this.passw d" formControlName="password">12 <input matInput type="password" placeholder="Password" [(ngModel)]="this.password" formControlName="password"> 13 13 </mat-form-field> 14 14 15 15 <button [disabled]="!loginForm.valid" mat-raised-button color="primary" (click)="loginPharmacyHead()" mat-button>Најави се</button> 16 17 <div class="checkbox mb-3 text-danger" *ngIf="loginError"> 18 Login failed. Please try again. 19 </div> 16 20 </div> 17 21 </form> -
Farmatiko/ClientApp/src/app/login/login.component.ts
r993189e r1f4846d 1 import { Component, OnInit } from '@angular/core';1 import { Component, OnInit, OnDestroy } from '@angular/core'; 2 2 import { FormGroup, FormControl, Validators } from '@angular/forms'; 3 3 import { Router, ActivatedRoute } from '@angular/router'; 4 import { Subscription } from 'rxjs'; 4 5 import { first } from 'rxjs/operators'; 5 import { AuthService } from '../shared/auth.service'; 6 import { AuthService } from '../shared/services/auth.service'; 7 import { finalize } from 'rxjs/operators'; 6 8 7 9 @Component({ … … 10 12 styleUrls: ['./login.component.css'] 11 13 }) 12 export class LoginComponent implements OnInit { 14 export class LoginComponent implements OnInit, OnDestroy { 15 busy = false; 13 16 loginForm: FormGroup; 14 email: string; 15 passwd: string; 16 errorMessage: string; 17 username = ''; 18 password = ''; 19 loginError = false; 20 private subscription: Subscription; 17 21 18 22 constructor(private authService: AuthService,private router: Router, private route: ActivatedRoute) { 19 23 this.loginForm = new FormGroup({ 20 email: new FormControl('', [Validators.required, Validators.email]),24 username: new FormControl('', [Validators.required, Validators.email]), 21 25 password: new FormControl('', [Validators.required]) 22 26 }); 23 27 } 28 ngOnDestroy(): void { 29 this.subscription?.unsubscribe(); 30 } 24 31 25 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 }); 26 43 } 27 44 28 45 loginPharmacyHead() { 29 this.authService.login(this.email,this.passwd) 30 .pipe(first()) 31 .subscribe((data) => { 32 if(data) { 33 this.router.navigate(['/dashboard']); 34 }}, 35 (err: any) => { 36 this.errorMessage = err.toString(); 37 }); 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 ); 38 62 this.loginForm.reset(); 39 63 }
Note:
See TracChangeset
for help on using the changeset viewer.