--- url: "https://web.dev/articles/security-credential-management-save-forms" titulek: "Save Credentials from Forms | Articles | web.dev" h1: "Save Credentials from Forms Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/security-credential-management-save-forms" drobecky: ["Articles", "Save Credentials from Forms"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:05:27" znaku: 5200 slov: 659 kodovani: "iso-8859-2" sha256: "14ae538c0e4c85921de802eda20d48f2fc4e0f482d439ecb95bd8ab96405aba3" --- Save Credentials from Forms Stay organized with collections Save and categorize content based on your preferences. Eiji Kitamura Meggin Kearney Save Credentials from sign-in forms Keep your registration and sign-in forms as simple as possible. Save credentials from sign-in forms so users won't have to sign in again when they return. To store user credentials from forms: Include `autocomplete` in the form. Prevent the form from submitting. Authenticate by sending a request. Store the credential. Update the UI or proceed to the personalized page. Include `autocomplete` in the form Before moving forward, check if your form includes `autocomplete` attributes. This helps the Credential Management API find the `id` and `password` from the form and construct a credential object. This also helps browsers not supporting the Credential Management API to understand its semantics. Learn more about autofill in [this article](https://cloudfour.com/thinks/autofill-what-web-devs-should-know-but-dont/) by [Jason Grigsby](https://medium.com/@grigs). `
` Prevent the form from submitting When the user presses the submit button, prevent the form from submitting, which would otherwise result in a page transition: `var f = document.querySelector('#signup'); f.addEventListener('submit', e => { e.preventDefault();` By preventing a page transition, you can retain the credential information while verifying its authenticity. Authenticate by sending a request To authenticate the user, deliver credential information to your server using AJAX. On the server side, create an endpoint (or simply alter an existing endpoint) that responds with HTTP code 200 or 401, so that itâs clear to the browser whether the sign-up/sign-in/change password is successful or not. For example: `// Try sign-in with AJAX fetch('/signin', { method: 'POST', body: new FormData(e.target), credentials: 'include', });` Store the credential To store a credential, first check if the API is available, then instantiate a [`PasswordCredential`](https://developer.mozilla.org/docs/Web/API/PasswordCredential) with the form element as an argument either synchronously or asynchronously. Call [`navigator.credentials.store()`](https://developer.mozilla.org/docs/Web/API/CredentialsContainer/store). If the API is not available, you can simply forward the profile information to the next step. Synchronous example: `if (window.PasswordCredential) { var c = new PasswordCredential(e.target); return navigator.credentials.store(c); } else { return Promise.resolve(profile); }` Asynchronous example: `if (window.PasswordCredential) { var c = await navigator.credentials.create({password: e.target}); return navigator.credentials.store(c); } else { return Promise.resolve(profile); }` Once the request succeeds, store the credential information. (Don't store the credentials information if the request failed as doing so confuses returning users.) When the Chrome browser obtains credential information, a notification pops up asking to store a credential (or federation provider). Notification for an auto signed-in user Update the UI If everything went well, update the UI using the profile information, or proceed to the personalized page. `}).then(profile => { if (profile) { updateUI(profile); } }).catch(error => { showError('Sign-in Failed'); }); });` Full code example `// 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'); }); });` Browser compatibility `PasswordCredential` Browser Support 51 79 x x [Source](https://developer.mozilla.org/docs/Web/API/PasswordCredential) `navigator.credentials.store()` Browser Support 51 79 60 13 [Source](https://developer.mozilla.org/docs/Web/API/CredentialsContainer/store) Feedback Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2022-02-24 UTC.