4

What I already know:

What I use:

  • Angular 2 (v2.4.4) with TypeScript on Angular CLI base

The actual problem:

The script (which shares the sessionStorage between tabs) runs within Angular before HTTP requests, but when the page creates a HTTP request, it uses a session id (which is sent to the server in order to identify the session) and the session id should be taken from sessionStorage, but it is not there yet.

When I open the browser's debugger console (on the new tab), the sessionStorage is there. And because of this when I reload this new tab everything works just fine. But the problem is that the sessionStorage was set after HTTP request already tried to use.

The above linked solution truly runs before HTTP calls, but the event calls happen after HTTP calls. I think about especial sessionStorage_transfer from the above linked code:

window.addEventListener("storage", sessionStorage_transfer, false);

I store the session id like this:

get sid(): string {
    return sessionStorage.getItem('user.sid') || '';
}
set sid(value: string) {
    sessionStorage.setItem('user.sid', value);
}

So eventually sid receives value but just after the HTTP call already tried to use it despite the fact that the script (which shares sessionStorage) started to run before any HTTP call.

(If there is no solution then I will use session cookie for sid alone but I hope someone knows a solution for this. Perhaps a Promise, Observable, Subject, anything?)

Thank you for your solutions! :-)

Community
  • 1
  • 1
Erik Kránicz
  • 345
  • 1
  • 2
  • 12
  • New tab has its own sessionStorage and it should not come anything from previous tab. I think http request gives you new sid, which is set in new tab's sessionStorage and you think it comes from previous tab. – oto lolua Feb 15 '17 at 15:00
  • 1
    I know that `sessionStorage` does not come from previous tab. That is the actual problem: I need the `sessionStorage` from the previous tab! I don't want to store the session variables in cookies, but in the `sessionStorage`, but it is not available on the other tab... – Erik Kránicz Feb 16 '17 at 08:16
  • There is good explanation: http://security.stackexchange.com/questions/80727/best-place-to-store-authentication-tokens-client-side – oto lolua Feb 16 '17 at 08:34

1 Answers1

1

keep the code from here mentioned in correct answer in your index.html of your angular web-app. It worked for me.

Chaitanya Chauhan
  • 743
  • 1
  • 11
  • 28