function login(data) {
    return (dispatch) => {
        return new Promise((resolve, reject) => {
            const {email, password} = data;
            auth.signInWithEmailAndPassword(email, password)
                .then((resp) => {
                    //Get the user object from the realtime database
                    let {user} = resp;
                    database.ref('users').child(user.uid).once('value')
                        .then((snapshot) => {

                            const exists = (snapshot.val() !== null);

                            //if the user exist in the DB, replace the user variable with the returned snapshot
                            if (exists) user = snapshot.val();

                            if (exists) dispatch({type: t.LOGGED_IN, user});
                            resolve({exists, user});
                        })
                        .catch((error) => reject(error));
                })
                .catch((error) => reject(error));
        });
    }
}
//Sign the user in with their email and password
Sign the user in with their email and password
Sign the user in with their email and password
function login(data) {
    return (dispatch) => {
        return new Promise((resolve, reject) => {
            const {email, password} = data;
            auth.signInWithEmailAndPassword(email, password)
                .then((resp) => {
                    let {user} = resp;
                    database.ref('users').child(user.uid).once('value')
                        .then((snapshot) => {
                            const exists = (snapshot.val() !== null);
                            if (exists) user = snapshot.val();
                            if (exists) dispatch({type: t.LOGGED_IN, user});
                            resolve({exists, user});
                        })
                        .catch((error) => reject(error));
                })
                .catch((error) => reject(error));
        });
    }
}
function login ( data ) { return ( dispatch ) => { return new Promise ( ( resolve , reject ) => { const { email , password } = data ; auth . signInWithEmailAndPassword ( email , password ) . then ( ( resp ) => { let { user } = resp ; database . ref ( 'users' ) . child ( user . uid ) . once ( 'value' ) . then ( ( snapshot ) => { const exists = ( snapshot . val ( ) !== null ) ; if ( exists ) user = snapshot . val ( ) ; if ( exists ) dispatch ( { type : t . LOGGED_IN , user } ) ; resolve ( { exists , user } ) ; } ) . catch ( ( error ) => reject ( error ) ) ; } ) . catch ( ( error ) => reject ( error ) ) ; } ) ; } }
Sign the user in with their email and password
Sign the user in with their email and password
//Get the user object from the realtime database //if the user exist in the DB, replace the user variable with the returned snapshot