0

Need to delay the route canActivate method call until my Authentication service return user role for the user id.

Our angular application doesn't have any login page, it uses user id from the server login. When user access the URL, authentication service get called in app.compoment.ts but before the service return any result, canActivate method get called, and doesn't have the user role at that time. So blocks all user access.

I have tried using async and wait with https call to get the user data, but that also not worked. Can anyone help here?

Vishal
  • 127
  • 1
  • 2
  • 16

1 Answers1

0

Here, pipe() of rxJS is useful, rather subscribe first use pipe().

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>  {
    return this​.​loginService​.​isLoggedIn​().​pipe​(
        map(e => {
           if (e) {
               return true;
             } else {

             }
         }),
          catchError((err) => {
             this​.​router​.​navigate​([​'/login'​]);
             return of(false);
           })
      );
  }   
Developer
  • 3,309
  • 2
  • 19
  • 23
  • Pipe doesn't resolve the issue, I have used the pipe as pipe are mandatory in angular 7 API calls. To resolve the issue I have created the login page where user will provide the user id and then application will route to SSO login. – Vishal Jan 22 '20 at 13:29