How to persist a local storage data when user get redirected to subdomain

Viewed 31

I'm building a react app where users get redirected to the subdomain when they log in or signup. But the problem is After they get redirected to a subdomain, they need to log in again on the subdomain, and I also don't have access to the user's local storage data and redux state from the subdomain.

I'm redirecting a user using this code:

window.location.href = `http://${data?.result?.username}.localhost:3000/`;

Anyone, please guide me!

1 Answers

You must be able to have generated token from the API where you requested your login credentials. This token must be a known contract from both client side and backend side.

When you are able to have that information, set that token together with the URL as a query string after the login succeeded. Since you cannot use or access storage across different tab sessions.

window.location.href =`http://${data?.result?.username}.localhost:3000?token=${your-token-from-login-api-response}`;

Then on the first load of your app in the different subdomain get the set query params and parse it in a way to identify if the user is already authenticated.

const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
Related