@react-native-firebase/auth
Version:
React Native Firebase - The authentication module provides an easy-to-use API to integrate an authentication workflow into new and existing applications. React Native Firebase provides access to all Firebase authentication methods and identity providers.
1,413 lines (1,338 loc) • 88.7 kB
TypeScript
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { ReactNativeFirebase } from '@react-native-firebase/app';
/**
* Firebase Authentication package for React Native.
*
* #### Example: Access the firebase export from the `auth` package:
*
* ```js
* import { firebase } from '@react-native-firebase/auth';
*
* // firebase.auth().X
* ```
*
* #### Example: Using the default export from the `auth` package:
*
* ```js
* import auth from '@react-native-firebase/auth';
*
* // auth().X
* ```
*
* #### Example: Using the default export from the `app` package:
*
* ```js
* import firebase from '@react-native-firebase/app';
* import '@react-native-firebase/auth';
*
* // firebase.auth().X
* ```
* TODO @salakar @ehesp missing auth providers (PhoneAuthProvider, Facebook etc)
*
* @firebase auth
*/
export namespace FirebaseAuthTypes {
import FirebaseModule = ReactNativeFirebase.FirebaseModule;
import NativeFirebaseError = ReactNativeFirebase.NativeFirebaseError;
export interface NativeFirebaseAuthError extends NativeFirebaseError {
userInfo: {
/**
* When trying to sign in or link with an AuthCredential which was already associated with an account,
* you might receive an updated credential (depending of provider) which you can use to recover from the error.
*/
authCredential: AuthCredential | null;
/**
* When trying to sign in the user might be prompted for a second factor confirmation. Can
* can use this object to initialize the second factor flow and recover from the error.
*/
resolver: MultiFactorResolver | null;
};
}
/**
* Interface that represents the credentials returned by an auth provider. Implementations specify the details
* about each auth provider's credential requirements.
*
* TODO Missing; signInMethod, toJSON, fromJSON
*
* #### Example
*
* ```js
* const provider = firebase.auth.EmailAuthProvider;
* const authCredential = provider.credential('foo@bar.com', '123456');
*
* await firebase.auth().signInWithCredential(authCredential);
* ```
*/
export interface AuthCredential {
/**
* The authentication provider ID for the credential. For example, 'facebook.com', or 'google.com'.
*/
providerId: string;
token: string;
secret: string;
}
/**
* Interface that represents an auth provider. Implemented by other providers.
*/
export interface AuthProvider {
/**
* The provider ID of the provider.
*/
PROVIDER_ID: string;
/**
* Creates a new `AuthCredential`.
*
* @returns {@link auth.AuthCredential}.
* @param token A provider token.
* @param secret A provider secret.
*/
credential: (token: string | null, secret?: string) => AuthCredential;
}
/**
* Interface that represents an OAuth provider. Implemented by other providers.
*/
export interface OAuthProvider extends AuthProvider {
/**
* The provider ID of the provider.
* @param providerId
*/
// eslint-disable-next-line @typescript-eslint/no-misused-new
new (providerId: string): OAuthProvider;
/**
* Creates a new `AuthCredential`.
*
* @returns {@link auth.AuthCredential}.
* @param token A provider token.
* @param secret A provider secret.
*/
credential: (token: string | null, secret?: string) => AuthCredential;
/**
* Sets the OAuth custom parameters to pass in an OAuth request for sign-in
* operations.
*
* @remarks
* For a detailed list, check the reserved required OAuth 2.0 parameters such as `client_id`,
* `redirect_uri`, `scope`, `response_type`, and `state` are not allowed and will be ignored.
*
* @param customOAuthParameters - The custom OAuth parameters to pass in the OAuth request.
*/
setCustomParameters: (customOAuthParameters: Record<string, string>) => AuthProvider;
/**
* Retrieve the current list of custom parameters.
* @returns The current map of OAuth custom parameters.
*/
getCustomParameters: () => Record<string, string>;
/**
* Add an OAuth scope to the credential.
*
* @param scope - Provider OAuth scope to add.
*/
addScope: (scope: string) => AuthProvider;
/**
* Retrieve the current list of OAuth scopes.
*/
getScopes: () => string[];
}
/**
* Interface that represents an Open ID Connect auth provider. Implemented by other providers.
*/
export interface OIDCProvider {
/**
* The provider ID of the provider.
*/
PROVIDER_ID: string;
/**
* Creates a new `OIDCProvider`.
*
* @returns {@link auth.AuthCredential}.
* @param oidcSuffix this is the "Provider ID" value from the firebase console fx `azure_test`.
* @param token A provider token.
*/
credential: (oidcSuffix: string, idToken: string) => AuthCredential;
}
/**
* Email and password auth provider implementation.
*/
export interface EmailAuthProvider {
/**
* The provider ID. Always returns `password`.
*/
PROVIDER_ID: string;
/**
* This corresponds to the sign-in method identifier as returned in {@link auth#fetchSignInMethodsForEmail}.
*
* #### Example
*
* ```js
* const signInMethods = await firebase.auth().fetchSignInMethodsForEmail('...');
* if (signInMethods.indexOf(firebase.auth.EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD) != -1) {
* // User can sign in with email/link
* }
* ```
*/
EMAIL_LINK_SIGN_IN_METHOD: string;
/**
* This corresponds to the sign-in method identifier as returned in {@link auth#fetchSignInMethodsForEmail}.
*
* #### Example
*
* ```js
* const signInMethods = await firebase.auth().fetchSignInMethodsForEmail('...');
* if (signInMethods.indexOf(firebase.auth.EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD) != -1) {
* // User can sign in with email/password
* }
* ```
*/
EMAIL_PASSWORD_SIGN_IN_METHOD: string;
/**
* Returns the auth provider credential.
*
* #### Example
*
* ```js
* const authCredential = firebase.auth.EmailAuthProvider.credential('joe.bloggs@example.com', '123456');
* ```
*
* @returns {@link auth.AuthCredential}
* @param email Users email address.
* @param password User account password.
*/
credential: (email: string, password: string) => AuthCredential;
/**
* Initialize an `EmailAuthProvider` credential using an email and an email link after a sign in with email link operation.
*
* #### Example
*
* ```js
* const authCredential = firebase.auth.EmailAuthProvider.credentialWithLink('joe.bloggs@example.com', 'https://myexample.com/invite');
* ```
*
* @param email Users email address.
* @param emailLink Sign-in email link.
*/
credentialWithLink: (email: string, emailLink: string) => AuthCredential;
}
/**
*
*/
export interface PhoneAuthState {
/**
* SMS message with verification code sent to phone number.
*/
CODE_SENT: 'sent';
/**
* The timeout specified in {@link auth#verifyPhoneNumber} has expired.
*/
AUTO_VERIFY_TIMEOUT: 'timeout';
/**
* Phone number auto-verification succeeded.
*/
AUTO_VERIFIED: 'verified';
/**
* Phone number verification failed with an error.
*/
ERROR: 'error';
}
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface MultiFactorSession {
// this is has no documented contents, it is simply returned from some APIs and passed to others
}
export interface PhoneMultiFactorGenerator {
/**
* Identifies second factors of type phone.
*/
FACTOR_ID: FactorId.PHONE;
/**
* Build a MultiFactorAssertion to resolve the multi-factor sign in process.
*/
assertion(credential: AuthCredential): MultiFactorAssertion;
}
/**
* firebase.auth.X
*/
export interface Statics {
/**
* Return the #{@link MultiFactorUser} instance for the current user.
*/
multiFactor: multiFactor;
/**
* Try and obtain a #{@link MultiFactorResolver} instance based on an error.
* Returns null if no resolver object could be found.
*
*/
getMultiFactorResolver: getMultiFactorResolver;
/**
* Email and password auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.EmailAuthProvider;
* ```
*/
EmailAuthProvider: EmailAuthProvider;
/**
* Phone auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.PhoneAuthProvider;
* ```
*/
PhoneAuthProvider: AuthProvider;
/**
* Google auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.GoogleAuthProvider;
* ```
*/
GoogleAuthProvider: AuthProvider;
/**
* Apple auth provider implementation. Currently this is iOS only.
*
* For Apple Authentication please see our [`@invertase/react-native-apple-authentication`](https://github.com/invertase/react-native-apple-authentication) library which integrates well with Firebase and provides Firebase + Apple Auth examples.
*
* #### Example
*
* ```js
* firebase.auth.AppleAuthProvider;
* ```
*/
AppleAuthProvider: AuthProvider;
/**
* Github auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.GithubAuthProvider;
* ```
*/
GithubAuthProvider: AuthProvider;
/**
* Twitter auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.TwitterAuthProvider;
* ```
*/
TwitterAuthProvider: AuthProvider;
/**
* Facebook auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.FacebookAuthProvider;
* ```
*/
FacebookAuthProvider: AuthProvider;
/**
* Custom OAuth auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.OAuthProvider;
* ```
*/
OAuthProvider: OAuthProvider;
/**
* Custom Open ID connect auth provider implementation.
*
* #### Example
*
* ```js
* firebase.auth.OIDCAuthProvider;
* ```
*/
OIDCAuthProvider: OIDCProvider;
/**
* A PhoneAuthState interface.
*
* #### Example
*
* ```js
* firebase.auth.PhoneAuthState;
* ```
*/
PhoneAuthState: PhoneAuthState;
/**
* A PhoneMultiFactorGenerator interface.
*/
PhoneMultiFactorGenerator: PhoneMultiFactorGenerator;
}
/**
* A structure containing additional user information from a federated identity provider via {@link auth.UserCredential}.
*
* #### Example
*
* ```js
* const userCredential = await firebase.auth().signInAnonymously();
* console.log('Additional user info: ', userCredential.additionalUserInfo);
* ```
*
* @error auth/operation-not-allowed Thrown if anonymous accounts are not enabled. Enable anonymous accounts in the Firebase Console, under the Auth tab.
*/
export interface AdditionalUserInfo {
/**
* Returns whether the user is new or existing.
*/
isNewUser: boolean;
/**
* Returns an Object containing IDP-specific user data if the provider is one of Facebook,
* GitHub, Google, Twitter, Microsoft, or Yahoo.
*/
profile?: Record<string, any>;
/**
* Returns the provider ID for specifying which provider the information in `profile` is for.
*/
providerId: string;
/**
* Returns the username if the provider is GitHub or Twitter.
*/
username?: string;
}
/**
* A structure containing a User, an AuthCredential, the operationType, and any additional user
* information that was returned from the identity provider. operationType could be 'signIn' for
* a sign-in operation, 'link' for a linking operation and 'reauthenticate' for a re-authentication operation.
*
* TODO @salakar; missing credential, operationType
*/
export interface UserCredential {
/**
* Any additional user information assigned to the user.
*/
additionalUserInfo?: AdditionalUserInfo;
/**
* Returns the {@link auth.User} interface of this credential.
*/
user: User;
}
/**
* Holds the user metadata for the current {@link auth.User}.
*
* #### Example
*
* ```js
* const user = firebase.auth().currentUser;
* console.log('User metadata: ', user.metadata);
* ```
*/
export interface UserMetadata {
/**
* Returns the timestamp at which this account was created as dictated by the server clock
* as an ISO Date string.
*/
creationTime?: string;
/**
* Returns the last signin timestamp as dictated by the server clock as an ISO Date string.
* This is only accurate up to a granularity of 2 minutes for consecutive sign-in attempts.
*/
lastSignInTime?: string;
}
/**
* Identifies the type of a second factor.
*/
export enum FactorId {
PHONE = 'phone',
}
/**
* Contains information about a second factor.
*/
export type MultiFactorInfo = PhoneMultiFactorInfo | TotpMultiFactorInfo;
export interface PhoneMultiFactorInfo extends MultiFactorInfoCommon {
factorId: 'phone';
/**
* The phone number used for this factor.
*/
phoneNumber: string;
}
export interface TotpMultiFactorInfo extends MultiFactorInfoCommon {
factorId: 'totp';
}
export interface MultiFactorInfoCommon {
/**
* User friendly name for this factor.
*/
displayName?: string;
/**
* Time the second factor was enrolled, in UTC.
*/
enrollmentTime: string;
/**
* Unique id for this factor.
*/
uid: string;
}
export interface MultiFactorAssertion {
token: string;
secret: string;
}
export interface PhoneMultiFactorEnrollInfoOptions {
phoneNumber: string;
session: MultiFactorSession;
}
export interface PhoneMultiFactorSignInInfoOptions {
multiFactorHint?: MultiFactorInfo;
/**
* Unused in react-native-firebase ipmlementation
*/
multiFactorUid?: string;
session: MultiFactorSession;
}
/**
* Facilitates the recovery when a user needs to provide a second factor to sign-in.
*/
export interface MultiFactorResolver {
/**
* A list of enrolled factors that can be used to complete the multi-factor challenge.
*/
hints: MultiFactorInfo[];
/**
* Serialized session this resolver belongs to.
*/
session: MultiFactorSession;
/**
* For testing purposes only
*/
_auth?: FirebaseAuthTypes.Module;
/**
* Resolve the multi factor flow.
*/
resolveSignIn(assertion: MultiFactorAssertion): Promise<UserCredential>;
}
/**
* Try and obtain a #{@link MultiFactorResolver} instance based on an error.
* Returns null if no resolver object could be found.
*
* #### Example
*
* ```js
* const auth = firebase.auth();
* auth.signInWithEmailAndPassword(email, password).then((user) => {
* // signed in
* }).catch((error) => {
* if (error.code === 'auth/multi-factor-auth-required') {
* const resolver = getMultiFactorResolver(auth, error);
* }
* });
* ```
*/
export type getMultiFactorResolver = (
auth: FirebaseAuthTypes.Module,
error: unknown,
) => MultiFactorResolver | null;
/**
* The entry point for most multi-factor operations.
*/
export interface MultiFactorUser {
/**
* Returns the user's enrolled factors.
*/
enrolledFactors: MultiFactorInfo[];
/**
* Return the session for this user.
*/
getSession(): Promise<MultiFactorSession>;
/**
* Enroll an additional factor. Provide an optional display name that can be shown to the user.
* The method will ensure the user state is reloaded after successfully enrolling a factor.
*/
enroll(assertion: MultiFactorAssertion, displayName?: string): Promise<void>;
}
/**
* Return the #{@link MultiFactorUser} instance for the current user.
*/
export type multiFactor = (auth: FirebaseAuthTypes.Module) => Promise<MultiFactorUser>;
/**
* Holds information about the user's enrolled factors.
*
* #### Example
*
* ```js
* const user = firebase.auth().currentUser;
* console.log('User multi factors: ', user.multiFactor);
* ```
*/
export interface MultiFactor {
/**
* Returns the enrolled factors
*/
enrolledFactors: MultiFactorInfo[];
}
/**
* Represents a collection of standard profile information for a user. Can be used to expose
* profile information returned by an identity provider, such as Google Sign-In or Facebook Login.
*
* TODO @salakar: isEmailVerified
*
* #### Example
*
* ```js
* const user = firebase.auth().currentUser;
*
* user.providerData.forEach((userInfo) => {
* console.log('User info for provider: ', userInfo);
* });
* ```
*/
export interface UserInfo {
/**
* Returns the user's display name, if available.
*/
displayName?: string;
/**
* Returns the email address corresponding to the user's account in the specified provider, if available.
*/
email?: string;
/**
* The phone number normalized based on the E.164 standard (e.g. +16505550101) for the current user. This is null if the user has no phone credential linked to the account.
*/
phoneNumber?: string;
/**
* Returns a url to the user's profile picture, if available.
*/
photoURL?: string;
/**
* Returns the unique identifier of the provider type that this instance corresponds to.
*/
providerId: string;
/**
* Returns a string representing the multi-tenant tenant id. This is null if the user is not associated with a tenant.
*/
tenantId?: string;
/**
* Returns a user identifier as specified by the authentication provider.
*/
uid: string;
}
/**
* Interface representing ID token result obtained from {@link auth.User#getIdTokenResult}.
* It contains the ID token JWT string and other helper properties for getting different data
* associated with the token as well as all the decoded payload claims.
*
* TODO @salakar validate timestamp types
*
* #### Example
*
* ```js
* const idTokenResult = await firebase.auth().currentUser.getIdTokenResult();
* console.log('User JWT: ', idTokenResult.token);
* ```
*/
export interface IdTokenResult {
/**
* The Firebase Auth ID token JWT string.
*/
token: string;
/**
* The authentication time formatted as a UTC string. This is the time the user authenticated
* (signed in) and not the time the token was refreshed.
*/
authTime: string;
/**
* The ID token issued at time formatted as a UTC string.
*/
issuedAtTime: string;
/**
* The ID token expiration time formatted as a UTC string.
*/
expirationTime: string;
/**
* The sign-in provider through which the ID token was obtained (anonymous, custom,
* phone, password, etc). Note, this does not map to provider IDs.
*/
signInProvider: null | string;
/**
* The entire payload claims of the ID token including the standard reserved claims as well as
* the custom claims.
*/
claims: {
[key: string]: any;
};
}
/**
* Request used to update user profile information.
*
* #### Example
*
* ```js
* const update = {
* displayName: 'Alias',
* photoURL: 'https://my-cdn.com/assets/user/123.png',
* };
*
* await firebase.auth().currentUser.updateProfile(update);
* ```
*/
export interface UpdateProfile {
/**
* An optional display name for the user. Explicitly pass null to clear the displayName.
*/
displayName?: string | null;
/**
* An optional photo URL for the user. Explicitly pass null to clear the photoURL.
*/
photoURL?: string | null;
}
/**
* A result from a {@link auth#signInWithPhoneNumber} call.
*
* #### Example
*
* ```js
* // Force a new message to be sent
* const result = await firebase.auth().signInWithPhoneNumber('#4423456789');
* const user = await result.confirm('12345');
* ```
*/
export interface ConfirmationResult {
/**
* The phone number authentication operation's verification ID. This can be used along with
* the verification code to initialize a phone auth credential.
*/
verificationId: string | null;
/**
* Finishes the sign in flow. Validates a code that was sent to the users device.
*
* @param verificationCode The code sent to the users device from Firebase.
*/
confirm(verificationCode: string): Promise<UserCredential | null>;
}
/**
* Android specific options which can be attached to the {@link auth.ActionCodeSettings} object
* to be sent with requests such as {@link auth.User#sendEmailVerification}.
*
* #### Example
*
* ```js
* await firebase.auth().currentUser.sendEmailVerification({
* android: {
* installApp: true,
* packageName: 'com.awesome.app',
* },
* });
* ```
*/
export interface ActionCodeSettingsAndroid {
/**
* Sets the Android package name. This will try to open the link in an android app if it is installed.
*/
packageName: string;
/**
* If installApp is passed, it specifies whether to install the Android app if the device supports it and the app is not already installed. If this field is provided without a packageName, an error is thrown explaining that the packageName must be provided in conjunction with this field.
*/
installApp?: boolean;
/**
* If minimumVersion is specified, and an older version of the app is installed, the user is taken to the Play Store to upgrade the app. The Android app needs to be registered in the Console.
*/
minimumVersion?: string;
}
/**
* Additional data returned from a {@link auth#checkActionCode} call.
* For the PASSWORD_RESET, VERIFY_EMAIL, and RECOVER_EMAIL actions, this object contains an email field with the address the email was sent to.
* For the RECOVER_EMAIL action, which allows a user to undo an email address change, this object also contains a fromEmail field with the user account's new email address. After the action completes, the user's email address will revert to the value in the email field from the value in fromEmail field.
*
* #### Example
*
* ```js
* const actionCodeInfo = await firebase.auth().checkActionCode('ABCD');
*Data
* console.log('Action code email: ', actionCodeInfo.data.email);
* console.log('Action code from email: ', actionCodeInfo.data.fromEmail);
* ```
*/
export interface ActionCodeInfoData {
/**
* This signifies the email before the call was made.
*/
email?: string;
/**
* This signifies the current email associated with the account, which may have changed as a result of the {@link auth#checkActionCode} call performed.
*/
fromEmail?: string;
}
/**
* The interface returned from a {@link auth#checkActionCode} call.
*
* #### Example
*
* ```js
* const actionCodeInfo = await firebase.auth().checkActionCode('ABCD');
* console.log('Action code operation: ', actionCodeInfo.operation);
* ```
*/
export interface ActionCodeInfo {
/**
* The data associated with the action code.
*/
data: ActionCodeInfoData;
/**
* The operation from where the action originated.
*/
operation: 'PASSWORD_RESET' | 'VERIFY_EMAIL' | 'RECOVER_EMAIL' | 'EMAIL_SIGNIN' | 'ERROR';
}
/**
* iOS specific options which can be attached to the {@link auth.ActionCodeSettings} object
* to be sent with requests such as {@link auth.User#sendEmailVerification}.
*
* #### Example
*
* ```js
* await firebase.auth().currentUser.sendEmailVerification({
* iOS: {
* bundleId: '123456',
* },
* });
* ```
*/
export interface ActionCodeSettingsIos {
/**
* Sets the iOS bundle ID. This will try to open the link in an iOS app if it is installed. The iOS app needs to be registered in the Console.
*/
bundleId?: string;
}
/**
* Options to be sent with requests such as {@link auth.User#sendEmailVerification}.
*
* #### Example
*
* ```js
* await firebase.auth().currentUser.sendEmailVerification({
* handleCodeInApp: true,
* url: 'app/email-verification',
* });
* ```
*/
export interface ActionCodeSettings {
/**
* Android specific settings.
*/
android?: ActionCodeSettingsAndroid;
/**
* Whether the email action link will be opened in a mobile app or a web link first. The default is false. When set to true, the action code link will be be sent as a Universal Link or Android App Link and will be opened by the app if installed. In the false case, the code will be sent to the web widget first and then on continue will redirect to the app if installed.
*/
handleCodeInApp?: boolean;
/**
* iOS specific settings.
*/
iOS?: ActionCodeSettingsIos;
/**
* Sets the dynamic link domain (or subdomain) to use for the current link if it is to be opened using Firebase Dynamic Links. As multiple dynamic link domains can be configured per project, this field provides the ability to explicitly choose one. If none is provided, the first domain is used by default.
* Deprecated - use {@link ActionCodeSettings.linkDomain} instead.
*/
dynamicLinkDomain?: string;
/**
* This URL represents the state/Continue URL in the form of a universal link. This URL can should be constructed as a universal link that would either directly open the app where the action code would be handled or continue to the app after the action code is handled by Firebase.
*/
url: string;
/**
* Firebase Dynamic Links is deprecated and will be shut down as early as August * 2025.
* Instead, use ActionCodeSettings.linkDomain to set a a custom domain. Learn more at: https://firebase.google.com/support/dynamic-links-faq
*/
linkDomain?: string;
}
/**
* An auth listener callback function for {@link auth#onAuthStateChanged}.
*
* #### Example
*
* ```js
* function listener(user) {
* if (user) {
* // Signed in
* } else {
* // Signed out
* }
* }
*
* firebase.auth().onAuthStateChanged(listener);
* ```
*/
export type AuthListenerCallback = (user: User | null) => void;
/**
* A snapshot interface of the current phone auth state.
*
* #### Example
*
* ```js
* firebase.auth().verifyPhoneNumber('+4423456789')
* .on('state_changed', (phoneAuthSnapshot) => {
* console.log('Snapshot state: ', phoneAuthSnapshot.state);
* });
* ```
*/
export interface PhoneAuthSnapshot {
/**
* The current phone auth verification state.
*
* - `sent`: On iOS, this is the final event received. Once sent, show a visible input box asking the user to enter the verification code.
* - `timeout`: Auto verification has timed out. Show a visible input box asking the user to enter the verification code.
* - `verified`: The verification code has automatically been verified by the Android device. The snapshot contains the verification ID & code to create a credential.
* - `error`: An error occurred. Handle or allow the promise to reject.
*/
state: 'sent' | 'timeout' | 'verified' | 'error';
/**
* The verification ID to build a `PhoneAuthProvider` credential.
*/
verificationId: string;
/**
* The verification code. Will only be available if auto verification has taken place.
*/
code: string | null;
/**
* A native JavaScript error if an error occurs.
*/
error: NativeFirebaseError | null;
}
/**
* A custom error in the event verifying a phone number failed.
*
* #### Example
*
* ```js
* firebase.auth().verifyPhoneNumber('+4423456789')
* .on('state_changed', (phoneAuthSnapshot) => {
* console.log('Snapshot state: ', phoneAuthSnapshot.state);
* }, (phoneAuthError) => {
* console.error('Error: ', phoneAuthError.message);
* });
* ```
*/
export interface PhoneAuthError {
/**
* The code the verification failed with.
*/
code: string | null;
/**
* The verification ID which failed.
*/
verificationId: string;
/**
* JavaScript error message.
*/
message: string | null;
/**
* JavaScript error stack trace.
*/
stack: string | null;
}
/**
* The listener function returned from a {@link auth#verifyPhoneNumber} call.
*/
export interface PhoneAuthListener {
/**
* The phone auth state listener. See {@link auth.PhoneAuthState} for different event state types.
*
* #### Example
*
* ```js
* firebase.auth().verifyPhoneNumber('+4423456789')
* .on('state_changed', (phoneAuthSnapshot) => {
* console.log('State: ', phoneAuthSnapshot.state);
* }, (error) => {
* console.error(error);
* }, (phoneAuthSnapshot) => {
* console.log('Success');
* });
* ```
*
* @param event The event to subscribe to. Currently only `state_changed` is available.
* @param observer The required observer function. Returns a new phone auth snapshot on each event.
* @param errorCb An optional error handler function. This is not required if the `error` snapshot state is being handled in the `observer`.
* @param successCb An optional success handler function. This is not required if the `sent` or `verified` snapshot state is being handled in the `observer`.
*/
on(
event: string,
observer: (snapshot: PhoneAuthSnapshot) => void,
errorCb?: (error: PhoneAuthError) => void,
successCb?: (snapshot: PhoneAuthSnapshot) => void,
): PhoneAuthListener;
/**
* A promise handler called once the `on` listener flow has succeeded or rejected.
*
* #### Example
*
* ```js
* firebase.auth().verifyPhoneNumber('+4423456789')
* .on('state_changed', (phoneAuthSnapshot) => {
* if (phoneAuthSnapshot.state === firebase.auth.PhoneAuthState.CODE_SENT) {
* return Promise.resolve();
* } else {
* return Promise.reject(
* new Error('Code not sent!')
* );
* }
* })
* .then((phoneAuthSnapshot) => {
* console.log(phoneAuthSnapshot.state);
* }, (error) => {
* console.error(error.message);
* });
* ```
*
* @param onFulfilled Resolved promise handler.
* @param onRejected Rejected promise handler.
*/
then(
onFulfilled?: ((a: PhoneAuthSnapshot) => any) | null,
onRejected?: ((a: NativeFirebaseError) => any) | null,
): Promise<any>;
/**
* A promise handler called once the `on` listener flow has rejected.
*
* #### Example
*
* ```js
* firebase.auth().verifyPhoneNumber('+4423456789')
* .on('state_changed', (phoneAuthSnapshot) => {
* return Promise.reject(
* new Error('Code not sent!')
* );
* })
* .catch((error) => {
* console.error(error.message);
* });
* ```
*
* > Used when no `onRejected` handler is passed to {@link auth.PhoneAuthListener#then}.
*
* @param onRejected Rejected promise handler.
*/
catch(onRejected: (a: NativeFirebaseError) => any): Promise<any>;
}
/**
* Interface for module auth settings.
*
* #### Example
*
* ```js
* const settings = firebase.auth().settings;
* console.log(settings.appVerificationDisabledForTesting);
* ```
*/
export interface AuthSettings {
/**
* Forces application verification to use the web reCAPTCHA flow for Phone Authentication.
*
* Once this has been called, every call to PhoneAuthProvider#verifyPhoneNumber() will skip the Play Integrity API verification flow and use the reCAPTCHA flow instead.
*
* > Calling this method a second time will overwrite the previously passed parameter.
*
* @android
* @param appName
* @param forceRecaptchaFlow
* @param promise
*/
forceRecaptchaFlowForTesting: boolean;
/**
* Flag to disable app verification for the purpose of testing phone authentication. For this property to take effect, it needs to be set before rendering a reCAPTCHA app verifier. When this is disabled, a mock reCAPTCHA is rendered instead. This is useful for manual testing during development or for automated integration tests.
*
* > In order to use this feature, you will need to [whitelist your phone number](https://firebase.google.com/docs/auth/web/phone-auth#test-with-whitelisted-phone-numbers) via the Firebase Console.
*
* @param disabled Boolean value representing whether app verification should be disabled for testing.
*/
appVerificationDisabledForTesting: boolean;
/**
* Calling this method a second time will overwrite the previously passed parameters.
* Only one number can be configured at a given time.
*
* > The phone number and SMS code here must have been configured in the Firebase Console (Authentication > Sign In Method > Phone).
*
* #### Example
*
* ```js
* await firebase.auth().settings.setAutoRetrievedSmsCodeForPhoneNumber('+4423456789', 'ABCDE');
* ```
*
* @android
* @param phoneNumber The users phone number.
* @param smsCode The pre-set SMS code.
*/
setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber: string, smsCode: string): Promise<null>;
}
/**
* Represents a user's profile information in your Firebase project's user database. It also
* contains helper methods to change or retrieve profile information, as well as to manage that user's authentication state.
*
* #### Example 1
*
* Subscribing to the users authentication state.
*
* ```js
* firebase.auth().onAuthStateChanged((user) => {
* if (user) {
* console.log('User email: ', user.email);
* }
* });
* ```
*
* #### Example 2
*
* ```js
* const user = firebase.auth().currentUser;
*
* if (user) {
* console.log('User email: ', user.email);
* }
* ```
*/
export interface User {
/**
* The user's display name (if available).
*/
displayName: string | null;
/**
* - The user's email address (if available).
*/
email: string | null;
/**
* - True if the user's email address has been verified.
*/
emailVerified: boolean;
/**
* Returns true if the user is anonymous; that is, the user account was created with
* {@link auth#signInAnonymously} and has not been linked to another account
* with {@link auth#linkWithCredential}.
*/
isAnonymous: boolean;
/**
* Returns the {@link auth.UserMetadata} associated with this user.
*/
metadata: UserMetadata;
/**
* Returns the {@link auth.MultiFactor} associated with this user.
*/
multiFactor: MultiFactor | null;
/**
* Returns the phone number of the user, as stored in the Firebase project's user database,
* or null if none exists. This can be updated at any time by calling {@link auth.User#updatePhoneNumber}.
*/
phoneNumber: string | null;
/**
* The URL of the user's profile picture (if available).
*/
photoURL: string | null;
/**
* Additional provider-specific information about the user.
*/
providerData: UserInfo[];
/**
* The authentication provider ID for the current user.
* For example, 'facebook.com', or 'google.com'.
*/
providerId: string;
/**
* - The user's unique ID.
*/
uid: string;
/**
* Delete the current user.
*
* #### Example
*
* ```js
* await firebase.auth().currentUser.delete();
* ```
*
* @error auth/requires-recent-login Thrown if the user's last sign-in time does not meet the security threshold. Use `auth.User#reauthenticateWithCredential` to resolve. This does not apply if the user is anonymous.
*/
delete(): Promise<void>;
/**
* Returns the users authentication token.
*
* #### Example
*
* ```js
* // Force a token refresh
* const idToken = await firebase.auth().currentUser.getIdToken(true);
* ```
*
* @param forceRefresh A boolean value which forces Firebase to refresh the token.
*/
getIdToken(forceRefresh?: boolean): Promise<string>;
/**
* Returns a firebase.auth.IdTokenResult object which contains the ID token JWT string and
* other helper properties for getting different data associated with the token as well as
* all the decoded payload claims.
*
* #### Example
*
* ```js
* // Force a token refresh
* const idTokenResult = await firebase.auth().currentUser.getIdTokenResult(true);
* ```
*
* @param forceRefresh boolean Force refresh regardless of token expiration.
*/
getIdTokenResult(forceRefresh?: boolean): Promise<IdTokenResult>;
/**
* Link the user with a 3rd party credential provider.
*
* #### Example
*
* ```js
* const facebookCredential = firebase.auth.FacebookAuthProvider.credential('access token from Facebook');
* const userCredential = await firebase.auth().currentUser.linkWithCredential(facebookCredential);
* ```
*
* @error auth/provider-already-linked Thrown if the provider has already been linked to the user. This error is thrown even if this is not the same provider's account that is currently linked to the user.
* @error auth/invalid-credential Thrown if the provider's credential is not valid. This can happen if it has already expired when calling link, or if it used invalid token(s). See the Firebase documentation for your provider, and make sure you pass in the correct parameters to the credential method.
* @error auth/credential-already-in-use Thrown if the account corresponding to the credential already exists among your users, or is already linked to a Firebase User.
* @error auth/email-already-in-use Thrown if the email corresponding to the credential already exists among your users.
* @error auth/operation-not-allowed Thrown if you have not enabled the provider in the Firebase Console. Go to the Firebase Console for your project, in the Auth section and the Sign in Method tab and configure the provider.
* @error auth/invalid-email Thrown if the email used in a auth.EmailAuthProvider.credential is invalid.
* @error auth/wrong-password Thrown if the password used in a auth.EmailAuthProvider.credential is not correct or when the user associated with the email does not have a password.
* @error auth/invalid-verification-code Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification code of the credential is not valid.
* @error auth/invalid-verification-id Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification ID of the credential is not valid.
* @throws on iOS {@link auth.NativeFirebaseAuthError}, on Android {@link auth.NativeFirebaseError}
* @param credential A created {@link auth.AuthCredential}.
*/
linkWithCredential(credential: AuthCredential): Promise<UserCredential>;
/**
* Link the user with a federated 3rd party credential provider (Microsoft, Yahoo).
* The APIs here are the web-compatible linkWithPopup and linkWithRedirect but both
* share the same underlying native SDK behavior and may be used interchangably.
*
* #### Example
*
* ```js
* const provider = new firebase.auth.OAuthProvider('microsoft.com');
* const userCredential = await firebase.auth().currentUser.linkWithPopup(provider);
* ```
*
* @error auth/provider-already-linked Thrown if the provider has already been linked to the user. This error is thrown even if this is not the same provider's account that is currently linked to the user.
* @error auth/invalid-credential Thrown if the provider's credential is not valid. This can happen if it has already expired when calling link, or if it used invalid token(s). See the Firebase documentation for your provider, and make sure you pass in the correct parameters to the credential method.
* @error auth/credential-already-in-use Thrown if the account corresponding to the credential already exists among your users, or is already linked to a Firebase User.
* @error auth/email-already-in-use Thrown if the email corresponding to the credential already exists among your users.
* @error auth/operation-not-allowed Thrown if you have not enabled the provider in the Firebase Console. Go to the Firebase Console for your project, in the Auth section and the Sign in Method tab and configure the provider.
* @error auth/invalid-email Thrown if the email used in a auth.EmailAuthProvider.credential is invalid.
* @error auth/wrong-password Thrown if the password used in a auth.EmailAuthProvider.credential is not correct or when the user associated with the email does not have a password.
* @error auth/invalid-verification-code Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification code of the credential is not valid.
* @error auth/invalid-verification-id Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification ID of the credential is not valid.
* @throws on iOS {@link auth.NativeFirebaseAuthError}, on Android {@link auth.NativeFirebaseError}
* @param provider A created {@link auth.AuthProvider}.
*/
linkWithPopup(provider: AuthProvider): Promise<UserCredential>;
/**
* Link the user with a federated 3rd party credential provider (Microsoft, Yahoo).
* The APIs here are the web-compatible linkWithPopup and linkWithRedirect but both
* share the same underlying native SDK behavior and may be used interchangably.
*
* #### Example
*
* ```js
* const provider = new firebase.auth.OAuthProvider('microsoft.com');
* const userCredential = await firebase.auth().currentUser.linkWithRedirect(provider);
* ```
*
* @error auth/provider-already-linked Thrown if the provider has already been linked to the user. This error is thrown even if this is not the same provider's account that is currently linked to the user.
* @error auth/invalid-credential Thrown if the provider's credential is not valid. This can happen if it has already expired when calling link, or if it used invalid token(s). See the Firebase documentation for your provider, and make sure you pass in the correct parameters to the credential method.
* @error auth/credential-already-in-use Thrown if the account corresponding to the credential already exists among your users, or is already linked to a Firebase User.
* @error auth/email-already-in-use Thrown if the email corresponding to the credential already exists among your users.
* @error auth/operation-not-allowed Thrown if you have not enabled the provider in the Firebase Console. Go to the Firebase Console for your project, in the Auth section and the Sign in Method tab and configure the provider.
* @error auth/invalid-email Thrown if the email used in a auth.EmailAuthProvider.credential is invalid.
* @error auth/wrong-password Thrown if the password used in a auth.EmailAuthProvider.credential is not correct or when the user associated with the email does not have a password.
* @error auth/invalid-verification-code Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification code of the credential is not valid.
* @error auth/invalid-verification-id Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification ID of the credential is not valid.
* @throws on iOS {@link auth.NativeFirebaseAuthError}, on Android {@link auth.NativeFirebaseError}
* @param provider A created {@link auth.AuthProvider}.
*/
linkWithRedirect(provider: AuthProvider): Promise<UserCredential>;
/**
* Re-authenticate a user with a third-party authentication provider.
*
* #### Example
*
* ```js
* const facebookCredential = firebase.auth.FacebookAuthProvider.credential('access token from Facebook');
* const userCredential = await firebase.auth().currentUser.reauthenticateWithCredential(facebookCredential);
* ```
*
* @error auth/user-mismatch Thrown if the credential given does not correspond to the user.
* @error auth/user-not-found Thrown if the credential given does not correspond to any existing user.
* @error auth/invalid-credential Thrown if the provider's credential is not valid. This can happen if it has already expired when calling link, or if it used invalid token(s). See the Firebase documentation for your provider, and make sure you pass in the correct parameters to the credential method.
* @error auth/invalid-email Thrown if the email used in a auth.EmailAuthProvider.credential is invalid.
* @error auth/wrong-password Thrown if the password used in a auth.EmailAuthProvider.credential is not correct or when the user associated with the email does not have a password.
* @error auth/invalid-verification-code Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification code of the credential is not valid.
* @error auth/invalid-verification-id Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification ID of the credential is not valid.
* @param credential A created {@link auth.AuthCredential}.
*/
reauthenticateWithCredential(credential: AuthCredential): Promise<UserCredential>;
/**
* Re-authenticate a user with a federated authentication provider (Microsoft, Yahoo). For native platforms, this will open a browser window.
* #### Example
*
* ```js
* const provider = new firebase.auth.OAuthProvider('microsoft.com');
* const userCredential = await firebase.auth().currentUser.reauthenticateWithProvider(provider);
* ```
*
* @error auth/user-mismatch Thrown if the credential given does not correspond to the user.
* @error auth/user-not-found Thrown if the credential given does not correspond to any existing user.
* @error auth/invalid-credential Thrown if the provider's credential is not valid. This can happen if it has already expired when calling link, or if it used invalid token(s). See the Firebase documentation for your provider, and make sure you pass in the correct parameters to the credential method.
* @error auth/invalid-email Thrown if the email used in a auth.EmailAuthProvider.credential is invalid.
* @error auth/wrong-password Thrown if the password used in a auth.EmailAuthProvider.credential is not correct or when the user associated with the email does not have a password.
* @error auth/invalid-verification-code Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification code of the credential is not valid.
* @error auth/invalid-verification-id Thrown if the credential is a auth.PhoneAuthProvider.credential and the verification ID of the credential is not valid.
* @param provider A created {@link auth.AuthProvider}.
* @returns A promise that resolves with no value.
*/
reauthenticateWithRedirect(provider: AuthProvider): Promise<void>;
/**
* Re-authenticate a user with a federated authentication provider (Microsoft, Yahoo). For native platforms, this will open a browser window.
* pop-up equivalent on native platforms.
*
* @param provider - The auth provider.
* @returns A promise that resolves with the user credentials.
*/
reauthenticateWithPopup(provider: AuthProvider): Promise<UserCredential>;
/**
* Refreshes the current user.
*
* #### Example
*
* ```js
* await firebase.auth().currentUser.reload();
* ```
*/
reload(): Promise<void>;
/**
* Sends a verification email to a user.
*
* #### Example
*
* ```js
* await firebase.auth().currentUser.sendEmailVerification({
* handleCodeInApp: true,
* });
* ```
*
* > This will Promise reject if the user is anonymous.
*
* @error auth/missing-android-pkg-name An Android package name must be provided if the Android app is required to be installed.
* @error auth/missing-continue-uri A continue URL must be provided in the request.
* @error auth/mi