src/actions/member.js
/**
  * Login to Firebase with Email/Password
  */
Login to Firebase with Email/Password
Login to Firebase with Email / Password
function login(formData) {
  const {
    email,
    password,
  } = formData;
  return dispatch => new Promise(async (resolve, reject) => {
    await statusMessage(dispatch, 'loading', true);
    if (!email) {
      showNotification(dispatch, ErrorMessages.missingEmail);
      return reject(new Error(ErrorMessages.missingEmail));
    }
    if (!password) {
      showNotification(dispatch, ErrorMessages.missingPassword);
      return reject(new Error(ErrorMessages.missingPassword));
    }
    return Firebase.auth()
      .setPersistence(Firebase.auth.Auth.Persistence.LOCAL)
      .then(() =>
        Firebase.auth()
          .signInWithEmailAndPassword(email, password)
          .then(async (res) => {
            if (res && res.uid) {
              FirebaseRef.child(`users/${res.uid}`).update({
                lastLoggedIn: Firebase.database.ServerValue.TIMESTAMP,
              });
              if (res.emailVerified === false) {
                Firebase.auth().currentUser
                  .sendEmailVerification()
                  .catch(() => console.log('Verification email failed to send'));
              }
              getUserData(dispatch);
            }
            await statusMessage(dispatch, 'loading', false);
            return resolve(dispatch({
              type: 'USER_LOGIN',
              data: res,
            }));
          }).catch(reject));
  }).catch(async (err) => {
    await statusMessage(dispatch, 'error', err.message);
    let toastMessage = err.message;
    if (err.code === 'auth/invalid-email') { toastMessage = ErrorMessages.invalidEmail; }
    if (err.code === 'auth/invalid-password') { toastMessage = ErrorMessages.wrongPassword; }
    if (err.code === 'auth/wrong-password') { toastMessage = ErrorMessages.wrongPassword; }
    if (err.code === 'auth/user-not-found') { toastMessage = ErrorMessages.userNotFound; }
    if (err.code === 'auth/auth/internal-error') { toastMessage = ErrorMessages.internalError; }
    showNotification(dispatch, toastMessage);
    throw err.message;
  });
}
function login ( formData ) { const { email , password , } = formData ; return dispatch => new Promise ( async ( resolve , reject ) => { await statusMessage ( dispatch , 'loading' , true ) ; if ( ! email ) { showNotification ( dispatch , ErrorMessages . missingEmail ) ; return reject ( new Error ( ErrorMessages . missingEmail ) ) ; } if ( ! password ) { showNotification ( dispatch , ErrorMessages . missingPassword ) ; return reject ( new Error ( ErrorMessages . missingPassword ) ) ; } return Firebase . auth ( ) . setPersistence ( Firebase . auth . Auth . Persistence . LOCAL ) . then ( ( ) => Firebase . auth ( ) . signInWithEmailAndPassword ( email , password ) . then ( async ( res ) => { if ( res && res . uid ) { FirebaseRef . child ( ` ${ res . uid } ` ) . update ( { lastLoggedIn : Firebase . database . ServerValue . TIMESTAMP , } ) ; if ( res . emailVerified === false ) { Firebase . auth ( ) . currentUser . sendEmailVerification ( ) . catch ( ( ) => console . log ( 'Verification email failed to send' ) ) ; } getUserData ( dispatch ) ; } await statusMessage ( dispatch , 'loading' , false ) ; return resolve ( dispatch ( { type : 'USER_LOGIN' , data : res , } ) ) ; } ) . catch ( reject ) ) ; } ) . catch ( async ( err ) => { await statusMessage ( dispatch , 'error' , err . message ) ; let toastMessage = err . message ; if ( err . code === 'auth/invalid-email' ) { toastMessage = ErrorMessages . invalidEmail ; } if ( err . code === 'auth/invalid-password' ) { toastMessage = ErrorMessages . wrongPassword ; } if ( err . code === 'auth/wrong-password' ) { toastMessage = ErrorMessages . wrongPassword ; } if ( err . code === 'auth/user-not-found' ) { toastMessage = ErrorMessages . userNotFound ; } if ( err . code === 'auth/auth/internal-error' ) { toastMessage = ErrorMessages . internalError ; } showNotification ( dispatch , toastMessage ) ; throw err . message ; } ) ; }
Login to Firebase with Email/Password
Login to Firebase with Email / Password
// Validation checks // Go to Firebase // Update last logged in data // Send verification Email when email hasn't been verified // Get User Data // Send Login data to Redux