react-with-firebase-auth
Version: 
Higher Order Component for integrating Firebase with a React Component
155 lines (154 loc) • 10.6 kB
TypeScript
import React from 'react';
import firebase from 'firebase';
export declare type WrappedComponentProps = {
    signInWithEmailAndPassword: (email: string, password: string) => void;
    createUserWithEmailAndPassword: (email: string, password: string) => void;
    signInWithGoogle: () => void;
    signInWithFacebook: () => void;
    signInWithGithub: () => void;
    signInWithTwitter: () => void;
    signInWithApple: () => void;
    signInWithPhoneNumber: (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => void;
    signInAnonymously: () => void;
    signOut: () => void;
    setError: (error: string | null) => void;
    user?: firebase.User | null;
    error?: string;
    loading: boolean;
};
export declare type PossibleProviders = keyof ProvidersMapper;
export declare type ProvidersMapper = {
    googleProvider?: firebase.auth.GoogleAuthProvider_Instance;
    facebookProvider?: firebase.auth.FacebookAuthProvider_Instance;
    twitterProvider?: firebase.auth.TwitterAuthProvider_Instance;
    githubProvider?: firebase.auth.GithubAuthProvider_Instance;
    appleProvider?: firebase.auth.OAuthProvider;
};
export declare type HocParameters = {
    firebaseAppAuth: firebase.auth.Auth;
    providers?: ProvidersMapper;
};
export declare type FirebaseAuthProviderState = {
    loading: boolean;
    user?: firebase.User | null;
    error?: string | null;
};
declare const withFirebaseAuth: ({ firebaseAppAuth, providers, }: HocParameters) => <P>(WrappedComponent: React.ComponentType<P & WrappedComponentProps>) => {
    new (props: P | Readonly<P>): {
        state: {
            loading: boolean;
            user: undefined;
            error: undefined;
        };
        unsubscribeAuthStateListener: firebase.Unsubscribe | undefined;
        componentDidMount(): void;
        componentWillUnmount(): void;
        setError: (error: string | null) => void;
        toggleLoading: () => void;
        tryTo<T>(operation: () => Promise<T>): Promise<firebase.auth.Error | T>;
        tryToSignInWithProvider: (provider: PossibleProviders) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signOut: () => Promise<void | firebase.auth.Error>;
        signInAnonymously: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithGithub: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithTwitter: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithGoogle: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithFacebook: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithApple: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithPhoneNumber: (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => Promise<firebase.auth.Error | firebase.auth.ConfirmationResult>;
        createUserWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        sharedHandlers: {
            createUserWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithGithub: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithTwitter: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithGoogle: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithFacebook: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithApple: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithPhoneNumber: (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => Promise<firebase.auth.Error | firebase.auth.ConfirmationResult>;
            signInAnonymously: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            setError: (error: string | null) => void;
            signOut: () => Promise<void | firebase.auth.Error>;
        };
        render(): JSX.Element;
        context: any;
        setState<K extends keyof FirebaseAuthProviderState>(state: FirebaseAuthProviderState | ((prevState: Readonly<FirebaseAuthProviderState>, props: Readonly<P>) => FirebaseAuthProviderState | Pick<FirebaseAuthProviderState, K> | null) | Pick<FirebaseAuthProviderState, K> | null, callback?: (() => void) | undefined): void;
        forceUpdate(callback?: (() => void) | undefined): void;
        readonly props: Readonly<P> & Readonly<{
            children?: React.ReactNode;
        }>;
        refs: {
            [key: string]: React.ReactInstance;
        };
        shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<FirebaseAuthProviderState>, nextContext: any): boolean;
        componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
        getSnapshotBeforeUpdate?(prevProps: Readonly<P>, prevState: Readonly<FirebaseAuthProviderState>): any;
        componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<FirebaseAuthProviderState>, snapshot?: any): void;
        componentWillMount?(): void;
        UNSAFE_componentWillMount?(): void;
        componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
        UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
        componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<FirebaseAuthProviderState>, nextContext: any): void;
        UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<FirebaseAuthProviderState>, nextContext: any): void;
    };
    new (props: P, context: any): {
        state: {
            loading: boolean;
            user: undefined;
            error: undefined;
        };
        unsubscribeAuthStateListener: firebase.Unsubscribe | undefined;
        componentDidMount(): void;
        componentWillUnmount(): void;
        setError: (error: string | null) => void;
        toggleLoading: () => void;
        tryTo<T>(operation: () => Promise<T>): Promise<firebase.auth.Error | T>;
        tryToSignInWithProvider: (provider: PossibleProviders) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signOut: () => Promise<void | firebase.auth.Error>;
        signInAnonymously: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithGithub: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithTwitter: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithGoogle: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithFacebook: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithApple: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        signInWithPhoneNumber: (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => Promise<firebase.auth.Error | firebase.auth.ConfirmationResult>;
        createUserWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
        sharedHandlers: {
            createUserWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithEmailAndPassword: (email: string, password: string) => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithGithub: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithTwitter: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithGoogle: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithFacebook: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithApple: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            signInWithPhoneNumber: (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => Promise<firebase.auth.Error | firebase.auth.ConfirmationResult>;
            signInAnonymously: () => Promise<firebase.auth.Error | firebase.auth.UserCredential>;
            setError: (error: string | null) => void;
            signOut: () => Promise<void | firebase.auth.Error>;
        };
        render(): JSX.Element;
        context: any;
        setState<K extends keyof FirebaseAuthProviderState>(state: FirebaseAuthProviderState | ((prevState: Readonly<FirebaseAuthProviderState>, props: Readonly<P>) => FirebaseAuthProviderState | Pick<FirebaseAuthProviderState, K> | null) | Pick<FirebaseAuthProviderState, K> | null, callback?: (() => void) | undefined): void;
        forceUpdate(callback?: (() => void) | undefined): void;
        readonly props: Readonly<P> & Readonly<{
            children?: React.ReactNode;
        }>;
        refs: {
            [key: string]: React.ReactInstance;
        };
        shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<FirebaseAuthProviderState>, nextContext: any): boolean;
        componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
        getSnapshotBeforeUpdate?(prevProps: Readonly<P>, prevState: Readonly<FirebaseAuthProviderState>): any;
        componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<FirebaseAuthProviderState>, snapshot?: any): void;
        componentWillMount?(): void;
        UNSAFE_componentWillMount?(): void;
        componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
        UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
        componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<FirebaseAuthProviderState>, nextContext: any): void;
        UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<FirebaseAuthProviderState>, nextContext: any): void;
    };
    displayName: string;
    contextType?: React.Context<any> | undefined;
};
export default withFirebaseAuth;