Angular Route Guard CanActivate stops routing

Developer

I am trying to check weather the user has already selected a company to login. If they select then they see those company employees. Otherwise, we will redirect to login page.

I have used Angular Route Guard to do this. But its not continuing the route and stops there, though I return true. How to continue route

Route config

const appRoutes: Routes = [
    { path: "employees", component: EmployeeListComponent, canActivate: [AuthGuard] },
    { path: "", redirectTo: "/employees", pathMatch: "full" },
    { path: "login", component: LoginComponent },
    { path: "**", component: PageNotFoundComponent }
];

Auth Guard

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AppService } from '../../app/app.service';

@Injectable()
export class AuthGuard implements CanActivate {
    constructor(private appService: AppService, private router: Router) {

    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        let user: IUserViewData;
        var _this = this;

        this.appService.getUserViewData()
            .subscribe(
                (response: IUserViewData) => {
                    user = response;
                    if (user.LoggedInCompany != null) {

                        return true;
                    } else {
                        _this.router.navigate(['/login']);
                        return false;
                    }
            },
            (error: any) => { });

        return false;
    }
}
Murali Murugesan

Change subscribe to map that returns Observable<boolean>

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    var _this = this;
    return this.appService.getUserViewData().map(user => {
        if (user.LoggedInCompany != null) {
            return true;
        } else {
            _this.router.navigate(['/login']);
            return false;
        }
    }); 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Read cookie in CanActivate route guard in Angular

How to redirect from parent route to child route in Angular canActivate guard?

Angular 2 Route Guard CanActivate based on Firebase User Role

Get target route URL in CanActivate guard Lazy load modules Angular

Angular Router route guard CanActivate always returns false

Angular canActivate Guard with API

Angular CanActivate Guard

Angular5 canactivate guard :Types of property 'canActivate' are incompatible

Angular CanActivate guard - createUrlTree relative navigation

Angular2 CanActivate guard not working

Angular(5) - lazy loading and canActivate guard - StaticInjectorError

Using defaultIfEmpty() still not emitting in Angular canActivate guard

Angular 2 router canActivate Auth Guard

Authorization Issue with Angular Auth Guard canActivate method

Why does route guard canLoad not fire, but canActivate does

Can't get route params for a lazy module within a CanActivate guard

Angular 2 - Routing - CanActivate work with Observable

Angular routing canActivate AuthGuard transmit query params

Angular 2 routing: Route guard validation fails when accessing the protected url directly

Angular 2 Route Guard / Auth Guard Security

Angular: Run canActivate on each route change

How to check asynchronously if user is authenticated in router canActivate guard in angular 2?

Return condition always is false into subscribe Angular CanActivate Guard

Return observable inside observable in canactivate guard of Angular 4

Angular guard canActivate method dont work with Observable<boolean>

Angular How to implement CanActivate guard with HTTP Get call

Angular 9 - Guard: canActivate wait until specific condition is met

error in canActivate guard method

AngularFireAuth redirect issue with angular routing guard