CanActivate Promise <boolean>在拒绝

时间:2017-03-04 06:28:21

标签: angular angular-ui-router firebase-authentication

当尝试将不同用户的角色转移到应用的不同区域时,最终获得了CanActivate。但是,如果他们走错路线,我就无法重定向。这就是我所拥有的:

 canActivate(route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Promise<boolean> {
    return new Promise((resolve, reject) => {

        this.af.auth.subscribe(user => {

            if (user) {
                let userProfile = firebase.database().ref(`/users/${user.uid}`);
                userProfile.once("value", (snapshot) => {
                    let userInfo = snapshot.val().role;
                    if (userInfo === "GroupA") {
                        resolve(true);
                    } else {
                        reject(this.router.navigate(['/login']));
                    }
                });

            } else {
                reject(this.router.navigate(['/login']));
            }

        });

    });
}

为什么它不会router.navigate? 100%愿意听取更好的想法来做到这一点。

2 个答案:

答案 0 :(得分:1)

这个有效!

private static void visit(CallGraph cg, SootMethod method) {
  String identifier = method.getSignature();
  visited.put(method.getSignature(), true);
  dot.drawNode(identifier);
  // iterate over unvisited parents
  Iterator<MethodOrMethodContext> ptargets = new Targets(cg.edgesInto(method));
  if (ptargets != null) {
    while (ptargets.hasNext()) {
        SootMethod parent = (SootMethod) ptargets.next();
        if (!visited.containsKey(parent.getSignature())) visit(cg, parent);
    }
  }
    // iterate over unvisited children
    Iterator<MethodOrMethodContext> ctargets = new Targets(cg.edgesOutOf(method));
  if (ctargets != null) {
    while (ctargets.hasNext()) {
       SootMethod child = (SootMethod) ctargets.next();
       dot.drawEdge(identifier, child.getSignature());
       System.out.println(method + " may call " + child);
       if (!visited.containsKey(child.getSignature())) visit(cg, child);
    }
  }
}

答案 1 :(得分:0)

CanActivate返回承诺仅会被解析为truefalse ,如果您拒绝< / em>。

这应该有效:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
    let navigateToLogin = () => this.router.navigate(['/login']);
    return new Promise((resolve) => {
        this.af.auth.subscribe(user => {
            if (user.isAuthenticated) {
                resolve(true);
            } else {
                navigateToLogin();
                resolve(false);
            }
        });
    });
}