title: Using angular 2 guard. How to Keep user logged in when refreshing the page?
body: <p>Here is my angular code for routing in app.module.ts file.</p>
  
  <pre><code>const appRoutes: Routes = [
  {path: '', component: LoginformComponent},
    {path: 'dashboard', component: DashboardComponent, canActivate: [AuthenticationGuard]},
    {path: 'user', children: [
        {path: '', component: UserComponent},
        {path: ':id', component: UserdetailComponent}
      ], canActivate: [AuthenticationGuard]
    },
    {path: '**', component: NotfoundComponent}
  ];
  </code></pre>
  
  <p>And following code represent the guard file.</p>
  
  <pre><code>export class AuthenticationGuard implements CanActivate {
  
    constructor(private user: UserService, private router: Router) {}
  
    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
      if (! this.user.getUserLoggedIn()) {
        this.router.navigate(['/']);
      }
      return this.user.getUserLoggedIn();
    }
  }
  </code></pre>
  
  <p>Guard works fine when Im logged in as a user. But in case of refreshing it log me out. I want a method to keep me logged in the system even after refreshing the page?</p>
  
  <p>This is the user service.ts file,</p>
  
  <pre><code>@Injectable()
  export class UserService {
    private isUserLoggedIn: boolean;
    private username: string;
  
    constructor() {
      this.isUserLoggedIn = false;
    }
  
    setUserLoggedIn(username) {
      this.isUserLoggedIn = true;
      this.username = username;
    }
  
    getUserLoggedIn() {
      return this.isUserLoggedIn;
    }
  
    getUserName() {
      return this.username;
    }
  }
  </code></pre>
  
  <p>Can someone help me..........</p>