Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Background:

I am handling authentication in my project using Authentication Handler class. The logic is: token created during login will be verified and if it is valid, then only access to required APIs will be given.

Scenario:

I am logged in the application with valid credentials(I have a token created during login). If I open a page in another tab from the app (using window.open(address, "_blank")), it is opening that page. But when I manually open another tab on browser and enter the same address in address bar, ,it is redirecting me to login page.

Question:

Isn't opening page using window.open and manually entering address in address bar the same thing? How come I have token available in session storage when I am using window.open but not available when I am manually entering url in new tab?

What I have tried:

When redirecting from window.open code, newly opened tab has token present in Session Storage of browser(idk how it got that, did not passed token as query string), but on manually entering url, token is not present in session storage.
Posted
Updated 19-Jul-22 23:03pm
v2

1 solution

According to the Window.sessionStorage - Web APIs | MDN[^] page, the session is only valid for that particular tab. What I suspect is happening is that when the new window is opened via Javascript, the browser knows to pass along the session storage to the new tab (ie. the origin tab has the session so passes it to the new tab). Whereas opening a new tab explicitly forces the browser to treat it as a new session.

If you want to persist the token regardless of new tabs, consider using Window.localStorage - Web APIs | MDN[^]
 
Share this answer
 
Comments
Codes DeCodes 21-Jul-22 7:57am    
Thanks for the link. Came to conclusion that I am duplicating a tab on windows.open and "Duplicating a tab copies the tab's sessionStorage into the new tab." as per link you have attached. That's how session storage is being passed to new tab.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900