title: Retrieving URL From Just Prior to Logout in Angular 2 App
body: <p>I am trying to obtain the last active url prior to logging a user out of my Angular 2 app, so that when they log back in it will redirect them to that component/page. The thing is, while I'm able to obtain the active url with <code>this.router.routerState.snapshot['url']</code> -- this always provides me with the result <code>/login</code> -- which is the url they're re-directed to right AFTER logout takes place. What I need to do is find a way to get the url JUST PRIOR to logout happening. I tried doing this in my logout function in my authenticationService:</p>
  
  <pre><code>logout()
  {
      let activeUrl = this.router.routerState.snapshot['url'];
      console.log(activeUrl);
      this.apiService.logout();
  
      // remove user from local storage to log user out
      sessionStorage.removeItem('currentUser');
      console.log('User successfully logged out');
  
      // Trigger event
      this.change.emit(this);
  }
  </code></pre>
  
  <p>But even though the activeUrl is being console.logged before <code>this.apiService.logout()</code> is called (see order of operations in above function), I still end up getting <code>/login</code> logged to the console. Is this to be expected? How can I get around this?</p>
  
  <p>EDIT: After some reflection it occurs to me now that the problem may be that I'm calling the logout() function from within the <code>ngOnInit</code> life cycle hook of my login component - so at that point '/login' is going to be the result regardless:</p>
  
  <pre><code>ngOnInit()
  {
      this.authenticationService.getActiveUrl();
      this.authenticationService.logout();
  }
  </code></pre>
  
  <p>So the pertinent question becomes: how do I capture the active URL <em>just prior</em> to being logged out?</p>