gpt4 book ai didi

angular - 可以激活守卫并使用带有 angular 5 的 observables

转载 作者:行者123 更新时间:2023-12-03 20:30:09 25 4
gpt4 key购买 nike

我正在使用实现 canActivate 的路由保护

我在代码中放置了一堆控制台日志以了解它在哪里失败。

如果我导航到 protected 路线,会发生什么。导航失败,因为守卫未能返回值。我的 http map 没有完成。

我目前有一个 JWT token 保存在我的 session 存储中,但没有保存在我的本地

这些是我运行守卫时得到的控制台日志
running the local checkrunning the session checkgot session token authorizing it
然后 http map 回来,然后代码中断。

完整代码如下。帮助将不胜感激!

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 返回一个 observable发出一个 bool 值。在幕后,Angular 订阅返回的 observable,然后根据发出的值正确处理路由。
如果您将调用返回到您的 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;
}
);
}
}

}

关于angular - 可以激活守卫并使用带有 angular 5 的 observables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com