Is there a way to redirect to the authorization endpoint before loading the Angular components and HTML? Can I use the CanActivate auth guard on my main app component for this purpose?
I have implemented an auth guard in my Angular project, but it seems to load the HTML before redirecting to the authorization endpoint.
This is what I have configured:
(Routing Component)
const routes: Routes = [
{
path: '',
redirectTo: 'main',
pathMatch: 'full',
canActivate: [AdminRouteGuard]
},
{
path: 'main',
component: MainComponent,
canActivate: [AdminRouteGuard]
},
{
path: '**',
redirectTo: 'main',
pathMatch: 'full',
canActivate: [AdminRouteGuard]
}];
(Route Guard)
export class AdminRouteGuard implements CanActivate {
constructor(@Inject(DOCUMENT) private document: any, private _authService: AuthService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this._authService.isLoggedIn()) {
return true;
}
else {
return this._authService.login();
}}}