Skip to main content

Posts

Showing posts with the label AuthGuard

Angular 5 Redirect if not logged in - CanActivate AuthGuard

In this article, I will discuss how to redirect one page to other page and also discuss how to use the AuthGuard  and CanActivate in Angular 5 applications. Three options are available to redirect page in Angular 5 – 1.       Imperatively redirect the user in AppComponent 2.       Use the route data property 3.       Use a CanActivate guard The AuthGuard should call an application service that can login a user and retain information about the current user. Options 1 – Imperatively redirect the user in AppComponent @ Component ({   selector: 'app-root' ,   template: `.....` }) export class AppComponent {   constructor ( private _authService : AuthService , _router : Router ) {     if ( _authService . isLoggedIn ()) {       _router . navigate ([ 'user' ]);     }   } } This is not good pr...