gpt4 book ai didi

javascript - 路由器 : Redirect from within a promise returned by a `canActivate` method

转载 作者:行者123 更新时间:2023-11-28 05:30:41 26 4
gpt4 key购买 nike

我有多种方法来访问我的服务器。所有这些方法都不返回 Promise 或 observables,我正在传递回调。

现在我需要编写一个防护程序,以确保在用户可以使用多个路由之前已加载用户数据。 canActivate 方法返回一个 Promise,但有一些代码可以在该 Promise 中导航,但该代码不起作用。

有人知道如何解决这个问题吗?我是否必须重写我的 api 方法才能返回 Promise 或 obervables?或者有没有办法通过 promise 进行重定向,返回我的canActivate。将获取用户数据(如果用户已登录)的代码移动到解析对象中会更好吗?

import {Injectable} from '@angular/core';
import {CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {GlobalStateService} from './globalState.service';
import {AuthTokenService} from './authToken.service';
import {UserApi} from '../api/user.api';

@Injectable()
export class AfterLogInGuardService implements CanActivate {

constructor(
private globalState: GlobalStateService,
private authToken: AuthTokenService,
private router: Router,
private userApi: UserApi
) { }

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean|Promise<boolean> {

// The tho user is not logged in, redirect her always to the login page, if
// she calls routes with this guard.
if (!this.authToken.has()) {
this.router.navigateByUrl('/logIn');
return false;
}

const thisGuard = this;

// If the user has a token, we have to ensure, that we have the user data
// loaded from the server.
return this.afterUserDataHasBeenLoaded(function () {

// Redirect the user to the "Please enter your personal data" page, if
// she has not entered them yet.
if (!thisGuard.globalState.personalDataStored && route.url.toString() !== 'signUp,personalData') {

//
// This here doesn't work!
//

thisGuard.router.navigateByUrl('/signUp/personalData');
return false;
}
return true;
});
};

private afterUserDataHasBeenLoaded(callback: () => boolean) {

const thisGuard = this;

return new Promise<boolean>(function(resolve, reject) {

if (thisGuard.globalState.userDataLoaded)
return callback();

const innerCallback = function () {
thisGuard.globalState.userDataLoaded = true;
resolve(callback());
};

thisGuard.userApi.getUserData(innerCallback);
});
}
}

最佳答案

以下实现可能很有用。 Promise 定义在 auth.service

auth.guard

import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { AuthService} from './services/auth.service';

@Injectable()
export class AuthGuard implements CanActivate {
status:boolean=false;
constructor(private authService:AuthService, private router:Router) { }
canActivate() {
var self=this;
this.authService.isAuth()
.then((res)=>{if(res==true)return true;else self.router.navigate(['/login']);});
}
}

auth.service

isAuth(): Promise<Status> {
const url = "/account/check";
return this.http.get(url)
.toPromise()
.then(response=>return response.json().status as Status)
.catch(this.handleError);
}

服务器响应

{_body: "{"status":false}", status: 200, ok: true, statusText: "OK", headers: Headers…}

关于javascript - 路由器 : Redirect from within a promise returned by a `canActivate` method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39721364/

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