File

src/app/guards/limits.guard.ts

Index

Methods

Constructor

constructor(app: AppState, errorStore: ErrorStore, router: Router)
Parameters :
Name Type Optional
app AppState no
errorStore ErrorStore no
router Router no

Methods

canActivate
canActivate()
Returns : any
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { filter, map } from 'rxjs/operators';
import { AppState } from '../stores/app-state.store';
import { ErrorStore } from '../stores/error.store';

@Injectable()
export class LimitsGuard implements CanActivate {
  constructor(private app: AppState, private errorStore: ErrorStore, private router: Router) {}

  canActivate() {
    return this.app.state$.pipe(
      filter(state => state !== null && state !== undefined),
      map(state => {
        if (!state.limitsResult.isAllowed) {
          console.log('payment failed limits');
          this.errorStore.addError('Limit Exceeded', state.limitsResult);
          this.errorStore.displayErrors();
        }
        if (state.limitsResult.isAllowed && state.limitsResult.requiresMfa && !this.app.hasPassedMfa) {
          console.log('limits requires mfa');
          this.router.navigate(['/authenticate-method']);
          return false;
        }
        const message = state.limitsResult.isAllowed ? 'passed limits guard' : 'failed limits guard';
        console.log(message);
        return state.limitsResult.isAllowed;
      })
    );
  }
}

results matching ""

    No results matching ""