我想重定向未登录的用户,当他们尝试访问受保护的路由但重定向不起作用,因为检查用户是否登录的函数返回一个 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
url 中的路由消失,而不是 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;
});
}
}
谢谢大家的答案