PasswordCredential: hàm khởi tạo PasswordCredential()

Khả dụng hạn chế

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.

Hàm khởi tạo PasswordCredential() tạo một đối tượng PasswordCredential mới.

Cú pháp

js
new PasswordCredential(data)
new PasswordCredential(form)

Tham số

Một trong hai loại sau:

data

Một đối tượng với các thuộc tính sau:

iconURL Optional

Một chuỗi đại diện cho URL của biểu tượng hoặc avatar để liên kết với thông tin xác thực.

id

Một chuỗi đại diện cho phần tên đăng nhập của cặp tên đăng nhập/mật khẩu.

name Optional

Một chuỗi đại diện cho tên có thể hiểu được bởi con người liên kết với thông tin xác thực, nhằm giúp người dùng chọn thông tin xác thực này trong giao diện người dùng.

origin

Một chuỗi đại diện cho origin của thông tin xác thực. Các đối tượng PasswordCredential được gắn với origin, có nghĩa là chúng chỉ có thể sử dụng được trên origin được chỉ định mà chúng được thiết kế để sử dụng.

password

Một chuỗi đại diện cho mật khẩu thông tin xác thực.

form

Tham chiếu đến HTMLFormElement với các trường đầu vào phù hợp. Biểu mẫu ít nhất phải chứa id và mật khẩu. Nó cũng có thể yêu cầu CSRF token.

Ngoại lệ

TypeError

Được ném nếu một trong các tùy chọn id, origin hoặc password trống.

Ví dụ

Ví dụ này cho thấy cách thiết lập HTMLFormElement để thu thập dữ liệu mà chúng ta sẽ dùng để tạo đối tượng PasswordCredential.

Bắt đầu với phần tử biểu mẫu.

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>

Sau đó, tham chiếu đến phần tử biểu mẫu này, sử dụng nó để tạo đối tượng PasswordCredential, và lưu vào hệ thống mật khẩu của trình duyệt.

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.
});

Thông số kỹ thuật

Thông số kỹ thuật
Credential Management Level 1
# dom-passwordcredential-passwordcredential
Credential Management Level 1
# dom-passwordcredential-passwordcredential-data

Tương thích trình duyệt