Within my Angular 2 application, I am facing a challenge in storing the last active URL before a user logs out. The goal is to reload this URL once the user logs back in. However, this task has proven to be quite troublesome. Take a look at the logout function in my authenticationService:
logout()
{
let lastUrl = this.getActiveUrl();
console.log('Url before logout: ', lastUrl);
this.apiService.logout();
}
It is important to note that the "lastUrl" variable, which calls this.getActiveUrl()
, works as follows:
getActiveUrl()
{
let activeUrl = this.router.routerState.snapshot['url'];
console.log('activeUrl: ', activeUrl);
return activeUrl;
}
Strangely enough, even though "lastUrl" appears BEFORE this.apiService.logout()
, it still ends up displaying "/login". This is confusing because "/"login" is actually where I get redirected immediately after logging out.
So, here are my questions:
If this process is synchronous, why doesn't the correct URL display here? What am I overlooking? And how can I retrieve the active URL right before the logout event occurs and redirects to '/login'?
UPDATE: Following a suggestion from a commenter, I attempted to store the URL in localStorage rather than a local variable like so:
logout()
{
localStorage.setItem('returnUrl', JSON.stringify(this.router.routerState.snapshot['url']));
this.apiService.logout();
}
However, when I attempt to retrieve this value using localStorage.returnUrl
, I still end up with "/login".