docs.rodeo

MDN Web Docs mirror

CredentialsContainer: create() method

{{APIRef("Credential Management API")}} {{SecureContext_Header}} 

The create() method of the {{domxref("CredentialsContainer")}}  interface creates a new {{glossary("credential")}} , which can then be stored and later retrieved using the {{domxref("CredentialsContainer.get", "navigator.credentials.get()")}}  method. The retrieved credential can then be used by a website to authenticate a user.

This method supports three different types of credential:

Note that the Federated Credential Management API (FedCM) supersedes the federated credential type.

Syntax

create()
create(options)

Parameters

Return value

A {{jsxref("Promise")}}  that resolves with one of the following:

If no credential object can be created, the promise resolves with null.

Exceptions

Examples

Creating a password credential

This example creates a password credential from a {{domxref("PasswordCredentialInit")}}  object.

const credInit = {
  id: "1234",
  name: "Serpentina",
  origin: "https://example.org",
  password: "the last visible dog",
};

const makeCredential = document.querySelector("#make-credential");

makeCredential.addEventListener("click", async () => {
  const cred = await navigator.credentials.create({
    password: credInit,
  });
  console.log(cred.name);
  // Serpentina
  console.log(cred.password);
  // the last visible dog
});

Creating a federated credential

This example creates a federated credential from a {{domxref("FederatedCredentialInit")}}  object.

const credInit = {
  id: "1234",
  name: "Serpentina",
  origin: "https://example.org",
  protocol: "openidconnect",
  provider: "https://provider.example.org",
};

const makeCredential = document.querySelector("#make-credential");

makeCredential.addEventListener("click", async () => {
  const cred = await navigator.credentials.create({
    federated: credInit,
  });
  console.log(cred.name);
  console.log(cred.provider);
});

Creating a public key credential

This example creates a public key credential from a {{domxref("PublicKeyCredentialCreationOptions")}}  object.

const publicKey = {
  challenge: challengeFromServer,
  rp: { id: "acme.com", name: "ACME Corporation" },
  user: {
    id: new Uint8Array([79, 252, 83, 72, 214, 7, 89, 26]),
    name: "jamiedoe",
    displayName: "Jamie Doe",
  },
  pubKeyCredParams: [{ type: "public-key", alg: -7 }],
};

const publicKeyCredential = await navigator.credentials.create({ publicKey });

The create() call, if successful, returns a promise that resolves with a {{domxref("PublicKeyCredential")}}  object instance, representing a public key credential that can later be used to authenticate a user via a WebAuthn {{domxref("CredentialsContainer.get()", "get()")}}  call. Its {{domxref("PublicKeyCredential.response")}}  property contains an {{domxref("AuthenticatorAttestationResponse")}}  object providing access to several useful pieces of information including the authenticator data, public key, transport mechanisms, and more.

navigator.credentials.create({ publicKey }).then((publicKeyCredential) => {
  const response = publicKeyCredential.response;

  // Access attestationObject ArrayBuffer
  const attestationObj = response.attestationObject;

  // Access client JSON
  const clientJSON = response.clientDataJSON;

  // Return authenticator data ArrayBuffer
  const authenticatorData = response.getAuthenticatorData();

  // Return public key ArrayBuffer
  const pk = response.getPublicKey();

  // Return public key algorithm identifier
  const pkAlgo = response.getPublicKeyAlgorithm();

  // Return permissible transports array
  const transports = response.getTransports();
});

Some of this data will need to be stored on the server for future authentication operations against this credential — for example the public key, the algorithm used, and the permissible transports.

[!NOTE] See Creating a key pair and registering a user for more information about how the overall flow works.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN