Angular 当守卫 return Observable 时路由器不工作

Angular router doesn't work when guards return an Observable

我想在尝试访问受保护路由时重定向未登录的用户,但重定向不起作用,因为检查用户是否登录的函数 returns 一个 Observable。

这是我在 PrivateGuard 中的代码:

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { AmplifyService } from 'aws-amplify-angular';

@Injectable({
  providedIn: 'root'
})
export class PrivateGuard implements CanActivate {

  constructor (private _router: Router,
               private _amplifyService: AmplifyService) {}

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return this._amplifyService.authStateChange$.pipe(map(authState => {
        console.log(authState);
        if (authState.state === 'signedIn')  {
          return true;
        }else{
          this._router.navigate(['/login']); // Why Never redirect?
          return false;
        }
      })
    );
  }
}

app.routes.ts:

import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './components/public/login/login.component';
import { HomeComponent } from './components/private/home/home.component';

const APP_ROUTES: Routes = [
  { path: 'login', component: LoginComponent},
  { path: 'home', component: HomeComponent, canActivate: [PrivateGuard] },
  { path: '**', pathMatch: 'full', redirectTo: 'home' }
];

export const APP_ROUTING = RouterModule.forRoot(APP_ROUTES);

如果我在登录时在 else 语句中写 console.log,这个 console.log 会出现在控制台中,但不会重定向到 homeComponent

Console when I try navigate to a private route

The route in url dissapears, instead of localhost:4200/login

感谢您的任何回复。

已解决

最后我用promise代替了observable。 代码如下:

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { AmplifyService } from 'aws-amplify-angular';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class PrivateGuard implements CanActivate {

  constructor (private _router: Router,
               private _amplifyService: AmplifyService) {}

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return this._amplifyService.auth().currentAuthenticatedUser()
      .then(() => {
        return true;
      })
      .catch(() => {
        this._router.navigate(['/login']);
        return false;
      });
  }
}

谢谢大家的回答

您的 observable 可能永远不会完成,它仍在等待 authStateChange$ 发出更多值。

你可以通过使用 take 运算符来实现你想要的(它会在一个值发出后取消订阅:

return this._amplifyService.authStateChange$.pipe(
   take(1), // completes the observable on first emited value
   map(authState => /* */)
);