reZach / secure-electron-template

The best way to build Electron apps with security in mind.
MIT License
1.65k stars 154 forks source link

How to create multiple stores? It creates path issues in my project #106

Closed Anas-Nabulsi closed 2 years ago

Anas-Nabulsi commented 2 years ago

I'm trying to have multiple stores in my project. I set them up as follows in main.js:

const DATA_PATH = app.getPath("userData");
// General store for user settings
  const store = new Store({
    path: DATA_PATH
  });
// Store for cars added by user
  const carsStore = new Store({
    path: DATA_PATH,
    filename: "cars", 
    unprotectedFilename: "cars_unprotected",
  });

And in the preload.js:

const store = new Store({});
const carsStore = new Store({
  filename: "cars", 
  unprotectedFilename: "cars_unprotected",
});

contextBridge.exposeInMainWorld("api", {
  ...
  store: store.preloadBindings(ipcRenderer, fs),
  carsStore: carsStore.preloadBindings(ipcRenderer, fs),
  ...
});

This throws the following error in the console and prevents from executing preload.js image

Why does it search for a directory name called "unprotected.json" ? Any help is appreciated.

reZach commented 2 years ago

The "unprotected.json" is the json file that's meant to hold system-level config values that aren't considered secure (ie. x/y of the screen position, size of app screen), so that you can persist these values across app launches see here.

I have a theory of what's wrong, can you try using this code?

const store = new Store({});
const carsStore = new Store({
  filename: "cars", 
  unprotectedFilename: "cars_unprotected",
  unprotectedPath: ""
});

contextBridge.exposeInMainWorld("api", {
  ...
  store: store.preloadBindings(ipcRenderer, fs),
  carsStore: carsStore.preloadBindings(ipcRenderer, fs),
  ...
});
Anas-Nabulsi commented 2 years ago

That actually worked. Thank you for saving me time. I added the cars_unprotected because I thought a conflict is occurring between the unprotected files of the two stores since they have the same default filename value of "unprotected".