Setar localStorage in different Omains

Asked

Viewed 526 times

0

I have three applications:

1. Login (which runs on door 8080) with jQuery very simple

2. API (wheel at port 4000) with Nodejs

3. Web (rotating at port 4200) with Angular

When I am logged in and click on "Log in", will call the API, validate if the user exists, if success will return a JWT token. When you return this token, I need to set it to localStorage, then redirect it to the web application.

The problem with this is that they are different ports, if I set in the login application’s localStorage and then redirect to the web application, the token will no longer exist in the localStorage.

How can I solve this problem?

1 answer

0

The API localStorage() is executed by the browser on the "Politics of Same Origin" (Same-Origin) so even if the application runs in the same domain (example.com) ports and subdomains are treated as different sources.

To get around this you can work on some solution that makes use of <iframe> and postMessage() to synchronize the data of localStorage().

There are several libraries that do just this as for example to Cross Domain Local Storage however, if its sole purpose is to confer the "token" just save this "token" in a cookie that it will be available for every domain, subdomains and ports.

Remember that JWT "tokens" have time markup for validation as well as you could create some basis to control outgoing and already used "tokens".

You can remove the cookie once using the "token", if using this approach do not forget to use (case in production) a cookie on the flags HTTPONLY (for the server only), SECURE (only about TLS) and even SAMESITE.

  • And then Lauro. I didn’t like the idea of using <iframe> to synchronize the data. Do you really think it’s good practice? I’ll take a look at this library you commented on.

  • No, it can be difficult to maintain, debug and if you need a more restrictive CSP becomes difficult... you will have to create rules for which pages to use or not a <iframe> is viable bad, I think if you just want the token will be easier to use cookie even

  • I came to use a long time ago something I found and modified...if I find in my documents sending to a Gist Github and add an Aki link... may have some utility within the scope of the question but I do not recommend to carry an access token

Browser other questions tagged

You are not signed in. Login or sign up in order to post.