Skip to content

Commit 9fab0a7

Browse files
committed
CREDENTIAL: Add a federation to the demo.
1 parent 776714a commit 9fab0a7

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

demos/credential-management/script.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,30 @@ function processResponse(c) {
4949
* if no credential is provided).
5050
*/
5151
document.querySelector('#signin').addEventListener('click', function () {
52+
var formHTML = '' +
53+
'<button>Sign in via Federation.com</button>' +
54+
'<hr>' +
55+
'<label for="username">Username:</label>' +
56+
'<input id="username" type="text" name="username" autocomplete="username"></input>' +
57+
'<label for="password">Password:</label>' +
58+
'<input id="password" type="password" name="password" autocomplete="new-password"></input>' +
59+
'<input type="submit">';
60+
5261
console.log("Clicked 'sign in!'");
5362
if (navigator.credentials) {
5463
navigator.credentials.get({
5564
password: true
5665
}).then(function (c) {
5766
processResponse(c);
5867
if (!c) {
59-
document.querySelector('form').innerHTML = '<label for="username">Username:</label><input id="username" type="text" name="username" autocomplete="username"></input><label for="password">Password:</label><input id="password" type="password" name="password" autocomplete="new-password"></input><input type="submit">';
68+
document.querySelector('form').innerHTML = formHTML;
69+
document.querySelector('form button').addEventListener('click', handleFederation);
6070
document.querySelector('dialog').showModal();
6171
}
6272
});
6373
} else {
64-
document.querySelector('form').innerHTML = '<label for="username">Username:</label><input id="username" type="text" name="username" autocomplete="username"></input><label for="password">Password:</label><input id="password" type="password" name="password" autocomplete="new-password"></input><input type="submit">';
74+
document.querySelector('form').innerHTML = formHTML;
75+
document.querySelector('form button').addEventListener('click', handleFederation);
6576
document.querySelector('dialog').showModal();
6677
}
6778
});
@@ -77,6 +88,31 @@ document.querySelector('#signout').addEventListener('click', function () {
7788
document.body.classList.toggle('signedout');
7889
});
7990

91+
/*
92+
* Wire up the 'sign in via' button.
93+
*/
94+
function handleFederation(e) {
95+
console.log("Signed in via a federation!");
96+
e.preventDefault();
97+
98+
if (navigator.credentials) {
99+
var c = new FederatedCredential({
100+
101+
provider: 'https://accounts.federation.com/',
102+
iconURL: getFace('[email protected]')
103+
});
104+
navigator.credentials.store(c).then(function (a) { console.log(a); }).catch(function (e) { console.log(e); });
105+
}
106+
107+
// Sign the user in asynchronously using the relevant SDK for the chosen federation.
108+
109+
toggleState();
110+
document.querySelector('var').textContent = '[email protected]';
111+
document.querySelector('dialog').close();
112+
113+
return false;
114+
}
115+
80116
/*
81117
* Finally, we'll wire up the submit button on the form to call `store()` upon
82118
* submission if the API is available.

0 commit comments

Comments
 (0)