Angular 2 canActivate with a promise hitting a remote service

Ramee Mossa picture Ramee Mossa · Apr 18, 2017 · Viewed 14.2k times · Source

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 loaded, to avoid a screen flashing since were checking auth status on a remote server.

I am implementing the canActivate interface, but its not working as expected. It hits the auth service, and returns the user, which according to this test code should redirect the user, but instead I am seeing the user in the console but no redirect.

Here is the code

import { Injectable }               from '@angular/core';
import { Router, CanActivate }      from '@angular/router';

import { Observable }               from 'rxjs/Observable';

import { User }                     from './../../models/user.model';
import { AuthService }              from './auth.service';

@Injectable()
export class HomeAuthGuard implements CanActivate {
  constructor(
    private router: Router,
    private authService: AuthService
  ) {}

  canActivate(): Promise<boolean> {
    return new Promise((resolve) => {
      this.authService.getStatus()
        .then(function (user: User) {
          console.log('home auth', user)
          this.router.navigate(['/dashboard']);
          resolve(false);
        })
        .catch(function () {
          resolve(true);
        });
      });
  }
}

Answer

Julia Passynkova picture Julia Passynkova · Apr 18, 2017

This is happen because of 'this' binding. Mostly likely your code goes to catch block after console.log due to fact that 'this' is not your component class instance. Use "fat arrow" to solve this issue. like:

return new Promise((resolve) => {
  this.authService.getStatus()
    .then((user: User) => {
      console.log('home auth', user)
      this.router.navigate(['/dashboard']);
      resolve(false);
    })
    .catch(err => {
      resolve(true);
    });
  })