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
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.