To find more information :
Questions tagged [angular-router-guards]
289 questions
8
votes
3 answers
Angular 4 load data before initialize the application
I'm trying to load some data before my application starts. The reason why I need to do it, is because some of the menu has restricted access based on some user condition, for example, based on the user location.
So if the user doesn't have a…

celsomtrindade
- 4,501
- 18
- 61
- 116
7
votes
2 answers
Apply router guard to all children except one
Currently, in Angular, you can restrict access to all child routes by applying a router guard to one of the parents:
export const routes: Routes = [
{
path: 'my-account',
canActivate: [IsUserLoggedIn],
children: [{
path:…

A. Duff
- 4,097
- 7
- 38
- 69
7
votes
1 answer
Accessing component of the ActivatedRoute in the canActivate Guard
As far as I know the call signature of canActivate looks like this:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
}
I have a service that expects the name of a component and returns the necessary user role to access this…

Michael L.
- 273
- 1
- 3
- 17
7
votes
3 answers
Angular 2/4 prevent user to leave component if changes not saved
I have this interface that i'm using to prevent the user to leave page
export interface ComponentCanDeactivate {
canDeactivate: () => boolean;
}
@Injectable()
export class PendingChangesGuard implements CanDeactivate {
…

user9052661
- 161
- 1
- 5
6
votes
2 answers
TypeError: Cannot read property 'canDeactivate' of null
I'm trying to use the canDeactivate router guard. The component I'm passing in (ClaimsViewComponent) is null the first time the code runs. But on subsequent runs, the code runs as expected.
We're trying to figure out why the component is null on…

acclaris
- 61
- 1
- 3
6
votes
2 answers
Auth guard not working in angular 5
Here is my static login service
login(email: string, password: string) {
debugger;
const user = {
username: email,
password: password,
};
if (email === "admin" && password === "admin") {
localStorage.setItem("currentUser",…

Melvin
- 877
- 3
- 11
- 27
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
6
votes
2 answers
Angular. How to make CanDeactivate work correctly with the use of Location.back()
I'm implementing CanDeactivate functionality in one of my main components. To test it I've made it to always return false so the route must not change.
This is the CanDeactivate implementation in which the call to component.canDeactivate() returns a…

francadaval
- 2,451
- 3
- 26
- 36
5
votes
1 answer
rxjs switchMap not working in Angular Guard?
return this.auth.user.pipe(
switchMap((user: IUser) => {
return of(true);
})
);
My initial code was a bit more complex with some cases depending on the user data, but for testing purposes I've used the code above in a…

SebastianG
- 8,563
- 8
- 47
- 111
5
votes
0 answers
How to provide routes with guards for unit testing Angular 6 Component?
I have an Angular 6 component that has input fields. If any of the input fields fail validation if the user tries to navigate away, a guard will trigger on the canDeactivate function. The guard is generic because this logic needs to happen on more…

J-man
- 1,743
- 3
- 26
- 50
5
votes
1 answer
How safe are angular route guards?
I'm making an online store, and there's an administration part from where you can track orders and modify them.
It is protected by authentication, which is done on a node server, passwords is hashed into DB etc. but I'm worried about the route…

Simeon Nakov
- 978
- 3
- 14
- 32
5
votes
1 answer
When refreshing Angular app, it ignores current URL and goes back to root URl
If my Angular app, if I navigate (via explicitly typing in the URL or clicking a page link to it) to http://localhost:4200/#/sign-in the page loads fine and shows my sign in form. If I then click Refresh in my browser, I am taken back to the root…

CodyBugstein
- 21,984
- 61
- 207
- 363
5
votes
1 answer
Best way to show/hide a link in angular 5
I currently have a home page that routes to all the different workflows my company runs. We have about 15 different workflows and each workflow is guarded by a user role. If you don't have the correct user role in the database you wont see…

Samuel Thompson
- 2,429
- 4
- 24
- 34
5
votes
1 answer
Route params not available in guards
Why isn't id available in the below guard?
@Injectable()
export class ProjectDetailsGuard implements CanActivate {
constructor(private activatedRoute: ActivatedRoute) { }
canActivate() {
const id =…

Alex Lomia
- 6,705
- 12
- 53
- 87
4
votes
1 answer
Angular & passing data between components: use cases for Resolve guard vs Service injection
In Angular there are multiple ways to pass data to a Component, in particular one can use a Resolve guard to load the data even before the Component is created so that it is always available or one can inject a Service in the constructor of the…

technicated
- 169
- 1
- 9