Skip to content

Instantly share code, notes, and snippets.

@unders
Last active February 16, 2020 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unders/d22dae09e9bc11c30021b68bb207a763 to your computer and use it in GitHub Desktop.
Save unders/d22dae09e9bc11c30021b68bb207a763 to your computer and use it in GitHub Desktop.
JavaScript Recipes
// Try sign-in with AJAX
fetch('/signin', {
method: 'POST',
body: new FormData(e.target),
credentials: 'include'
})
// Get form's DOM object
var f = document.querySelector('#signup');
f.addEventListener('submit', e => {
// Stop submitting form by itself
e.preventDefault();
// Try sign-in with AJAX
fetch('/signin', {
method: 'POST',
body: new FormData(e.target),
credentials: 'include'
}).then(res => {
if (res.status == 200) {
return Promise.resolve();
} else {
return Promise.reject('Sign-in failed');
}
}).then(profile => {
// Instantiate PasswordCredential with the form
if (window.PasswordCredential) {
var c = new PasswordCredential(e.target);
return navigator.credentials.store(c);
} else {
return Promise.resolve(profile);
}
}).then(profile => {
// Successful sign-in
if (profile) {
updateUI(profile);
}
}).catch(error => {
// Sign-in failed
showError('Sign-in Failed');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment