簡體   English   中英

可以激活警衛並使用角度為5的可觀察物

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM