一件事是确保在调用watch()
之前调用设置回调request()
。
例如,在您的页面中是这样的:
<script src="https://login.persona.org/include.js"></script>
<script>
window.addEventListener("DOMContentLoaded", function() {
navigator.id.watch({
// Provide a hint to Persona: who do you think is logged in?
loggedInUser: null,
// Called when persona provides you an identity assertion
// after a successful request(). You *must* post the assertion
// to your server for verification. Never verify assertions
// in client code. See Step 3 in this document:
// https://developer.mozilla.org/en/Persona/Quick_Setup
onlogin: function(assertion) {
// do something with assertion ...
// Note that Persona will also call this function automatically
// if a previously-signed-in user visits your page again.
},
onlogout: function() {
// handle logout ...
},
onready: function() {
// Your signal that Persona's state- and callback-management
// business is complete. Enable signin buttons etc.
}
});
// Set up click handlers for your buttons
document.getElementById("signin").addEventListener(
'click', function() {
navigator.id.request({
// optional callback to request so you can respond to
// a user canceling the sign-in flow
oncancel: function() { /* do something */ }
});
}
});
});
</script>
下面是一个例子,展示了这一点:
https://people.mozilla.org/~jparsons/persona_example.html
但是,在 FirefoxOS 上,您应该知道已安装的应用程序(未打包或认证,而是通用安装的应用程序)具有唯一的表单来源,app://{uuid}
每个设备具有不同的 uuid。不幸的是,这对于登录目的毫无用处,因为您的服务器无法知道请求登录的应用程序是朋友还是敌人。解决此问题的方法是在服务器上托管的不可见 iframe 中运行您的角色代码。因此 iframe 将具有正确的来源,并且您的服务器将知道它是您的应用程序。iframe 和应用程序可以通过 postMessage 进行通信。
对于打包的应用程序(有时称为特权应用程序),您的来源将是您的 webapp 清单中声明的来源。例如,app://yourapp.yoursite.org
。这可以让您更好地确保应用程序确实是您的,但真正偏执的人可能仍希望部署 iframe 技巧。
希望这可以帮助!j