--- title: navigator.id.watch slug: Web/API/IdentityManager/watch tags: - BrowserID - DOM - Persona translation_of: Archive/IdentityManager/watch ---
この関数は、Persona ユーザのログインとログアウトに応答するコールバックを登録します。
navigator.id.watch({
loggedInUser: 'bob@example.org',
onlogin: function(assertion) {
// ユーザがログインしました! ここで必要なことは:
// 1. 検証とセッション作成のためのアサーションをバックエンドに送信する。
// 2. UI を更新する。
},
onlogout: function() {
// ユーザがログアウトしました! ここで必要なことは:
// リダイレクトするかバックエンドの呼び出しを行って、ユーザのセッションを破棄する。
}
});
loggedInUser {{optional_inline}}null あるいは undefined です。| loggedInUser | Persona's State | Callback |
|---|---|---|
null |
"foo@example.com" | onlogin() |
undefined |
"foo@example.com" | onlogin() |
| "bar@example.com" | "foo@example.com" | onlogin() |
| "foo@example.com" | "foo@example.com" | none |
null |
null |
none |
| "foo@example.com" | null |
onlogout() |
undefined |
null |
onlogout() |
onlogin と onlogout の どちらか であることに注意してください。両方 呼ばれることはありません(訳注:表のとおりどちらも呼ばれないことはある)。loggedInUser に foo@example.com がセットされているのに、Persona が bar@example.com がログインしていると認識している場合、onlogin のみが呼び出されます。この場合、第1引数として bar@example.com のアサーションが渡されます。onloginonlogout {{ optional_inline() }}onlogout が与えられなかったとき Observer API によるセッション管理は無効化されます。onready と onlogin だけが呼び出されます。onlogin は、ユーザーによるログイン操作の反応としてしか呼び出されません(つまりユーザーがログインしていた場合に自動的に呼び出されたりはしません)。onready {{ optional_inline() }}id.request and id.logout. The onready callback will be invoked immediately after any automatic invocations of onlogin, onlogout, or onmatch. By waiting to display UI until onready is called, relying parties can avoid UI flicker in cases where the user agent's preferred state is out of sync with the site's session.onready will not be invoked after calls to id.request or id.logout. It is the punctuation mark that concludes the conversation started by watch.navigator.id.watch({
loggedInUser: currentUser, // This is email of current user logged into your site
onlogin: function(assertion) {
$.ajax({ // This example uses jQuery, but you can use whatever you'd like
type: 'POST',
url: '/auth/login', // This is a URL on your website.
data: {assertion: assertion}
success: function(res, status, xhr) { window.location.reload(); },
error: function(xhr, status, err) {
navigator.id.logout();
alert("Login failure: " + err);
}
});
},
onlogout: function() {
$.ajax({
type: 'POST',
url: '/auth/logout', // This is a URL on your website.
success: function(res, status, xhr) { window.location.reload(); },
error: function(xhr, status, err) { alert("Logout failure: " + err); }
});
}
});
まだどの仕様書にも含まれていません。