Skip to content

Commit 846c04d

Browse files
committed
Merge branch 'master' of https://github.com/w3c/webappsec
2 parents f80b7ca + 68375b4 commit 846c04d

File tree

3 files changed

+681
-539
lines changed

3 files changed

+681
-539
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<style>
2+
button {
3+
display: block;
4+
}
5+
6+
div {
7+
display: none;
8+
}
9+
</style>
10+
<button id="signin-button">Sign In</button>
11+
<button id="signout-button">Sign Out</button>
12+
<div>
13+
<form id="signin">
14+
<h2>Sign In!</h2>
15+
<label for="username">Username:</label>
16+
<input id="username" name="username" autocomplete="username"></input>
17+
<label for="password">Password:</label>
18+
<input id="password" name="password" autocomplete="current-password"></input>
19+
<input type="submit">
20+
</form>
21+
<form id="signup">
22+
<h2>Sign Up!</h2>
23+
<label for="username-signup">Username:</label>
24+
<input id="username-signup" name="username" autocomplete="username"></input>
25+
<label for="password-signup">Password:</label>
26+
<input id="password-signup" name="password" autocomplete="new-password"></input>
27+
<input type="submit">
28+
</form>
29+
</div>
30+
<script>
31+
// Attempt to automatically sign a user in.
32+
if (navigator.credentials) {
33+
console.log("Automagical sign in?");
34+
navigator.credentials.get({
35+
'password': true,
36+
'suppressUI': true
37+
}).then(function (credential) {
38+
if (credential) {
39+
console.log("Got credentials! Signing in!");
40+
document.querySelector('button').innerText = "Signed in!";
41+
return;
42+
}
43+
44+
console.log("No credentials. Sadness abounds.");
45+
});
46+
}
47+
48+
// When a user clicks on the sign in button, grab a credential, or render a sign in form.
49+
document.querySelector('button').addEventListener('click', function () {
50+
if (navigator.credentials) {
51+
console.log("Manually-toggled credentials.get!");
52+
navigator.credentials.get({ 'password': true }).then(function (credential) {
53+
if (!credential) {
54+
console.log("Still no credentials. Moar sadness.");
55+
toggleSignInForm();
56+
return;
57+
}
58+
59+
console.log("Got credentials! Signing in!");
60+
document.querySelector('button').innerText = "Signing in!";
61+
});
62+
} else {
63+
toggleSignInForm();
64+
}
65+
});
66+
67+
function toggleSignInForm() {
68+
document.querySelector('button').style.display = 'none';
69+
document.querySelector('div').style.display = 'block';
70+
}
71+
72+
// Capture "Sign Up" calls, store credentials.
73+
document.querySelector('#signup').addEventListener('submit', function (e) {
74+
if (navigator.credentials) {
75+
console.log("Creating credentials!");
76+
var c;
77+
try {
78+
c = new PasswordCredential({
79+
id: document.querySelector('#username-signup').value,
80+
password: document.querySelector('#password-signup').value
81+
});
82+
} catch (e) {
83+
c = new PasswordCredential(
84+
document.querySelector('#username-signup').value,
85+
document.querySelector('#password-signup').value
86+
);
87+
}
88+
navigator.credentials.store(c).then(function () {
89+
console.log("Stored credential!");
90+
});
91+
e.preventDefault();
92+
return false;
93+
}
94+
});
95+
96+
// Capture "sign out" calls.
97+
document.querySelector('#signout-button').addEventListener('click', function () {
98+
navigator.credentials.requireUserMediation();
99+
});
100+
101+
[].forEach.call(document.querySelectorAll('input[type=submit]'), function(el) {
102+
});
103+
</script>

0 commit comments

Comments
 (0)