Skip to content

Instantly share code, notes, and snippets.

@mikewest
Last active April 12, 2016 18:14
Show Gist options
  • Save mikewest/9a03db99a2b2d71afb50b3634952f225 to your computer and use it in GitHub Desktop.
Save mikewest/9a03db99a2b2d71afb50b3634952f225 to your computer and use it in GitHub Desktop.
Credential Management API + `https://myaccount.nytimes.com`
// On https://myaccount.nytimes.com/mobile/login/smart/index.html
navigator.credentials.get({password: true}) // Include `{ ..., 'unmediated': true }` to grab credentials w/o chooser
.then(c => {
if (!c)
return;
c.additionalData = new FormData(document.querySelector('.loginForm'));
c.idName = "userid";
fetch('/mobile/login/smart/index.html', { credentials: c, method:'POST' })
.then(r => window.location = 'http://mobile.nytimes.com');
});
// Assuming that the sign-in form is mildly adjusted to include `autocomplete` attributes:
//
// <form ...>
// ...
// <input type="text" id="userid" name="userid" autocomplete="username" ...>
// ...
// <input type="password" id="password" name="password" autocomplete="current-password" ...>
// ...
// </form>
document.querySelector('input[name=username]').setAttribute('autocomplete', 'username');
document.querySelector('input[name=password]').setAttribute('autocomplete', 'current-password');
document.querySelector('.loginForm').addEventListener('submit', e => {
if (!navigator.credentials)
return;
e.preventDefault();
var c = new PasswordCredential(e.target);
fetch('/mobile/login/smart/index.html', { credentials: c, method: 'POST' })
.then(r => {
if (r.type == 'opaqueredirect') { // If we're redirected, yay! Store the credential.
navigator.credentials.store(c).then(_ => {
window.location = 'http://mobile.nytimes.com';
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment