|
| 1 | +/* |
| 2 | + * First, try to log the user in automatically by calling `get()`, and passing |
| 3 | + * in the `suppressUI` option. If the user can be unambigiously signed in (e.g. |
| 4 | + * there is one and only one valid credential for the origin, and the user has |
| 5 | + * the auto-sign-in option set), then credentials will be provided. |
| 6 | + */ |
| 7 | +if (navigator.credentials) { |
| 8 | + console.log("Trying automatic sign-in."); |
| 9 | + navigator.credentials.get({ |
| 10 | + password: true, |
| 11 | + suppressUI: true |
| 12 | + }).then(processResponse); |
| 13 | +} |
| 14 | + |
| 15 | +function toggleState() { |
| 16 | + console.log("Toggling UI state."); |
| 17 | + document.body.classList.toggle('signedin'); |
| 18 | + document.body.classList.toggle('signedout'); |
| 19 | +} |
| 20 | + |
| 21 | +function processResponse(c) { |
| 22 | + if (c) { |
| 23 | + console.log("Got credentials for %s!", c.id); |
| 24 | + |
| 25 | + // In a real site, we'd do something like the following to asynchronously |
| 26 | + // sign the user in: |
| 27 | + // |
| 28 | + // var fd = c.toFormData(); |
| 29 | + // fetch("https://example.com/signinEndpoint/", { body: fd, method: "POST" }) |
| 30 | + // .then(function (response) { |
| 31 | + // if ([check that the response is a valid signin event]) |
| 32 | + // updateUI(); |
| 33 | + // }); |
| 34 | + // |
| 35 | + // Here, we'll just update the UI: |
| 36 | + toggleState(); |
| 37 | + document.querySelector('var').textContent = c.id; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/* |
| 42 | + * Next, wire up the "Sign In" button to call `get()` without the `suppressUI` |
| 43 | + * option, and to fall back to a sign in form if the API is not available (or |
| 44 | + * if no credential is provided). |
| 45 | + */ |
| 46 | +document.querySelector('#signin').addEventListener('click', function () { |
| 47 | + console.log("Clicked 'sign in!'"); |
| 48 | + if (navigator.credentials) { |
| 49 | + navigator.credentials.get({ |
| 50 | + password: true |
| 51 | + }).then(function (c) { |
| 52 | + processResponse(c); |
| 53 | + if (!c) { |
| 54 | + 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">'; |
| 55 | + document.querySelector('dialog').showModal(); |
| 56 | + } |
| 57 | + }); |
| 58 | + } else { |
| 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">'; |
| 60 | + document.querySelector('dialog').showModal(); |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +/* |
| 65 | + * Wire up the 'Sign Out' link to call `requireUserMediation()` |
| 66 | + */ |
| 67 | +document.querySelector('#signout').addEventListener('click', function () { |
| 68 | + console.log("Clicked 'sign out!'"); |
| 69 | + if (navigator.credentials) |
| 70 | + navigator.credentials.requireUserMediation(); |
| 71 | + document.body.classList.toggle('signedin'); |
| 72 | + document.body.classList.toggle('signedout'); |
| 73 | +}); |
| 74 | + |
| 75 | +/* |
| 76 | + * Finally, we'll wire up the submit button on the form to call `store()` upon |
| 77 | + * submission if the API is available. |
| 78 | + */ |
| 79 | +document.querySelector('form').addEventListener('submit', function (e) { |
| 80 | + console.log("Submitted a sign-in form."); |
| 81 | + e.preventDefault(); |
| 82 | + |
| 83 | + if (navigator.credentials) { |
| 84 | + var c = new PasswordCredential({ |
| 85 | + id: document.querySelector('#username').value, |
| 86 | + password: document.querySelector('#password').value, |
| 87 | + iconURL: getFace(document.querySelector('#username').value) |
| 88 | + }); |
| 89 | + |
| 90 | + navigator.credentials.store(c).then(function (a) { console.log(a); }).catch(function (e) { console.log(e); }); |
| 91 | + } |
| 92 | + |
| 93 | + // Sign the user in asynchronously using the data in the form. This will either look like |
| 94 | + // the code in `processResponse()` above (if the credentials API is available), or |
| 95 | + // something like the following if it's not: |
| 96 | + // |
| 97 | + // fetch("https://example.com/signinEndpoint/", |
| 98 | + // { body: new FormData(document.querySelector('form')), method: "POST" }); |
| 99 | + // |
| 100 | + // Note that we call `e.preventDefault()` at the top of this event handler to prevent |
| 101 | + // the actual form submission while we do the asynchronous request. |
| 102 | + |
| 103 | + toggleState(); |
| 104 | + document.querySelector('var').textContent = document.querySelector('#username').value; |
| 105 | + document.querySelector('dialog').close(); |
| 106 | + |
| 107 | + // Clear out the form's value, because we're not actually navigating... fake it. |
| 108 | + document.querySelector('#username').value = ""; |
| 109 | + document.querySelector('#password').value = ""; |
| 110 | + |
| 111 | + return false; |
| 112 | +}); |
| 113 | + |
| 114 | +function getFace(string) { |
| 115 | + var hashString = function() { |
| 116 | + var hash = 0, i, chr, len; |
| 117 | + if (string.length == 0) return hash; |
| 118 | + for (i = 0, len = string.length; i < len; i++) { |
| 119 | + chr = string.charCodeAt(i); |
| 120 | + hash = ((hash << 5) - hash) + chr; |
| 121 | + hash |= 0; // Convert to 32bit integer |
| 122 | + } |
| 123 | + return hash; |
| 124 | + }; |
| 125 | + |
| 126 | + // Faces from uifaces.com. Thanks! |
| 127 | + var faces = [ |
| 128 | + "https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg", |
| 129 | + "https://s3.amazonaws.com/uifaces/faces/twitter/sauro/128.jpg", |
| 130 | + "https://s3.amazonaws.com/uifaces/faces/twitter/brad_frost/128.jpg", |
| 131 | + "https://s3.amazonaws.com/uifaces/faces/twitter/rem/128.jpg", |
| 132 | + "https://s3.amazonaws.com/uifaces/faces/twitter/pixeliris/128.jpg", |
| 133 | + "https://s3.amazonaws.com/uifaces/faces/twitter/csswizardry/128.jpg", |
| 134 | + ]; |
| 135 | + |
| 136 | + return faces[hashString(string) % faces.length]; |
| 137 | +} |
0 commit comments