I have 2 subdomains and 1 main page: product.com
, expert.product.com
and user.product.com
.
After user login in the main page: product.com
, my code will check which role of user belongs to: expert, user. If user's role is expert, it will direct user to expert.product.com
.
I save user to local storage. I would like to use cross-domain local storage in Reactjs. However, I do not know how to use it :(.
Where should I use cross-domain local storage: in repository of main page, expert.product.com
or user.product.com
? Do I need to set up from my server or cross-domain local storage can be done in front end side? Could anyone help me for this case?
Set localstorage
handleSubmit = event => {
event.preventDefault();
api
.post(`myapi, {
identifier: this.state.userSignIn.emailSignIn,
password: this.state.userSignIn.passwordSignIn,
})
.then(response => {
localStorage.setItem('userData', response.data.user);
localStorage.setItem('user', JSON.stringify(response.data));
this.setState({ auth: true, open: false, openSignin: false });
this.props.history.push('/profile');
})
.catch(error => {
console.log('An error occurred:', error);
});
};
Get LocalStorage
componentDidMount() {
if (localStorage.getItem('userData')) {
const userObject = JSON.parse(localStorage.getItem('user'));
const jwt = userObject.jwt;
const config = {
headers: { 'Authorization': `bearer ${jwt}` },
};
}