Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

IdentityManager.watch()


You can include the library from https://login.persona.org/include.js.

注意: 不是所有的浏览器都支持这项功能. 使用 Persona 的网站必须在他们的页面中包含 polyfill library .

概要

This function registers callbacks that respond to a Persona user logging in or out.

语法

navigator.id.watch({
  loggedInUser: '[email protected]',
  onlogin: function(assertion) {
    // A user has logged in! Here you need to:
    // 1. Send the assertion to your backend for verification and to create a session.
    // 2. Update your UI.
  },
  onlogout: function() {
    // A user has logged out! Here you need to:
    // Tear down the user's session by redirecting the user or making a call to your backend.
  }
});

参数

loggedInUser 可选
NOTE: This parameter was renamed from loggedInEmail in early September. Both names will continue to work for the time being, but code should be changed to use loggedInUser instead.
The email address of the currently logged in user. This should be a string containing the user's email address if the website believes that a user is currently logged in, or null otherwise. If the website is unsure, this should be set to undefined or omitted.
Persona compares its knowledge of the local user to the value of loggedInUser to determine which callback—onlogin, onlogout, or none at all—to automatically invoke when your page loads. If loggedInUser is undefined or omitted, Persona will invoke either onlogin or onlogout, depending on whether or not a local user should be logged in to your site. These callbacks are not invoked automatically if both BrowserID and loggedInUser agree on the local user's state.
Note that Persona may automatically call either onlogin or onlogout when your page loads, but not both. If loggedInUser is set to [email protected], but Persona believes [email protected] should be logged in, only onlogin will be called. It will have an assertion for [email protected] as its first parameter.
onlogin
A function which will be invoked and passed a single argument, an assertion, when the user logs in. This function should send the assertion to the site's backend for verification. If verification succeeds, the backend should establish a session for the user and the function should update the UI as appropriate.
onlogout
A function that will be invoked, without any arguments, when the user logs out. This should tear down the user's session by making a call to the site's backend, or by redirecting the user.

示例

需要示例.

技术规范

没有包含在任何技术规范中.

查看其它内容

文档标签和贡献者

 此页面的贡献者: teoli, AshfaqHossain, ziyunfei, benhuan
 最后编辑者: teoli,