From 2611f7c8e3d1977dbef930bdc692d8ea246db3e5 Mon Sep 17 00:00:00 2001 From: Daniel W Date: Thu, 18 Jan 2018 17:05:44 +0100 Subject: [PATCH] [typings][typescript] update typings for the auth module --- lib/index.d.ts | 176 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 140 insertions(+), 36 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 628a806f..8d300c6f 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -7,7 +7,7 @@ declare module "react-native-firebase" { type AuthProvider = { PROVIDER_ID: string, - credential: (token: string, secret?: string) => object, + credential: (token: string, secret?: string) => AuthCredential, }; export default class FireBase { @@ -533,6 +533,20 @@ declare module "react-native-firebase" { [key: string]: any; } + type UserInfo = { + displayName?: string, + email?: string, + phoneNumber?: string, + photoURL?: string, + providerId: string, + uid: string, + } + + type UpdateProfile = { + displayName?: string, + photoURL?: string, + } + interface User { /** * The user's display name (if available). @@ -550,6 +564,8 @@ declare module "react-native-firebase" { * */ isAnonymous: boolean + + phoneNumber: string | null /** * - The URL of the user's profile picture (if available). */ @@ -557,12 +573,12 @@ declare module "react-native-firebase" { /** * - Additional provider-specific information about the user. */ - providerData: any | null + providerData: Array /** * - The authentication provider ID for the current user. * For example, 'facebook.com', or 'google.com'. */ - providerId: string | null + providerId: string /** * - The user's unique ID. */ @@ -575,18 +591,20 @@ declare module "react-native-firebase" { /** * Returns the users authentication token. + * + * @param forceRefresh: boolean - default to false */ - getToken(): Promise - - /** - * Reauthenticate the current user with credentials: - */ - reauthenticate(credential: Credential): Promise + getIdToken(forceRefresh: boolean?): Promise /** * Link the user with a 3rd party credential provider. */ - linkWithCredential(credential: Credential): Promise + linkWithCredential(credential: AuthCredential): Promise + + /** + * Re-authenticate a user with a third-party authentication provider + */ + reauthenticateWithCredential(credential: AuthCredential): Promise /** * Refreshes the current user. @@ -597,7 +615,11 @@ declare module "react-native-firebase" { * Sends a verification email to a user. * This will Promise reject is the user is anonymous. */ - sendEmailVerification(): Promise + sendEmailVerification(actionCodeSettings?: ActionCodeSettings): Promise + + toJSON(): object + + unlink(providerId: string): Promise /** * Updates the user's email address. @@ -617,16 +639,28 @@ declare module "react-native-firebase" { * Updates a user's profile data. * Profile data should be an object of fields to update: */ - updateProfile(profile: Object): Promise + updateProfile(updates: UpdateProfile): Promise } /** 3rd party provider Credentials */ - interface Credential { - provider: string, + type AuthCredential { + providerId: string, token: string, secret: string } + type ActionCodeSettings = { + android: { + installApp?: boolean, + minimumVersion?: string, + packageName: string, + }, + handleCodeInApp?: boolean, + iOS: { + bundleId?: string, + }, + url: string, + } interface ActionCodeInfo { email: string, @@ -637,17 +671,55 @@ declare module "react-native-firebase" { passwordReset: string } + interface ConfirmationResult { + + confirm(verificationCode: string): Promise; + + verificationId: string | null; + } + + type PhoneAuthSnapshot = { + state: 'sent' | 'timeout' | 'verified' | 'error', + verificationId: string, + code: string | null, + error: Error | null, + }; + + type PhoneAuthError = { + code: string | null, + verificationId: string, + message: string | null, + stack: string | null, + }; + + interface PhoneAuthListener { + + on(event: string, + observer: () => PhoneAuthSnapshot, + errorCb?: () => PhoneAuthError, + successCb?: () => PhoneAuthSnapshot): PhoneAuthListener; + + then(fn: () => PhoneAuthSnapshot): Promise + + catch(fn: () => Error): Promise + } + namespace auth { + type AuthResult = { + authenticated: boolean, + user: object | null + } | null; + interface Auth { /** * Returns the current Firebase authentication state. */ - authenticated: boolean; + authResult: AuthResult | null; /** * Returns the currently signed-in user (or null). See the User class documentation for further usage. */ - currentUser: User | null + user: User | null /** * Gets/Sets the language for the app instance @@ -659,8 +731,30 @@ declare module "react-native-firebase" { * This method returns a unsubscribe function to stop listening to events. * Always ensure you unsubscribe from the listener when no longer needed to prevent updates to components no longer in use. */ - onAuthStateChanged(nextOrObserver: Object, error?: (a: RnError) => any, - completed?: () => any): () => any; + onAuthStateChanged(listener: Function): () => void; + + /** + * Listen for changes in id token. + * This method returns a unsubscribe function to stop listening to events. + * Always ensure you unsubscribe from the listener when no longer needed to prevent updates to components no longer in use. + */ + onIdTokenChanged(listener: Function): () => void; + + /** + * Listen for changes in the user. + * This method returns a unsubscribe function to stop listening to events. + * Always ensure you unsubscribe from the listener when no longer needed to prevent updates to components no longer in use. + */ + onUserChanged(listener: Function): () => void; + + signOut(): Promise + + /** + * Sign an anonymous user. + * If the user has already signed in, that user will be returned + */ + signInAnonymously(): Promise + /** * We can create a user by calling the createUserWithEmailAndPassword() function. @@ -674,18 +768,6 @@ declare module "react-native-firebase" { */ signInWithEmailAndPassword(email: string, password: string): Promise - /** - * Sign an anonymous user. - * If the user has already signed in, that user will be returned - */ - signInAnonymously(): Promise - - /** - * Sign in the user with a 3rd party credential provider. - * credential requires the following properties: - */ - signInWithCredential(credential: Credential): Promise - /** * Sign a user in with a self-signed JWT token. * To sign a user using a self-signed custom token, @@ -694,22 +776,40 @@ declare module "react-native-firebase" { */ signInWithCustomToken(token: string): Promise + /** + * Sign in the user with a 3rd party credential provider. + * credential requires the following properties: + */ + signInWithCredential(credential: AuthCredential): Promise + + /** + * Asynchronously signs in using a phone number. + */ + signInWithPhoneNumber(phoneNumber: string): Promise + + /** + * Returns a PhoneAuthListener to listen to phone verification events, + * on the final completion event a PhoneAuthCredential can be generated for + * authentication purposes. + */ + verifyPhoneNumber(phoneNumber: string, autoVerifyTimeout?: number): PhoneAuthListener + /** * Sends a password reset email to the given email address. * Unlike the web SDK, * the email will contain a password reset link rather than a code. */ - sendPasswordResetEmail(email: string): Promise + sendPasswordResetEmail(email: string, actionCodeSettings?: ActionCodeSettings): Promise /** * Completes the password reset process, given a confirmation code and new password. */ - confirmPasswordReset(code: string, newPassword: string): Promise + confirmPasswordReset(code: string, newPassword: string): Promise /** * Applies a verification code sent to the user by email or other out-of-band mechanism. */ - applyActionCode(code: string): Promise + applyActionCode(code: string): Promise /** * Checks a verification code sent to the user by email or other out-of-band mechanism. @@ -717,10 +817,14 @@ declare module "react-native-firebase" { checkActionCode(code: string): Promise /** - * Completes the password reset process, - * given a confirmation code and new password. + * Get the currently signed in user */ - signOut(): Promise + getCurrentUser(): Promise + + /** + * Returns a list of authentication providers that can be used to sign in a given user (identified by its main email address). + */ + fetchProvidersForEmail(email: string): Promise> [key: string]: any; }