简体   繁体   中英

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 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);
        });
      });
  }
}

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);
    });
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM