Get the value returned by canActivate Method in Angular2 Route in a component?

后端 未结 3 1173
忘了有多久
忘了有多久 2020-12-20 06:21

I have created a Auth Manager in Angular2 to restrict Component access directly . I am still new to angular and learning the concepts.

I was able to restrict the use

相关标签:
3条回答
  • 2020-12-20 06:43

    What i understood by the Question is that you want to display Some kind of Error Message to the User if the Auth Fails. @PierreDuc is correct.But i have a different approach to this.

    what you can do is create a service class

    authenticate.service.ts

    import {Injectable} from '@angular/core'
    
    @Injectable()
    
    export class AuthenticateService
    
        isAuthenticated(//take the user credentials here):boolean{
            return true // if the credentials are correct 
    
         or retun false // if the credentials donot match.    
    
        }
    

    make use of this class in your component and AuthManager

    import { Injectable } from '@angular/core';
    import { CanActivate,Router,ActivatedRouteSnapshot,RouterStateSnapshot } from '@angular/router';
    import {AuthenticateService} from './authenticate.service'
    
    @Injectable()
    
    export class AuthManager implements CanActivate{
    
        user = "user";
    
        constructor(private router:Router,private authenticateService:AuthenticateService){
    
        }
    
        canActivate(route:ActivatedRouteSnapshot,state:RouterStateSnapshot){
            console.log("Comes Here");
               return  this.authenticateService.isAuthenticated();
        }
    }
    

    and in the component check the same

    this.authenticateService.isAuthenticated()== false
    *ngIf and then display an error message to the user.
    

    Hope this is what you where looking at . it not exactly the answer of the question but a way to solve your problem.

    0 讨论(0)
  • 2020-12-20 06:47

    You can use service and set the value you want to display in component inside the AuthManager.

    0 讨论(0)
  • 2020-12-20 06:49

    That's quite an uncommon thing to do, but I understand why you would want something like that. It is however not possible to capture the result returned from CanActivate.

    However you could make an ErrorService and subscribe a component to an triggered event to show a toast message or something like that.

    untested code ahead

    @Injectable()
    export class AuthManager implements CanActivate{
    
        user = "user";
    
        constructor(private router:Router, private errorService: ErrorService){}
    
        canActivate(route:ActivatedRouteSnapshot,state:RouterStateSnapshot){
           if(this.user == "user"){
                return true;
           } else{
               this.errorService.toastError("You are not authorized");
               this.router.navigate(['/persons']);
               return false;
           }
        }
    }
    

    Your error service would in basic look something like this, you should add this service to your root providers:

    export class ErrorService {
    
       public readonly error: Subject<string> = new Subject<string>(); 
    
       public toastError(error: string): void {
         this.error.next(error);
       }
    
    }
    

    After that place a component inside your AppComponent (or whatever you deem fit) and make it something like this:

    @Component({
       selector : 'error-toast',
       template : `<div [innerHtml]="error" *ngIf="error"></div>`
    })
    export class ErrorToastComponent implements OnInit, OnDestroy { 
    
       public error: string = "";  
    
       private timeout: number = 0;
    
       private errorSub: Subscription;
    
       private readonly timer: number = 5000;
    
       constructor(private errorService: ErrorService) {}
    
       ngOnInit(): void {
          this.errorSub = this.errorService.subscribe((error: string) => {
              this.toastError(error);
          });
       }
    
       ngOnDestroy(): void {
          this.errorSub.unsubscribe();
          this.cancelTimer();
       }
    
       private toastError(error): void {
          this.cancelTimer();
          this.error = error;
          this.timeout = window.setTimeout(() => {
             this.error = "";
          }, this.timer);
       }
    
       private cancelTimer(): void {
          if(this.timeout !== 0) {
             clearTimeout(this.timeout);
             this.timeout = 0;
          }
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题