File

C:/GoDev/src/TCSTK-Angular/projects/tibco-tcstk/tc-liveapps-lib/src/lib/guards/case.guard.ts

Index

Methods

Constructor

constructor(liveapps: LiveAppsService, appDefinitionService: TcAppDefinitionService, router: Router)
Parameters :
Name Type Optional
liveapps LiveAppsService No
appDefinitionService TcAppDefinitionService No
router Router No

Methods

canActivate
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
Parameters :
Name Type Optional
route ActivatedRouteSnapshot No
state RouterStateSnapshot No
Returns : Observable<boolean>
import {Inject, Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {LiveAppsService} from '../services/live-apps.service';
import {HttpClient, HttpHandler} from '@angular/common/http';
import {catchError, flatMap, map, mergeMap} from 'rxjs/operators';
import {Observable, of, throwError} from 'rxjs';
import {ClaimsResolver} from '../resolvers/claims.resolver';
import {Claim} from '@tibco-tcstk/tc-core-lib';
import {TcAppDefinitionService} from '../services/tc-app-definition.service';

@Injectable()
export class CaseGuard implements CanActivate {

  constructor(private liveapps: LiveAppsService, private appDefinitionService: TcAppDefinitionService, private router: Router) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    // get app config
    const caseRef = route.url[route.url.length - 1].path;

    const claimsResolver = new ClaimsResolver(this.appDefinitionService);

    const decision: Observable<boolean> = claimsResolver.resolve()
      .pipe(
        // use flatMap so we can pass the config into the getCaseByRef call)
        flatMap(
          claims => {
            return this.liveapps.getCaseByRef(claims.primaryProductionSandbox.id, caseRef)
              .pipe(
                map(caseinfo => {
                  if (caseinfo) {
                    return true;
                  } else {
                    // TODO: Externalize redirect URL
                    this.router.navigate(['/starterApp/home'], {});
                    return false;
                  }
                }),
                catchError(error => {
                  // if the case is not found (someone tried to manually route to an invalid caseref) then redirect back to home
                  console.error('Unable to load case ' + caseRef + ' are you sure that is a valid case reference?');
                  // TODO: Externalize redirect URL
                  this.router.navigate(['/starterApp/home'], {});
                  return throwError(error);
                })
              );
          }
        )
      );

    return decision;
  }

}

result-matching ""

    No results matching ""