PasswordCredential 不存储数据
PasswordCredential does not store the data
我在 Google Chrome 和 Edge 中使用 PasswordCredential API 来存储身份验证凭据,但是不会保存此数据。
我正在使用下面的代码,只有在 AJAX 登录成功时才会触发它。
var cred = new PasswordCredential({
name: account,
id: email,
password: password,
iconURL: 'https://example.com/favicon.ico'
});
navigator.credentials.store(cred).then(() => {
if (redirect !== undefined) {
$window.location.href = 'dashboard.html';
}
});
如果在.store
的then
函数里面我把它用来拉取保存的数据,显示空数据。
navigator.credentials.store(cred).then(() => {
navigator.credentials.get({ password: true }).then(function (auth) {
console.log(auth); //Return NULL
console.log(auth.password); //Returns that does not exist
console.log(auth.id); //Returns that does not exist
console.log(auth.name); //Returns that does not exist
});
});
我的代码哪里做错了?
编辑
我相信我发现了问题,因为用户选择并且没有在浏览器中保存凭据,承诺正在等待中。
有什么方法可以自动执行此操作而无需用户手动保存?
当它完成时我如何处理承诺?
根据 W3C 它存储 api 将 return 承诺以确保它已向浏览器提出正确的请求,它不会告诉是否使用保存。如果您想实现这一点,您可以创建 setInterval,您可以在其中通过调用 get from store 定期检查。
在保存参考中,promise 已经显示为 fulfilled 如下:
我在 Google Chrome 和 Edge 中使用 PasswordCredential API 来存储身份验证凭据,但是不会保存此数据。
我正在使用下面的代码,只有在 AJAX 登录成功时才会触发它。
var cred = new PasswordCredential({
name: account,
id: email,
password: password,
iconURL: 'https://example.com/favicon.ico'
});
navigator.credentials.store(cred).then(() => {
if (redirect !== undefined) {
$window.location.href = 'dashboard.html';
}
});
如果在.store
的then
函数里面我把它用来拉取保存的数据,显示空数据。
navigator.credentials.store(cred).then(() => {
navigator.credentials.get({ password: true }).then(function (auth) {
console.log(auth); //Return NULL
console.log(auth.password); //Returns that does not exist
console.log(auth.id); //Returns that does not exist
console.log(auth.name); //Returns that does not exist
});
});
我的代码哪里做错了?
编辑
我相信我发现了问题,因为用户选择并且没有在浏览器中保存凭据,承诺正在等待中。 有什么方法可以自动执行此操作而无需用户手动保存?
当它完成时我如何处理承诺?
根据 W3C 它存储 api 将 return 承诺以确保它已向浏览器提出正确的请求,它不会告诉是否使用保存。如果您想实现这一点,您可以创建 setInterval,您可以在其中通过调用 get from store 定期检查。
在保存参考中,promise 已经显示为 fulfilled 如下: