I am struggling to capture the last active URL before logging a user out of my Angular 2 app. My goal is to redirect them back to the same component or page once they log back in. Currently, I am using
this.router.routerState.snapshot['url']
to retrieve the active URL; however, this always returns /login
, which is the URL the user is sent to after being logged out. What I actually need is the URL right before the logout action. I have attempted to do this within my authenticationService's logout function:
logout()
{
let activeUrl = this.router.routerState.snapshot['url'];
console.log(activeUrl);
this.apiService.logout();
// Remove user from local storage
sessionStorage.removeItem('currentUser');
console.log('User successfully logged out');
// Emit event
this.change.emit(this);
}
Despite logging the activeURL before calling this.apiService.logout()
, I still receive /login
in the console. Is this behavior expected? How can I work around this issue?
Upon further reflection, I realize that the problem may be due to calling the logout() function within the ngOnInit
lifecycle hook of my login component, resulting in '/login' being captured. Therefore, how can I obtain the active URL just before the logout action occurs?