src/actions/member.js
/**
 * Sign Up to Firebase and Backend
 */
Sign Up to Firebase and Backend
Sign Up to Firebase and Backend
function signUp(formData) {
  const {
    email,
    password,
    password2,
    firstName,
    lastName,
    cellphone,
    referred_by, 
    checked
  } = formData;
  return dispatch =>
    new Promise(async (resolve, reject) => {
      if (!firstName)
        return reject({ message: ErrorMessages.missingFirstName });
      if (!lastName) return reject({ message: ErrorMessages.missingLastName });
      if (!cellphone)
        return reject({ message: ErrorMessages.missingCellphone });
      if (!email) return reject({ message: ErrorMessages.missingEmail });
      if (!password) return reject({ message: ErrorMessages.missingPassword });
      if (!password2) return reject({ message: ErrorMessages.missingPassword });
      if (password !== password2)
        return reject({ message: ErrorMessages.passwordsDontMatch });
      if (!checked) return reject({ message: ErrorMessages.missingTandC });
      const already_user = await publicAPI
        .post("/already_user", JSON.stringify({ cellphone: cellphone }), {
          headers: { common: {} }
        })
        .then(response => ({ ok: true, data: response.data.is_user }))
        .catch(error =>
          Promise.resolve({ ok: false, data: error })
        );
      if (already_user.data)
        return reject({ message: ErrorMessages.existingCellphone });
      await statusMessage(dispatch, "loading", true);
      return Firebase.auth()
        .createUserWithEmailAndPassword(email, password)
        .then(res => {
          if (res && res.user.uid) {
            FirebaseRef.child(`users/${res.user.uid}`)
              .set({
                firstName,
                lastName,
                phoneNumber: cellphone,
                signedUp: Firebase.database.ServerValue.TIMESTAMP,
                lastLoggedIn: Firebase.database.ServerValue.TIMESTAMP
              })
              .then(() => {
                axios
                  .post(
                    "https://seller-server-dev.herokuapp.com/api/sign_up",
                    {
                      sign_up: {
                        first_name: firstName,
                        last_name: lastName,
                        cellphone: cellphone,
                        password: password,
                        email: email,
                        commission: 0.3,
                        referred_by_code: referred_by
                      }
                    },
                    { headers: { common: {} } }
                  )
                  .then(response => {
                    console.log("user and seller registered");
                    console.log(response.data);
                    statusMessage(dispatch, "loading", false).then(resolve);
                  })
                  .catch(error => console.log(error));
              });
          }
        })
        .catch(reject);
    }).catch(async err => {
      await statusMessage(dispatch, "loading", false);
      if (err.code === "auth/email-already-in-use") {
        throw ErrorMessages.existingEmail;
      } else if (err.code === "auth/invalid-email") {
        throw ErrorMessages.invalidEmail;
      } else {
        throw err.message;
      }
      console.log(err.code);
      throw err.message;
    });
}
function signUp ( formData ) { const { email , password , password2 , firstName , lastName , cellphone , referred_by , checked } = formData ; return dispatch => new Promise ( async ( resolve , reject ) => { if ( ! firstName ) return reject ( { message : ErrorMessages . missingFirstName } ) ; if ( ! lastName ) return reject ( { message : ErrorMessages . missingLastName } ) ; if ( ! cellphone ) return reject ( { message : ErrorMessages . missingCellphone } ) ; if ( ! email ) return reject ( { message : ErrorMessages . missingEmail } ) ; if ( ! password ) return reject ( { message : ErrorMessages . missingPassword } ) ; if ( ! password2 ) return reject ( { message : ErrorMessages . missingPassword } ) ; if ( password !== password2 ) return reject ( { message : ErrorMessages . passwordsDontMatch } ) ; if ( ! checked ) return reject ( { message : ErrorMessages . missingTandC } ) ; const already_user = await publicAPI . post ( "/already_user" , JSON . stringify ( { cellphone : cellphone } ) , { headers : { common : { } } } ) . then ( response => ( { ok : true , data : response . data . is_user } ) ) . catch ( error => Promise . resolve ( { ok : false , data : error } ) ) ; if ( already_user . data ) return reject ( { message : ErrorMessages . existingCellphone } ) ; await statusMessage ( dispatch , "loading" , true ) ; return Firebase . auth ( ) . createUserWithEmailAndPassword ( email , password ) . then ( res => { if ( res && res . user . uid ) { FirebaseRef . child ( ` ${ res . user . uid } ` ) . set ( { firstName , lastName , phoneNumber : cellphone , signedUp : Firebase . database . ServerValue . TIMESTAMP , lastLoggedIn : Firebase . database . ServerValue . TIMESTAMP } ) . then ( ( ) => { axios . post ( "https://seller-server-dev.herokuapp.com/api/sign_up" , { sign_up : { first_name : firstName , last_name : lastName , cellphone : cellphone , password : password , email : email , commission : 0.3 , referred_by_code : referred_by } } , { headers : { common : { } } } ) . then ( response => { console . log ( "user and seller registered" ) ; console . log ( response . data ) ; statusMessage ( dispatch , "loading" , false ) . then ( resolve ) ; } ) . catch ( error => console . log ( error ) ) ; } ) ; } } ) . catch ( reject ) ; } ) . catch ( async err => { await statusMessage ( dispatch , "loading" , false ) ; if ( err . code === "auth/email-already-in-use" ) { throw ErrorMessages . existingEmail ; } else if ( err . code === "auth/invalid-email" ) { throw ErrorMessages . invalidEmail ; } else { throw err . message ; } console . log ( err . code ) ; throw err . message ; } ) ; }
Sign Up to Firebase and Backend
Sign Up to Firebase and Backend
//optional // Validation checks // Check already descubre account // console.log("Error @check already descubre account " + error) // Go to Firebase // Send user details to Firebase database // Send user details to Flask DB