Angular2:使用 AuthGuard 路由重定向
Angular2: Route redirect with AuthGuard
我有一个 Angular2 应用程序,如果用户未登录,它应该阻止到某个页面的路由。这是我的 app.routes 文件
export const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'login', component: LoginComponent },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
{ path: '**', component: LoginComponent },
];
和我的 AuthGuard 文件
@Injectable()
export class AuthGuard implements CanActivate {
response: ResponseInterface;
constructor(private router: Router, private localStorage: LocalStorageService,
private services: MyService) { }
canActivate() {
let token = String(this.localStorage.get('token'));
if (token != null) {
this.services.keepAlive(token).subscribe(
response => this.response = response,
error => alert(error),
() => {
if (this.response.status == 'OK') {
console.log("response OK");
return true;
} else {
console.log("response KO");
this.router.navigate(['/login']);
return false;
}
}
)
} else {
this.router.navigate(['/login']);
return false;
}
现在,如果我尝试导航到 http://localhost:4200/#/home path and I already have a token stored into localStorage, nothing happen: home page is not showed and path on navigation bar become http://localhost:4200/#/。
怎么了?
canActive
方法应该 return Observable<boolean>
、Promise<boolean>
或 boolean
。
您正在订阅 this.services.keepAlive
Observable,并且 returning boolean
值到订阅回调而不是 returning 到 canActivate
方法。更改您的代码如下:
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
let token = String(this.localStorage.get('token'));
if (token != null) {
return this.services.keepAlive(token)
.map(response => {
if (response.status == 'OK') {
console.log("response OK");
return true;
} else {
console.log("response KO");
this.router.navigate(['login']);
return false;
}
});
} else {
this.router.navigate(['login']);
return false;
}
}
这样,布尔类型 (Observable<boolean>
) 的 Observable 将 returned 到 canActive
方法,并且路由解析应该按预期工作。
我有一个 Angular2 应用程序,如果用户未登录,它应该阻止到某个页面的路由。这是我的 app.routes 文件
export const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'login', component: LoginComponent },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
{ path: '**', component: LoginComponent },
];
和我的 AuthGuard 文件
@Injectable()
export class AuthGuard implements CanActivate {
response: ResponseInterface;
constructor(private router: Router, private localStorage: LocalStorageService,
private services: MyService) { }
canActivate() {
let token = String(this.localStorage.get('token'));
if (token != null) {
this.services.keepAlive(token).subscribe(
response => this.response = response,
error => alert(error),
() => {
if (this.response.status == 'OK') {
console.log("response OK");
return true;
} else {
console.log("response KO");
this.router.navigate(['/login']);
return false;
}
}
)
} else {
this.router.navigate(['/login']);
return false;
}
现在,如果我尝试导航到 http://localhost:4200/#/home path and I already have a token stored into localStorage, nothing happen: home page is not showed and path on navigation bar become http://localhost:4200/#/。 怎么了?
canActive
方法应该 return Observable<boolean>
、Promise<boolean>
或 boolean
。
您正在订阅 this.services.keepAlive
Observable,并且 returning boolean
值到订阅回调而不是 returning 到 canActivate
方法。更改您的代码如下:
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
let token = String(this.localStorage.get('token'));
if (token != null) {
return this.services.keepAlive(token)
.map(response => {
if (response.status == 'OK') {
console.log("response OK");
return true;
} else {
console.log("response KO");
this.router.navigate(['login']);
return false;
}
});
} else {
this.router.navigate(['login']);
return false;
}
}
这样,布尔类型 (Observable<boolean>
) 的 Observable 将 returned 到 canActive
方法,并且路由解析应该按预期工作。