Ignore:
Timestamp:
11/01/20 00:55:08 (4 years ago)
Author:
Mile Jankuloski <mile.jankuloski@…>
Branches:
master
Children:
d23bf72
Parents:
993189e
Message:

Jwt token auth interceptors, services and guards

Location:
Farmatiko/ClientApp/src/app
Files:
6 added
2 deleted
9 edited
1 moved

Legend:

Unmodified
Added
Removed
  • Farmatiko/ClientApp/src/app/admin/admin.component.ts

    r993189e r1f4846d  
    44import { Router } from '@angular/router';
    55import { IPharmacyHead, IPharmacyHeadRequest, IPharmacy } from '../shared/interfaces';
    6 import { DataService } from '../shared/data.service';
     6import { DataService } from '../shared/services/data.service';
    77import { EditPharmacyHeadDialogComponent } from '../dialogs/edit-pharmacy-head-dialog/edit-pharmacy-head-dialog.component';
    88import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
  • Farmatiko/ClientApp/src/app/app.module.ts

    r993189e r1f4846d  
    77import { ReactiveFormsModule } from '@angular/forms';
    88
    9 import { DataService } from './shared/data.service';
     9import { CoreModule } from './shared/core.module';
    1010
     11import { AuthGuard } from './shared/guards/auth.guard';
     12import { DataService } from './shared/services/data.service';
    1113import { AppComponent } from './app.component';
    1214import { NavMenuComponent } from './nav-menu/nav-menu.component';
     
    2527import { EditPharmacyHeadDialogComponent } from './dialogs/edit-pharmacy-head-dialog/edit-pharmacy-head-dialog.component';
    2628import { PharmacyHeadDialogComponent } from './nav-menu/dialogs/pharmacy-head-dialog/pharmacy-head-dialog.component';
    27 import { AuthGuard } from './shared/auth.guard';
    2829
    2930@NgModule({
     
    5354      { path: 'mapa', component: CounterComponent },
    5455      { path: 'koronavirus', component: KoronaComponent },
    55       { path: 'admin', component: AdminComponent },
     56      { path: 'admin', component: AdminComponent, canActivate: [AuthGuard] },
    5657      { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
    5758      { path: 'login', component: LoginComponent }
     
    5960    BrowserAnimationsModule,
    6061    MaterialModule,
    61     ReactiveFormsModule
     62    ReactiveFormsModule,
     63    CoreModule
    6264  ],
    6365  providers: [
  • Farmatiko/ClientApp/src/app/counter/counter.component.ts

    r993189e r1f4846d  
    11import { Component, OnInit } from '@angular/core';
    22import { IHealthFacilities, IHealthcareWorkers } from '../shared/interfaces';
    3 import { DataService } from '../shared/data.service';
     3import { DataService } from '../shared/services/data.service';
    44import { MatDialog } from '@angular/material/dialog';
    55import { FacilityDialogComponent } from '../dialogs/facility-dialog/facility-dialog.component';
  • Farmatiko/ClientApp/src/app/dashboard/dashboard.component.ts

    r993189e r1f4846d  
    33import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material/snack-bar';
    44import { IPharmacy, IMedicine, IPharmacyHead, IPharmacyHeadRequest } from '../shared/interfaces';
    5 import { DataService } from '../shared/data.service';
     5import { DataService } from '../shared/services/data.service';
    66import { PharmacyDialogComponent } from '../dialogs/pharmacy-dialog/pharmacy-dialog.component';
    77import { EditPharmacyDialogComponent } from '../dialogs/edit-pharmacy-dialog/edit-pharmacy-dialog.component';
    88import { MedicineDialogComponent } from '../dialogs/medicine-dialog/medicine-dialog.component';
    99import { ActivatedRoute, Router } from '@angular/router';
    10 import { AuthService } from '../shared/auth.service';
     10import { AuthService } from '../shared/services/auth.service';
    1111
    1212@Component({
     
    2727
    2828  ngOnInit(): void {
    29     this.head = this.authService.headValue;
     29    this.authService.getUser().subscribe((data : IPharmacyHead) => {
     30        this.head = data;
     31    });
    3032    this.dataService.getPharmacies()
    3133        .subscribe((pharmacy: IPharmacy[]) => {
  • Farmatiko/ClientApp/src/app/home/home.component.ts

    r993189e r1f4846d  
    11import { Component, OnInit } from '@angular/core';
    22import { IMedicine, IPharmacy } from '../shared/interfaces';
    3 import { DataService } from '../shared/data.service';
     3import { DataService } from '../shared/services/data.service';
    44import { MatDialog } from '@angular/material/dialog';
    55import { MedicineDialogComponent } from '../dialogs/medicine-dialog/medicine-dialog.component';
  • Farmatiko/ClientApp/src/app/korona/korona.component.ts

    r993189e r1f4846d  
    11import { Component, OnInit } from '@angular/core';
    2 import { DataService } from '../shared/data.service';
     2import { DataService } from '../shared/services/data.service';
    33import { IPandemic } from '../shared/interfaces';
    44
  • Farmatiko/ClientApp/src/app/login/login.component.html

    r993189e r1f4846d  
    66  <div class="example-container">
    77    <mat-form-field>
    8       <input matInput placeholder="Email" [(ngModel)]="this.email" formControlName="email">
     8      <input matInput placeholder="Email" [(ngModel)]="this.username" formControlName="email">
    99    </mat-form-field>
    1010
    1111    <mat-form-field>
    12       <input matInput type="password" placeholder="Password" [(ngModel)]="this.passwd" formControlName="password">   
     12      <input matInput type="password" placeholder="Password" [(ngModel)]="this.password" formControlName="password">   
    1313    </mat-form-field>
    1414
    1515    <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>
    1620  </div>
    1721</form>
  • Farmatiko/ClientApp/src/app/login/login.component.ts

    r993189e r1f4846d  
    1 import { Component, OnInit } from '@angular/core';
     1import { Component, OnInit, OnDestroy } from '@angular/core';
    22import { FormGroup, FormControl, Validators } from '@angular/forms';
    33import { Router, ActivatedRoute } from '@angular/router';
     4import { Subscription } from 'rxjs';
    45import { first } from 'rxjs/operators';
    5 import { AuthService } from '../shared/auth.service';
     6import { AuthService } from '../shared/services/auth.service';
     7import { finalize } from 'rxjs/operators';
    68
    79@Component({
     
    1012  styleUrls: ['./login.component.css']
    1113})
    12 export class LoginComponent implements OnInit {
     14export class LoginComponent implements OnInit, OnDestroy {
     15  busy = false;
    1316  loginForm: FormGroup;
    14   email: string;
    15   passwd: string;
    16   errorMessage: string;
     17  username = '';
     18  password = '';
     19  loginError = false;
     20  private subscription: Subscription;
    1721
    1822  constructor(private authService: AuthService,private router: Router, private route: ActivatedRoute) {
    1923    this.loginForm = new FormGroup({
    20       email: new FormControl('', [Validators.required, Validators.email]),
     24      username: new FormControl('', [Validators.required, Validators.email]),
    2125      password: new FormControl('', [Validators.required])
    2226    });
    2327  }
     28  ngOnDestroy(): void {
     29    this.subscription?.unsubscribe();
     30  }
    2431
    2532  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    });
    2643  }
    2744
    2845  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      );
    3862    this.loginForm.reset();
    3963  }
  • Farmatiko/ClientApp/src/app/shared/interfaces.ts

    r993189e r1f4846d  
    5555    Passwd: string;
    5656    Name: string;
     57    Role?: string;
    5758
    5859
  • Farmatiko/ClientApp/src/app/shared/services/data.service.ts

    r993189e r1f4846d  
    55import { map, catchError } from 'rxjs/operators';
    66
    7 import { IHealthFacilities, IHealthcareWorkers, IMedicine, IPandemic, IPharmacy, IPharmacyHead, IPharmacyHeadRequest } from './interfaces';
     7import { IHealthFacilities, IHealthcareWorkers, IMedicine, IPandemic, IPharmacy, IPharmacyHead, IPharmacyHeadRequest } from '../interfaces';
    88
    99@Injectable()
Note: See TracChangeset for help on using the changeset viewer.