PhoneAuthProvider class

用於產生PhoneAuthCredential 的提供程序

PhoneAuthProvider不適用於 Node.js 環境。

簽名:

export declare class PhoneAuthProvider 

建構函數

建構函數修飾符描述
(構造函數)(授權)建構PhoneAuthProvider類別的新實例

特性

財產修飾符類型描述
PHONE_SIGN_IN_METHOD static '電話'始終設定為SignInMethod 。電話。
提供者_ID static '電話'始終設定為ProviderId 。電話。
提供者ID “電話”始終設定為ProviderId 。電話。

方法

方法修飾符描述
憑證(驗證ID,驗證碼) static根據PhoneAuthProvider.verifyPhoneNumber()的驗證 ID 和傳送到使用者行動裝置的程式碼,建立電話身分驗證憑證。
憑證來自錯誤(錯誤) static傳遞錯誤時回傳AuthCredential
憑證來自結果(使用者憑證) staticUserCredential產生AuthCredential
verifyPhoneNumber(phoneOptions, applicationVerifier)透過向給定電話號碼發送驗證碼來啟動電話號碼身份驗證流程。

PhoneAuthProvider.(建構子)

建構PhoneAuthProvider類別的新實例

簽名:

constructor(auth: Auth);

參數

範圍類型描述
授權授權應在其中進行登入的 Firebase Auth實例。

PhoneAuthProvider.PHONE_SIGN_IN_METHOD

始終設定為SignInMethod 。電話。

簽名:

static readonly PHONE_SIGN_IN_METHOD: 'phone';

PhoneAuthProvider.PROVIDER_ID

始終設定為ProviderId 。電話。

簽名:

static readonly PROVIDER_ID: 'phone';

PhoneAuthProvider.providerId

始終設定為ProviderId 。電話。

簽名:

readonly providerId: "phone";

PhoneAuthProvider.credential()

根據PhoneAuthProvider.verifyPhoneNumber()的驗證 ID 和傳送到使用者行動裝置的程式碼,建立電話身分驗證憑證。

簽名:

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

參數

範圍類型描述
驗證ID細繩PhoneAuthProvider.verifyPhoneNumber()傳回的驗證 ID
驗證碼細繩發送到用戶行動裝置的驗證碼。

返回:

電話驗證憑證

身份驗證提供者憑證。

實施例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);

實施例2

使用signInWithPhoneNumber方法提供了替代流程。

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

PhoneAuthProvider.credentialFromError()

傳遞錯誤時回傳AuthCredential

此方法適用於auth/account-exists-with-different-credentials錯誤。當嘗試設定使用者的電話號碼但相關號碼已與另一個帳戶綁定時,這對於恢復非常有用。例如,以下程式碼嘗試更新目前使用者的電話號碼,如果失敗,則將該使用者連結到與該號碼關聯的帳戶:

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.

簽名:

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

參數

範圍類型描述
錯誤Firebase錯誤產生憑證時出現錯誤。

返回:

驗證憑證|無效的

PhoneAuthProvider.credentialFromResult()

UserCredential產生AuthCredential

簽名:

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

參數

範圍類型描述
使用者憑證使用者憑證用戶憑證。

返回:

驗證憑證|無效的

PhoneAuthProvider.verifyPhoneNumber()

透過向給定電話號碼發送驗證碼來啟動電話號碼身份驗證流程。

簽名:

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

參數

範圍類型描述
電話選項電話資訊選項|細繩
應用程式驗證器應用驗證器為了防止濫用,這個方法還需要一個ApplicationVerifier 。該 SDK 包括基於 reCAPTCHA 的實作RecaptchaVerifier

返回:

承諾<字串>

驗證 ID 的 Promise,可以傳遞給PhoneAuthProvider.credential()以識別此流程。

實施例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);

實施例2

使用signInWithPhoneNumber方法提供了替代流程。

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

例子

// '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);