问题描述
我想为我的服务添加路由功能,因此尝试使用Authguards。因此,我有一个简单的服务,该服务使用Google Recaptcha,并且会根据收到的验证码得分,在检测到用户是否为漫游器时执行某些操作。
//appactionservice.ts
performActions(){
this.reCaptchaV3Service.execute()
.subscribe((res: any) => {
if (res.score > 0.7) {
this.isUserRobot(true);
} else {
this.isUserRobot(false);
}
}
这是一个返回承诺的函数,它将在AuthGuardService中使用
//appactionservice.ts
//variable
public userIsABot
//the actual method
public isUserRobot(boolval?): Promise<boolean> {
console.log('boolval',boolval);
return new Promise((resolve => {
this.userIsABoT = boolval;
console.log('userisabotboolean',this.userIsABoT);
resolve(this.userIsABoT);
}));
}
在AuthguardService中,我要执行的操作是在将用户检测为漫游器(分数小于阈值)时将用户路由到该页面
@Injectable({
providedIn: 'root'
})
export class AuthGuardsService implements CanActivate {
public varia: boolean;
constructor(private appActionService: AppActionService,private router: Router,private route: ActivatedRoute) { }
canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): Observable<boolean>
| Promise<boolean> | boolean {
console.log('inside authguard');
return new Promise((resolve => {
console.log('display a promise',this.appActionService.isUserRobot());
this.appActionService.isUserRobot().then((response) => {
console.log('inside a promise in authguard',response); //shows undefined
this.varia = response;
console.log('varia',this.varia); //shows undefined
if (!this.varia) {
resolve(true);
} else {
setTimeout(() => {
this.router
.navigate(['../welcome'],{ relativeTo: this.route })
.then(() => {
this.appActionService.closeChat();
});
},30000);
resolve(false);
}
});
}));
}
}
我要尝试的是在检测到用户是机器人时返回承诺,并在authguard-service.ts中解析该承诺
如何从appactionservice订阅更改值?
但是这些值是不确定的。有什么我想念的吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)