-1

I was trying to use route guard in my app that should let you go to a component only if the server sends you some parameters.

This is the list of the parameters that the server can send me (not all of them are sent each time)

  AGREEMENTS_VIEW 
  PROSPECTS_VIEW
  AGREEMENTS_INSERT_UPDATE
  PRODUCTS_INSERT_UPDATE
  PROSPECTS_INSERT_UPDATE
  DOCUMENTS_VIEW
  DOCUMENTS_INSERT_UPDATE

My route guard:

@Injectable()
export class UserRouteAccessService implements CanActivate {
    userActions=[];
    constructor(private router: Router, private securityService:CralSecurityService) {
     }
     securityActions(){debugger;
        this.securityService.securityActions().subscribe(
           (res: Array<actions>) => {
             this.userActions = res;
             console.log(res);

           });
       }


    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        this.securityActions();

        if (this.userActions = actions ) {
            return true;
        } else {
            return false;
        }
    }

Now how can I tell in the if that its true when some of the paramenters are sent or not? is userActions=[] correct? For example it should go to "true" when AGREEMENTS_VIEW PROSPECTS_VIEW are sent.

Also, is this ok?

export class actions{
          AGREEMENTS_VIEW :string;
          PROSPECTS_VIEW :string;
          AGREEMENTS_INSERT_UPDATE :string;
          PRODUCTS_INSERT_UPDATE :string;
          PROSPECTS_INSERT_UPDATE :string;
          DOCUMENTS_VIEW :string;
          DOCUMENTS_INSERT_UPDATE :string;
}

Http post:

securityActions(): <Array<any>> {
    return this.http.post<Array<any>>(
        `${this.ENDPOINT}/security-actions`,
        null,
    );
}

1 Answers1

1

The signature of the interface is as follows :

interface CanActivate {
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean
}

This means you can return an HTTP call that returns a boolean.

So you can write this :

authorized = [
  'AGREEMENTS_VIEW',
  'PROSPECTS_VIEW',
  'AGREEMENTS_INSERT_UPDATE',
  'PRODUCTS_INSERT_UPDATE',
  'PROSPECTS_INSERT_UPDATE',
  'DOCUMENTS_VIEW',
  'DOCUMENTS_INSERT_UPDATE',
];

securityActions(): Observable<boolean> {
  return this.securityService.securityActions().pipe(
    map(response => this.authorized.includes(response))
  );
}


canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.securityActions();
}
  • thank you for your help, "includes" says that the property doesnt exist in type 'string[]' and also in the canActivate method, return tell me that void is not assignable to type boolean :) – Francesco Giberti Aug 13 '18 at 07:31
  • 1
    then use `.indexOf(response) !== -1`, and I edited my answer –  Aug 13 '18 at 07:36
  • still nothing "a function which declared type isnt "void" or "any" must return a value, this is on Observable. Ive updated my post with the service where I have the HttpPost – Francesco Giberti Aug 13 '18 at 07:44
  • 1
    My bad again, I was missing a `return` statement in securityActions ! –  Aug 13 '18 at 07:46
  • thank you! last error, ( i hope :P ) on "response" in the securtyActions method it tells me "the argument boolean is not assignable to parameter of type "string". Ive tried also with ".indexOf(response) !== -1" but its the same thing ^^ – Francesco Giberti Aug 13 '18 at 07:49
  • If your service returns a boolean already, you can remove the `.pipe( map(response => this.authorized.includes(response)) )` and just return `this.securityService.securityActions()` –  Aug 13 '18 at 07:57
  • my bad sorry i was just messing with my service, it actually returns an Array of strings – Francesco Giberti Aug 13 '18 at 08:00