[英]can activate guard and using observables with in them angular 5
我正在使用实现canActivate的路由防护
我在代码中放置了一堆控制台日志,以了解失败的地方。
如果我导航到受保护的路线,将会发生什么。 导航失败,因为防护未能返回值。 我的http地图尚未完成。
我目前在会话存储中保存了一个JWT令牌,但在本地没有
这些是我运行警卫队时获得的控制台日志
running the local check
running the session check
got session token authorizing it
然后http映射回来,然后代码中断。
完整的代码如下。 帮助将不胜感激!
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router
} from '@angular/router';
import {Injectable} from '@angular/core';
import {UserAuthorizationService} from "../userauthorizationservice/userauthorizationservice";
@Injectable()
export class ClientSuitsAdminSuperUserGuard implements CanActivate{
constructor(private userservice: UserAuthorizationService, private router: Router){}
user ={
id: null,
isclient: false,
issuitsviewer: false,
issuitsadministrator: false,
issuitssuperuser: false,
isvenueuser: false
};
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | boolean {
let token = this.userservice.checklocalfortoken();
console.log('running the local check');
if(token == null){
console.log('running the session check');
token = this.userservice.checksessionfortoken();
if(token == null){
console.log('no session token nav to sign in return false');
this.router.navigate(['/signin']);
return false;
}
console.log('got session token authorizing it');
this.userservice.returnauthorizetoken(token)
.map(
(req: any)=>{
this.user = req;
console.log('this is the user object from the session auth');
console.log(this.user);
if(this.user.isclient || this.user.issuitsadministrator || this.user.issuitssuperuser){
console.log('the user has permissions from the session');
return true;
}else{
console.log('the user does not have permissions from the session');
this.router.navigate(['/401']);
return false;
}
},
error => {
console.log('error with the session authorization');
this.router.navigate(['/signin']);
return false;
}
);
}else{
console.log('doing the local check');
this.userservice.returnauthorizetoken(token)
.map(
(req: any)=>{
this.user = req;
console.log('got the user object from the local');
console.log(this.user);
if(this.user.isclient || this.user.issuitsadministrator || this.user.issuitssuperuser){
console.log('user has permissions from the local');
return true;
}else{
console.log('user does not have permissions from the local');
this.router.navigate(['/401']);
return false;
}
},
error => {
console.log('error from the local authorization');
this.router.navigate(['/signin']);
return false;
}
);
}
}
}
您需要从canActivate
返回一个可观察的canActivate
,该对象发出一个布尔值。 在幕后,Angular订阅了返回的可观察对象,然后根据发出的值正确处理了路由。
如果你回电话给你的你的代码应工作userservice
。
@Injectable()
export class ClientSuitsAdminSuperUserGuard implements CanActivate{
constructor(private userservice: UserAuthorizationService, private router: Router){}
user ={
id: null,
isclient: false,
issuitsviewer: false,
issuitsadministrator: false,
issuitssuperuser: false,
isvenueuser: false
};
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | boolean {
let token = this.userservice.checklocalfortoken();
if (token == null) {
token = this.userservice.checksessionfortoken();
if(token == null){
this.router.navigate(['/signin']);
return false;
}
return this.userservice.returnauthorizetoken(token)
.map(
(req: any)=>{
this.user = req;
console.log('this is the user object from the session auth');
console.log(this.user);
if(this.user.isclient || this.user.issuitsadministrator || this.user.issuitssuperuser){
console.log('the user has permissions from the session');
return true;
}else{
console.log('the user does not have permissions from the session');
this.router.navigate(['/401']);
return false;
}
},
error => {
console.log('error with the session authorization');
this.router.navigate(['/signin']);
return false;
}
);
} else {
console.log('doing the local check');
return this.userservice.returnauthorizetoken(token)
.map(
(req: any)=>{
this.user = req;
console.log('got the user object from the local');
console.log(this.user);
if(this.user.isclient || this.user.issuitsadministrator || this.user.issuitssuperuser){
console.log('user has permissions from the local');
return true;
}else{
console.log('user does not have permissions from the local');
this.router.navigate(['/401']);
return false;
}
},
error => {
console.log('error from the local authorization');
this.router.navigate(['/signin']);
return false;
}
);
}
}
}
像这样使用它:
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | boolean {
let token = this.userservice.checklocalfortoken();
console.log('running the local check');
if(token == null){
console.log('running the session check');
token = this.userservice.checksessionfortoken();
if(token == null){
console.log('no session token nav to sign in return false');
this.router.navigate(['/signin']);
return false;
}
console.log('got session token authorizing it');
//subscribe method_first and return boolean variable from here
}else{
//subscribe method_second and return boolean variable from here
}
}
method_first(token): observable<any>{
this.userservice.returnauthorizetoken(token)
.map(
(req: any)=>{
this.user = req;
console.log('this is the user object from the session auth');
console.log(this.user);
if(this.user.isclient || this.user.issuitsadministrator || this.user.issuitssuperuser){
console.log('the user has permissions from the session');
return true;
}else{
console.log('the user does not have permissions from the session');
this.router.navigate(['/401']);
return false;
}
},
error => {
console.log('error with the session authorization');
this.router.navigate(['/signin']);
return false;
}
);
}
method_second(token): observable<any>{
console.log('doing the local check');
this.userservice.returnauthorizetoken(token)
.map(
(req: any)=>{
this.user = req;
console.log('got the user object from the local');
console.log(this.user);
if(this.user.isclient || this.user.issuitsadministrator ||
this.user.issuitssuperuser){
console.log('user has permissions from the local');
return true;
}else{
console.log('user does not have permissions from the local');
this.router.navigate(['/401']);
return false;
}
},
error => {
console.log('error from the local authorization');
this.router.navigate(['/signin']);
return false;
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.