After spending 2 days struggling with this problem, scouring the API documentation of Electron.js and various websites, I am turning to you all as my final hope:
Here are the 3 files that are causing the issue:
main.ts (excerpt):
app.whenReady().then(() => {
ipcMain.on("set-credentials", (event, args: object) => {
encryptedCredentials = safeStorage.encryptString(JSON.stringify(args));
});
ipcMain.handle("get-credentials", async (event, args: null) => {
const decryptedCredentials = await JSON.parse(
safeStorage.decryptString(encryptedCredentials)
);
return decryptedCredentials;
});
createWindow();
});
preload.ts (full code):
import { ipcRenderer, contextBridge } from "electron";
contextBridge.exposeInMainWorld("electronAPI", {
setStoredCredentials: async (args: object) =>
await ipcRenderer.send("set-credentials", args),
getStoredCredentials: () => {
ipcRenderer.invoke("get-credentials");
},
});
Login.tsx (excerpt):
const setStoredCredentials = async (e: any, service: string) => {
e.preventDefault();
window.electronAPI.setStoredCredentials({
service,
username: e.target[0].value,
password: e.target[1].value,
});
setTimeout(() => {
window.electronAPI.getStoredCredentials().then(
(data: object) => { console.log(data); } //testing
)
}, 1000)
};
(the electron app features React)
Despite trying methods like ipcMain.on
and ipcRenderer.sendSync
with event.returnValue
, I still encountered an error that says:
caught TypeError: Cannot read properties of undefined (reading 'then')
The other methods I attempted resulted in either the same error or an undefined
value.