Questions tagged canactivate

Explore the latest questions and answers asked by our top developers.

Delayed CanActivate in Angular Router blocks rendering

I have a mat-table with 3000 rows (array with mock data) behind a CanActivate guard. Based on CanActivate docs, it can return both boolean or Observable<boolean>. If it returns true, mat-table component loads instantly. Now, I want to use the CanActivate to see if user has logged in. Here is an observable that will wait […]

Role based permissions system

I’ve never done this before and couldn’t find a good reference online so I’m asking. I’m building angular webapp and want to make a role -> permission system. I’ve made roleGuard with CanActivate, but I cannot find a good way to make logic for checking permissions for it. Storing permissions in token or localstorage and […]

By reznov46
Categorized as angular, angular8, canactivate Tagged , ,

CanActivate guard leads to multiple http calls

I observed strange behavior with my CanActivate guard (Angular 10.2.5): export class VersionGuardService implements CanActivate { constructor(private router: Router, private http: HttpClient){ } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { console.log("In the Guard"); return this.http.request<any>(‘get’,``).pipe( tap(resp => console.log("Response: ", resp)), map(resp => false) ); //return of(false); } } Routes in app-routing.module.ts looks like this: const routes: […]

By dzk008
Categorized as angular, canactivate Tagged ,

Angular Http call in CanActivate getting cancelled

I’m trying to create an guard to verify the token via http call, As CanActivate accepts return types as Observable . Though the http request is completed from the backend still in front it shows as cancelled, below is my code canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { if (this.isTokenPresent(route)) { return this.validateToken(route).pipe(map((e: any) […]

By Azeem
Categorized as angular, canactivate Tagged ,

Canactivate is returning false always coming from API condition check angular

I have used canActivate to protect a page but it always returning false because of that I can not access the protected router. I tried many ways, but was not successful to fix the issue. I am new to angular and i am stuck in validating the condition if (data.hasPermission == true). can any one […]

By sarasm
Categorized as angular, canactivate Tagged ,

Angular Routing to Child path based on role

After successful login, I redirect to following path this.router.navigate([‘/main/’]) Based on role received from login. Is it possible to redirect to diff modules. Example: If Role is ‘Admin’ if I redirect to [‘/main’] based on role it will navigate to admin module . Using guards Project structure: App module —Login Module —Main Module —Admin Module […]

Redirect to a specific page when using CanActivate

In my application when a user try to see the page "home" he is redirected to the login page (if he’s not already logged in). If the user is already logged in, or after he logged in, I want to redirect him to the page "home" or whatever page he was trying to see before […]

By Matt
Categorized as angular, canactivate Tagged ,

Problem of ending return statement on canActivate

I have a problem when I use CanActivate method: error ts2366 "Function lacks ending return statement and return type does not include undefined" appears. Following my code: import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from ‘@angular/router’; import { Observable } from ‘rxjs’; import { AuthService } from ‘./auth.service’; import { Injectable } from ‘@angular/core’; […]

By thomas rocheteau
Categorized as angular, canactivate, typescript Tagged , ,

How to process data from api response then return a boolean value synchronously in angular?

I have a simple function in my service: /** * Funtion to check if the user logged in has admin rights */ isAdmin(user_id: string) { var adminTemp: boolean; this.httpClient .get(`${this.urk}/${user_id}`) .subscribe((data: any) => { if (data.includes(‘Admin’)) { adminTemp = true; } else { adminTemp = false; } }); return adminTemp; } This function is called […]

make delay in canActivate till IP address data is fetched

I’m making a guard for my angular application where all users should be redirected to maintenance page except for certain IPs. I’m getting the client IP address using third party APIs like The PROBLEM I’m facing is that the canActivate guard gets executed before the IP API sends in the response, thus always returning […]

1 2

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us