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 とユーザーのモバイル デバイスに送信されたコードを指定して、電話認証資格情報を作成します。 |
credentialFromError(エラー) | static | エラーが渡された場合はAuthCredentialを返します。 |
credentialFromResult(userCredential) | static | UserCredentialから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;
パラメーター
パラメータ | タイプ | 説明 |
---|---|---|
エラー | FirebaseError | 認証情報を生成するためのエラー。 |
戻り値:
認証資格情報|ヌル
PhoneAuthProvider.credentialFromResult()
UserCredentialからAuthCredentialを生成します。
サイン:
static credentialFromResult(userCredential: UserCredential): AuthCredential | null;
パラメーター
パラメータ | タイプ | 説明 |
---|---|---|
ユーザー資格情報 | ユーザー資格情報 | ユーザーの資格情報。 |
戻り値:
認証資格情報|ヌル
PhoneAuthProvider.verifyPhoneNumber()
指定された電話番号に確認コードを送信して、電話番号認証フローを開始します。
サイン:
verifyPhoneNumber(phoneOptions: PhoneInfoOptions | string, applicationVerifier: ApplicationVerifier): Promise<string>;
パラメーター
パラメータ | タイプ | 説明 |
---|---|---|
電話オプション | 電話情報オプション|弦 | |
アプリケーション検証者 | アプリケーション検証者 | 悪用防止のため、このメソッドにはApplicationVerifierも必要です。この SDK には、reCAPTCHA ベースの実装であるRecaptchaVerifier が含まれています。 |
戻り値:
約束<文字列>
このフローを識別するためにPhoneAuthProvider.credential()に渡すことができる検証 ID の Promise。
例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);