PasswordCredential: PasswordCredential() constructor

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The PasswordCredential() constructor creates a new PasswordCredential object.

Syntax

js
new PasswordCredential(data)
new PasswordCredential(form)

Parameters

Either of the following:

data

An object with the following properties:

iconURL Optional

A string representing the URL of an icon or avatar to be associated with the credential.

id

A string representing the username portion of the username/password combination.

name Optional

A string representing a human-understandable name associated with the credential, intended to help the user select this credential in a user interface.

origin

A string representing the credential's origin. PasswordCredential objects are origin-bound, which means that they will only be usable on the specified origin they were intended to be used on.

password

A string representing the credential password.

form

A reference to an HTMLFormElement with appropriate input fields. The form should, at the very least, contain an id and password. It could also require a CSRF token.

Exceptions

TypeError

Thrown if one of the id, origin or password option is empty.

Examples

This example shows how to set up an HTMLFormElement to capture data which we'll use to create a PasswordCredential object.

Starting with the form element.

html
<form id="form" method="post">
  <label for="id">Username:</label>
  <input type="text" name="id" autocomplete="username" />
  <label for="password">Password:</label>
  <input type="password" name="password" autocomplete="current-password" />
  <input type="hidden" name="csrf_token" value="*****" />
</form>

Then, a reference to this form element, using it to create a PasswordCredential object, and storing it in the browser's password system.

js
const form = document.querySelector("#form");
const creds = new PasswordCredential(form);
// Store the credentials.
navigator.credentials.store(creds).then((creds) => {
  // Do something with the credentials if you need to.
});

Specifications

Specification
Credential Management Level 1
# dom-passwordcredential-passwordcredential
Credential Management Level 1
# dom-passwordcredential-passwordcredential-data

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
PasswordCredential() constructor
Experimental
Chrome – Full support
Chrome 51 (Release date: 2016-05-25)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 38 (Release date: 2016-06-08)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 51 (Release date: 2016-06-08)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 41 (Release date: 2016-10-25)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – Full support
WebView Android 51 (Release date: 2016-06-08)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.