首页 > 解决方案 > 如何使用keytar(用于电子)

问题描述

所以貌似electron有个叫keytar的模块,但是文档少了很多,不知道怎么用。

https://github.com/atom/node-keytar

我现在的代码是(在主进程中)

const keytar = require('keytar');
...
keytar.setPassword('KeytarTest', 'AccountName', 'secret');
const secret = keytar.getPassword('KeytarTest', 'AccountName');
console.log(secret);

哪个打印出来

Promise { <pending> }

有人可以教我如何获取实际密码吗?

标签: node.jspromiseelectron

解决方案


好的,环顾四周后,我找到了答案。您调用生成的 Promise 并对 Promise 的参数进行操作。

keytar.setPassword('KeytarTest', 'AccountName', 'secret');
const secret = keytar.getPassword('KeytarTest', 'AccountName');
secret.then((result) => {
    console.log("result: "+ result); // result will be 'secret'
});

推荐阅读