PhoneAuthProvider class

Nhà cung cấp để tạo PhoneAuthCredential .

PhoneAuthProvider không hoạt động trong môi trường Node.js.

Chữ ký:

export declare class PhoneAuthProvider 

nhà xây dựng

Người xây dựng sửa đổi Sự miêu tả
(hàm tạo)(auth) Xây dựng một phiên bản mới của lớp PhoneAuthProvider

Của cải

Tài sản sửa đổi Kiểu Sự miêu tả
PHONE_SIGN_IN_METHOD static 'điện thoại' Luôn đặt thành SignInMethod .ĐIỆN THOẠI.
PROVIDER_ID static 'điện thoại' Luôn đặt thành ProviderId .ĐIỆN THOẠI.
id nhà cung cấp "điện thoại" Luôn đặt thành ProviderId .ĐIỆN THOẠI.

phương pháp

Phương pháp sửa đổi Sự miêu tả
thông tin xác thực (Id xác minh, Mã xác minh) static Tạo thông tin xác thực xác thực điện thoại, được cung cấp ID xác minh từ PhoneAuthProvider.verifyPhoneNumber() và mã được gửi tới thiết bị di động của người dùng.
thông tin xác thựcFromError(lỗi) static Trả về AuthCredential khi xảy ra lỗi.
thông tin xác thựcFromResult(userCredential) static Tạo AuthCredential từ UserCredential .
verifyPhoneNumber(phoneOptions, applicationVerifier) Bắt đầu quy trình xác thực số điện thoại bằng cách gửi mã xác minh đến số điện thoại đã cho.

PhoneAuthProvider.(hàm tạo)

Xây dựng một phiên bản mới của lớp PhoneAuthProvider

Chữ ký:

constructor(auth: Auth);

Thông số

Tham số Kiểu Sự miêu tả
xác thực Xác thực Phiên bản Firebase Auth mà hoạt động đăng nhập sẽ diễn ra.

PhoneAuthProvider.PHONE_SIGN_IN_METHOD

Luôn đặt thành SignInMethod .ĐIỆN THOẠI.

Chữ ký:

static readonly PHONE_SIGN_IN_METHOD: 'phone';

PhoneAuthProvider.PROVIDER_ID

Luôn đặt thành ProviderId .ĐIỆN THOẠI.

Chữ ký:

static readonly PROVIDER_ID: 'phone';

PhoneAuthProvider.providerId

Luôn đặt thành ProviderId .ĐIỆN THOẠI.

Chữ ký:

readonly providerId: "phone";

PhoneAuthProvider.credential()

Tạo thông tin xác thực xác thực điện thoại, được cung cấp ID xác minh từ PhoneAuthProvider.verifyPhoneNumber() và mã được gửi tới thiết bị di động của người dùng.

Chữ ký:

static credential(verificationId: string, verificationCode: string): PhoneAuthCredential;

Thông số

Tham số Kiểu Sự miêu tả
ID xác minh sợi dây ID xác minh được trả về từ PhoneAuthProvider.verifyPhoneNumber() .
mã xác nhận sợi dây Mã xác minh được gửi tới thiết bị di động của người dùng.

Trả về:

Thông tin xác thực điện thoại

Thông tin xác thực của nhà cung cấp xác thực.

ví dụ 1

const provider = new PhoneAuthProvider(auth);
const verificationId = provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = signInWithCredential(auth, authCredential);

Ví dụ 2

Một luồng thay thế được cung cấp bằng phương thức signInWithPhoneNumber .

const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const userCredential = await confirmationResult.confirm(verificationCode);

PhoneAuthProvider.credentialFromError()

Trả về AuthCredential khi xảy ra lỗi.

Phương pháp này hoạt động với các lỗi như auth/account-exists-with-different-credentials . Điều này hữu ích cho việc khôi phục khi cố gắng đặt số điện thoại của người dùng nhưng số được đề cập đã được liên kết với một tài khoản khác. Ví dụ: đoạn mã sau cố gắng cập nhật số điện thoại của người dùng hiện tại và nếu không thành công, hãy liên kết người dùng với tài khoản được liên kết với số đó:

const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber(number, verifier);
try {
  const code = ''; // Prompt the user for the verification code
  await updatePhoneNumber(
      auth.currentUser,
      PhoneAuthProvider.credential(verificationId, code));
} catch (e) {
  if ((e as FirebaseError)?.code === 'auth/account-exists-with-different-credential') {
    const cred = PhoneAuthProvider.credentialFromError(e);
    await linkWithCredential(auth.currentUser, cred);
  }
}

// At this point, auth.currentUser.phoneNumber === number.

Chữ ký:

static credentialFromError(error: FirebaseError): AuthCredential | null;

Thông số

Tham số Kiểu Sự miêu tả
lỗi FirebaseLỗi Lỗi tạo thông tin xác thực từ đó.

Trả về:

Xác thực | vô giá trị

PhoneAuthProvider.credentialFromResult()

Tạo AuthCredential từ UserCredential .

Chữ ký:

static credentialFromResult(userCredential: UserCredential): AuthCredential | null;

Thông số

Tham số Kiểu Sự miêu tả
thông tin người dùng Thông tin người dùng Thông tin xác thực của người dùng.

Trả về:

Xác thực | vô giá trị

PhoneAuthProvider.verifyPhoneNumber()

Bắt đầu quy trình xác thực số điện thoại bằng cách gửi mã xác minh đến số điện thoại đã cho.

Chữ ký:

verifyPhoneNumber(phoneOptions: PhoneInfoOptions | string, applicationVerifier: ApplicationVerifier): Promise<string>;

Thông số

Tham số Kiểu Sự miêu tả
tùy chọn điện thoại Tùy chọn thông tin điện thoại | sợi dây
trình xác minh ứng dụng Trình xác minh ứng dụng Để ngăn chặn lạm dụng, phương pháp này cũng yêu cầu Trình xác minh ứng dụng . SDK này bao gồm triển khai dựa trên reCAPTCHA, RecaptchaVerifier .

Trả về:

Hứa<string>

Lời hứa về ID xác minh có thể được chuyển tới PhoneAuthProvider.credential() để xác định luồng này..

ví dụ 1

const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = await signInWithCredential(auth, authCredential);

Ví dụ 2

Một luồng thay thế được cung cấp bằng phương thức signInWithPhoneNumber .

const confirmationResult = signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const userCredential = confirmationResult.confirm(verificationCode);

Ví dụ

// 'recaptcha-container' is the ID of an element in the DOM.
const applicationVerifier = new RecaptchaVerifier('recaptcha-container');
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber('+16505550101', applicationVerifier);
// Obtain the verificationCode from the user.
const phoneCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = await signInWithCredential(auth, phoneCredential);