Questions tagged [canactivate]

145 questions
39
votes
5 answers

Angular's "CanActivate" interface is deprecated. How to replace it?

My Angular app includes a simple AuthGuard as shown below and there has never been a problem with it. Recently, I upgraded my Angular version from 15.1.4 to 15.2.0 and since then, my IDE indicates that both CanActivate and CanActivateChild are…
kellermat
  • 2,859
  • 2
  • 4
  • 21
19
votes
4 answers

How can you use Angular's canActivate to negate the result of a guard?

From the Angular documentation on canActivate, it seems you can only use canActivate guards to allow proceeding to a route if the canActivate function ultimately returns true. Is there some way to say, "only proceed to this route if the canActivate…
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
10
votes
2 answers

Error: Uncaught (in promise): TypeError: guard is not a function

I am writing an Authguard in Angular 4 to prevent accessing routes without login. However, I am getting this error. Below is the code from Authgaurd and Routing in App module. Please help resolve the issue. //Authgaurd Code import {…
user8300647
  • 335
  • 2
  • 4
  • 10
9
votes
5 answers

Angular refresh the application if the URL is written manually

I'm using Angular 6 and I have an issue with the change of routes. If I navigate through the application using the routerLink or the navigate() method, it works correctly, because it only load the new module (if necessary). But for example if I am…
panagulis72
  • 2,129
  • 6
  • 31
  • 71
8
votes
2 answers

Angular: Run canActivate on each route change

I got stuck recently with Angular route guards. CanActive runs only once when the page is loaded and does not run on route change within the guarded route. I think this was changed because it used to run on each change. From what I read in forums, I…
Filip Matys
  • 185
  • 3
  • 12
7
votes
2 answers

How to redirect unknown routes to home route in Angular program?

I have a routes.ts file like below: import { AuthGuardService as AuthGuard } from '../services/auth-guard.service'; export const routes:Routes = [ {path : '' , redirectTo : '/home' , pathMatch : 'full'}, {path: 'home' , component :…
Hasani
  • 3,543
  • 14
  • 65
  • 125
7
votes
2 answers

Pass Data from Guard to Component

My child route components are querying some data from an API. Currently I'm checking in the components if the id supplied as a route param is valid by checking the data the API returned. I consider this as bad style and want to place the check into…
Christian Klemm
  • 1,455
  • 5
  • 28
  • 49
7
votes
1 answer

Angular 2 canActivate with a promise hitting a remote service

Firstly, I am not sure that this is the best way to handle this problem, but what I am looking for is a route guard on "/" which checks if the user is logged in, if so to redirect them to "/dashboard". I would like to do this before the route is…
Ramee Mossa
  • 105
  • 1
  • 1
  • 4
6
votes
2 answers

Auth Guard canActivate not working for some reason

When I add AuthGuard service with canActivate on routes, the app crashes when I try to go to /profile and it redirect me to localhost:4200, not even /home and gives this error: ERROR Error: "[object Object]" My code : app.routing.ts import {…
Ed i
  • 61
  • 1
  • 1
  • 2
6
votes
2 answers

Why is ngOnInit() being called before canActivate()?

I'm using route guards, specifically the canActivate() method, but Angular is calling ngOnInit() of my root AppComponent before canActivate is called. I have to wait on some data in canActivate before the AppComponent can render it in the…
Kingamere
  • 9,496
  • 23
  • 71
  • 110
6
votes
1 answer

Can't resolve all parameters for AuthGuard

I have this AuthGuard: export class AuthGuard implements CanActivate { constructor (private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean { if…
Laiso
  • 2,630
  • 3
  • 16
  • 32
5
votes
0 answers

app component loads even before angular guard checks for data from API

i have an angular app whose app.component.html looks as below:
and 'app-nav-bar' is a component which gets loaded on launch of this angular app, whose…
Lisa
  • 655
  • 3
  • 10
  • 34
4
votes
1 answer

How to execute in first a function and in second canActivate()

I have this AuthGuard. In this code I want to create a condition. If my resetpasss is not null I want to navigate in here ResetPassIdComponent else I want to navigate in LoginFirstComponent. For this I create AuthGuard. export class AuthGuard…
web site
  • 99
  • 8
4
votes
1 answer

How to get a service response before loading a component Angular 4?

I have a service that makes request to an api and according to its response I should decide that a component must be loaded or not. but because of a time spent to receive the response, component loads regardless of response status, and after some…
Hamid Hosseini
  • 217
  • 4
  • 8
4
votes
1 answer

canActivate guard works only after refresh

I have canActivate guard which is on all routes (on the parent route). It works correctly when I go to any link first time, but then, when I'm changing routes it does not work. Guard is about logged in users (If api returns that I am logged in I…
Dato Gogshelidze
  • 666
  • 7
  • 20
1
2 3
9 10