I created an app in electron with react. I made two windows: login window and app window. In the login window, I set up a basic HTML form and I call the login endpoint from being like this:
async function login() {
const response = await loginResponse(
usernameInput.value,
passwordInput.value
);
if (response?.invalidData === true) {
alert("Wrong credentials!");
return;
}
return ipcRenderer.send("open-app", response?.token, response?.displayName);
}
The event open-app it's for creating an app window and sending a token, displayName to react app:
ipcMain.on("open-app", async (e, token, displayName) => {
console.log(token, displayName);
if (appWindow) {
appWindow.focus();
return;
}
appWindow = new BrowserWindow({
width: 1200,
height: 900,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
appWindow.webContents.on("dom-ready", (e) => {
appWindow.webContents.send("auth-data", {
token,
displayName,
});
});
appWindow.loadURL(
isDev
? "http://localhost:3000"
: `file://${__dirname}/../build/index.html`
);
});
And in my App.js I save token in local storage:
useEffect(() => {
ipcRenderer.on("auth-data", async (data) => {
console.log(data);
const { token, displayName } = data;
console.log(token);
if (token && displayName) {
window.localStorage.setItem("auth", token);
window.localStorage.setItem("displayName", displayName);
await apiService.markOnline(token);
}
});
}, []);
the problem it's the token doesn't save or if it's saved it doesn't update in localstorage when I re-open the app. How to send and update properly the token between the windows?