简体   繁体   中英

Unhandled Rejection (TypeError): keytar.setPassword is not a function

I am trying to save a password using keytar.js, for some reason I keep getting this error when using ANY function from keytar.

For information : the lib. is installed and the functions are there and present (I checked using console.log) and they work just fine with catch().

Here is some code :

 const keytar = require("keytar");

I am using it in a componentDidMount() function from react,

I tried these 2 versions but none of them seems to be working :

  keytar
  .setPassword("stoma", "user", user)
  .then(() => {
   console.log("User .. OK !");
   })
  .catch(() => {
   console.log("User .. ERROR !");
   ok = false;
   })

and

 keytar.setPassword("stoma", "statut", "true");

Here's some error log too :

 keytar.js:38 Uncaught (in promise) TypeError: keytar.setPassword is not a function
at keytar.js:38
at keytar.js:12
at new Promise (<anonymous>)
at callbackPromise (keytar.js:11)
at Object.setPassword (keytar.js:37)
at _callee$ (Login.js:52)
at tryCatch (runtime.js:62)
at Generator.invoke [as _invoke] (runtime.js:288)
at Generator.prototype.(:3000/anonymous function) [as next] (http://localhost:3000/static/js/0.chunk.js:1881:21)
at asyncGeneratorStep (asyncToGenerator.js:3)
at _next (asyncToGenerator.js:25)
at asyncToGenerator.js:32
at new Promise (<anonymous>)
at asyncToGenerator.js:21
at _validate (Login.js:48)
at validate (Login.js:48)

If some can help , I would be grateful.Thank you.

When using Electron, you have to use keytar through the main process. Calling keytar from the render process won't work.

As resolved in this question you have to do the following:

In the main.js file you have to define the listeners that you are going to invoke, with the keytar logic that you need. In this case, getting and setting the password

ipcMain.on('get-password', (event, user) => {
    event.returnValue = keytar.getPassword('ServiceName', user);
});

ipcMain.on('set-password', (event, user, pass) => {
    event.returnValue = keytar.replacePassword('ServiceName', user, pass);
});

Then from the renderer process, you can call

const password = ipcRenderer.sendSync('get-password', user);

or

ipcRenderer.sendSync('set-password', user, pass);

ipcMain docs: here . ipcRenderer docs: here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM