title: Angular 2 AuthGuard Service with redirect?
body: <p>I have an application that I am building that implements CanActivate on the dashboard route. It works fine except on page reload, I check a flag in the user service to see if a user is logged in or not. By default this flag is false which kicks the user out to login. Also on page reload I am trying to fetch user data with a token in localStorage, if fetch is successful, I want them to be able to stay on the dashboard. The problem is that I am seeing a glimpse of login and having to manually redirect them to the dashboard. Is there any way to fix this to where the authGuard doesn't do anything until after it checks the API? Code is here: <a href="https://github.com/judsonmusic/tfl" rel="noreferrer">https://github.com/judsonmusic/tfl</a></p>
  <p>dashboard:</p>
  <pre class="lang-ts prettyprint-override"><code>import { Component, ViewChild } from '@angular/core';
  import { LoginComponent } from &quot;../login.component&quot;;
  import { UserService } from &quot;../user.service&quot;;
  import { SimpleChartComponent } from &quot;../charts/simpleChart.component&quot;;
  import { AppleChartComponent } from &quot;../charts/appleChart.component&quot;;
  import { BarChartComponent } from &quot;../charts/barChart.component&quot;;
  import { DonutChartComponent } from &quot;../charts/donutChart.component&quot;;
  import { AlertComponent } from 'ng2-bootstrap/ng2-bootstrap';
  import { ModalDemoComponent } from &quot;../modals/modalDemoComponent&quot;;
  import { NgInitHelperComponent } from &quot;../helpers/nginit.helper.component&quot;;
  import { ModalDirective } from &quot;ng2-bootstrap/ng2-bootstrap&quot;;
  import { MODAL_DIRECTIVES, BS_VIEW_PROVIDERS } from 'ng2-bootstrap/ng2-bootstrap';
  
  
  @Component({
    selector: 'dashboard',
    templateUrl: '/app/components/dashboard/dashboard.component.html',
    providers: [UserService, BS_VIEW_PROVIDERS],
    directives: [SimpleChartComponent, AppleChartComponent, BarChartComponent, DonutChartComponent, AlertComponent, ModalDemoComponent, NgInitHelperComponent, ModalDirective]
  })
  export class DashboardComponent  {
  
    public areas: any;
  
    constructor() {
  
      this.areas = [
        &quot;Spiritual&quot;,
        &quot;Habits&quot;,
        &quot;Relationships&quot;,
        &quot;Emotional&quot;,
        &quot;Eating Habits&quot;,
        &quot;Relaxation&quot;,
        &quot;Exercise&quot;,
        &quot;Medical&quot;,
        &quot;Financial&quot;,
        &quot;Play&quot;,
        &quot;Work/ Life Balance&quot;,
        &quot;Home Environment&quot;,
        &quot;Intellectual Well-being&quot;,
        &quot;Self Image&quot;,
        &quot;Work Satisfaction&quot;
      ]
  
    }
  }
  </code></pre>
  <p>Routes:</p>
  <pre class="lang-ts prettyprint-override"><code>import { Routes, RouterModule } from '@angular/router';
  import { AboutComponent } from './components/about.component';
  import { PageNotFoundComponent } from &quot;./components/pageNotFound.component&quot;;
  import { HomeComponent } from &quot;./components/home.component&quot;;
  import { DashboardComponent } from &quot;./components/dashboard/dashboard.component&quot;;
  import { SurveyComponent } from &quot;./components/survey/survey.component&quot;;
  import { ResourcesComponent } from &quot;./components/resources.component&quot;;
  import { LogoutComponent } from &quot;./components/logout.component&quot;;
  import { AuthGuard } from &quot;./components/auth-guard.service&quot;;
  import { loginRoutes, authProviders } from './login.routing';
  import { LoginComponent } from &quot;./components/login.component&quot;;
  
  const appRoutes:Routes = [
    { path: '', component: HomeComponent },
    { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
    { path: 'logout', component: LogoutComponent },
    { path: 'resources', component: ResourcesComponent },
    { path: 'survey', component: SurveyComponent },
    { path: 'about', component: AboutComponent },
    { path: 'login', component: LoginComponent },
    { path: '**', component: PageNotFoundComponent }
  ];
  
  export const appRoutingProviders: any[] = [
    authProviders
  ];
  export const routing = RouterModule.forRoot(appRoutes);
  </code></pre>
  <p>login route:</p>
  <pre class="lang-ts prettyprint-override"><code>import { Routes }         from '@angular/router';
  import { AuthGuard }      from './components/auth-guard.service';
  import { AuthService }    from './components/auth.service';
  import { LoginComponent } from './components/login.component';
  export const loginRoutes: Routes = [
    { path: 'login', component: LoginComponent }
  ];
  export const authProviders = [
    AuthGuard,
    AuthService
  ];
  </code></pre>