Angular 2 - init a session service before guard kicks in
<p>So I have this guard to watch my protected routes on my app
Basically, my session service extracts its credentials from localStorage inside NgOnInit, and then the guard should ask the service if it has credentials so it will know if it should approve this route</p>

<pre><code>import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { SessionService } from './../session/session.service'
 
@Injectable()
export class AuthGuard implements CanActivate {
 
    constructor(
    private router: Router,
    private sessionService: SessionService
    ) { }
 
    canActivate() {
        //without this line this won't work
        //this.sessionService.loadUser();
        if (this.sessionService.isValidSession()) {
            // logged in so return true
            return true;
        }
 
        // not logged in so redirect to login page
        this.router.navigate(['/login']);
        return false;
    }
}
</code></pre>

<p>this is my session service:</p>

<pre><code>export class SessionService implements OnInit {
 .
 .
 .
constructor(
    private authService: AuthenticationService,
) {
    this.timeout = null;
}

ngOnInit() {
    this.loadUser();
}
</code></pre>

<p>Now the problem is that ngOnInit on my session service doesn't get called automatically and I have to explicitly call loadUser() inside the guard, which is not what I want to do</p>

<p>Any idea how to make the ngOnInit of session service to get executed before canActive of guard?</p>

<p>Thanks a lot!!!</p>