When a user wants to get access to a specific route on an Angular application, Guards comes in action verifying accesses and validating if that user is authorized to get access to that route. Guards are services executed before a route is loaded implementing functions such as canActivate in order to validate access
Questions tagged [angular2-guards]
44 questions
40
votes
3 answers
Angular 2 get current route in guard
I have an AccessGuard class in my project which its work is to determine if user can access to the route or not. I used the router.url to get the current route but the url returns the route before navigation to the new route like I'm in the users…

Hossein Ahmadi
- 839
- 2
- 11
- 24
33
votes
3 answers
How to unit-test canActivate guard method of angular2 using Jasmine?
Sorry for asking this type of question. But I'm not able to find any blog or youtube tutorials on writing the canActivate guard file testing. Nor in the official documentation there is anything mentioned.
any help will be much appreciated.

Sumit Khanduri
- 3,619
- 7
- 30
- 40
18
votes
2 answers
Angular 2 How to Apply Multiple Guards with Or-Relationship on the Same Path
My application allows access to contents based on user roles. I wrote a Router Guard for each role. Some contents allow access for role1 or role2 or role3. How should I write that canActivate declaration in the feature-routing.module.ts file? As I…

Nicole Naumann
- 1,018
- 2
- 10
- 23
14
votes
2 answers
angular2: CanDeactivate guard
I've created a CanDeactivate guard which returns an observable and it's applied to a component which is loaded in a inner nested router-outlet. Should this guard be called whenever one tries to navigate to another url? I'm asking this because this…

Luis Abreu
- 4,008
- 9
- 34
- 63
14
votes
2 answers
Using a base guard for all routes in Angular 2 application
Is there a way to set a "base" canActivate when configuring routes in Angular2? So that all routes are covered by the same base check, and then each route can have a more granular check.
I have an AppModule with routing like this:
@NgModule({
…

Joel
- 8,502
- 11
- 66
- 115
13
votes
4 answers
Trying to understand the differences between CanActivate and CanActivateChild
So, I'm trying to protect the access to several routes by using guards. I'm using the following routes to do so :
const adminRoutes : Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [ AuthGuardService ],
…

YounesM
- 2,279
- 15
- 28
9
votes
1 answer
Using BehaviorSubject in auth-guard's canActivate
What I want to achieve: I want to share authentication state across my application using BehaviorSubject. I use the authentication state e.g. inside an auth-guard to prevent the user from visiting login/register pages when the user already is…

DarkLeafyGreen
- 69,338
- 131
- 383
- 601
9
votes
2 answers
Angular2: Global Guard (user has to be logged in always)
I'm building an application where there's no access at all for unauthenticated users.
I wrote a LoggedInGuard, but now I have to add canActivate: [LoggedInGuard] to every route inside my router configuration (except the LoginComponent).
Is there a…

Benjamin M
- 23,599
- 32
- 121
- 201
8
votes
1 answer
Angular2 route guard returning Observable, how to handle errors
I have a route guard like below
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authenticationSvc: AuthenticationService) { }
canActivate(): Observable {
return…

Josh
- 1,648
- 8
- 27
- 58
8
votes
3 answers
Getting "No provider error" when using CanActivate guard
This is the guard:
// my.guard.ts
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
Injectable()
export class MyGuard implements CanActivate {
canActivate(next:…

dopatraman
- 13,416
- 29
- 90
- 154
7
votes
1 answer
Can "canActivate" received data?
I have these lines in my router:
..
canActivate: [MyGuard],
path: "search",
component: SearchComponent,
data: {
accessRoles: [roleAdmin, roleUser]
}
..
I want to limit access to SearchComponent by canActivate of…

Egor
- 2,122
- 2
- 21
- 23
6
votes
1 answer
How to make Angular2 Service singleton?
I'm trying to implement an auth guard in my app. ie; Only authenticated users can access certain routes of my app. I'm following the tut given here.
Once the user is logged in I change a boolean value in my AuthService to true to indicate that the…

Rahmathullah M
- 2,676
- 2
- 27
- 44
5
votes
0 answers
Can't resolve all parameters for router CanActivate
Im trying to implement authorization guard https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-child-guard
but I get a DI error, I really dont know why I inject my UserService in my module.
My RouteGuard is declared in a Global Module…

user3369579
- 486
- 3
- 7
- 22
5
votes
0 answers
Angular2 CanDeactivate with async observable
I am looking into Guards and trying to prevent the navigation from happening with CanDeactivate. I want to show a simple modal with Save/Cancel, save = navigate and well, cancel = cancel.
I've got CanDeactivate working, but it seems that its not…

ZeroCool
- 609
- 7
- 21
4
votes
2 answers
angular2 guard not working on page refresh
Before every request I want to be sure that there is a user profile available. I use a canActivateChild guard to do this.
According to the documentation of angular2 it is possible to return an observable:…

Rick Lenes
- 97
- 4