0

I have the following simple saga in my react-native app:

import {call, put, take} from 'redux-saga/effects';
import firebase from 'react-native-firebase';

export function* signupWithEmailPassword(action: AuthAction) {
  const {email, password} = action.payload;
  try {
    const user = yield call(doRegister, email, password);
    yield put({type: 'SIGNUP_SUCCESS', payload: user});
  } catch (error) {
    yield put({type: 'SIGNUP_FAILURE', error});
  }
}


function* doRegister(email: string, password: string) {
  return firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email, password)
    .catch((error) => {
      const {code, message} = error;
      console.log('in doRegister: error ' + code + ' - ' + message);
    });
}

if the saga gets called with an invalid email, then firebase will throw an error like 'auth/invalid-email'. That's fine and expected, but for some reason yield call(doRegister, email, password); is not failing and therefore yield put({type: 'SIGNUP_SUCCESS', payload: user}); is called even though it should fall back to the catch.

what am I doing wrong?

edit:

changing doRegister to this, causes the same issues:

function* doRegister(email: string, password: string) {
  return firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email, password);
}
domi
  • 2,167
  • 1
  • 28
  • 45

3 Answers3

0

change your saga

function* doRegister(email: string, password: string) {
 const response= yield firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email, password);
return response; /*this will ensure it does not return before you get the response*/
}
Karan Jariwala
  • 727
  • 6
  • 17
0

This is how I got it to work

export function* loginREST( email, password ) {
  // change this to whatever firebase call you need
  return yield call(
    [ firebase.auth(), firebase.auth().signInWithEmailAndPassword ],
    email,
    password,
  );
}

export function* login( action ) {
  try {
    const response = yield call(
       loginREST,
       action.payload.email,
       action.payload.email,
    );

    const { email, uid } = response.user;
    // for some weird reason passing back the entire response object gave
    // me an infinite loop so I fixed that by only passing the stuff I need 
    yield put( loginSuccessAction( { email, uid } ) );
  }
  catch ( error ) {
    yield put( loginFailedAction( error ) );
  }
}
Jose
  • 1,959
  • 20
  • 21
0

In your sagas :

export function* signupWithEmailPassword({email , password}) {
      try {
        const user = yield call(doRegister, email, password);
        yield put({type: 'SIGNUP_SUCCESS', payload: user});
      } catch (error) {
        yield put({type: 'SIGNUP_FAILURE', error});
      }
    }

If there is any error like email is badly formatted use trim like

export function* signupWithEmailPassword({email , password}) {
          try {
            const user = yield call(doRegister, email.trim(), password);
            yield put({type: 'SIGNUP_SUCCESS', payload: user});
          } catch (error) {
            yield put({type: 'SIGNUP_FAILURE', error});
          }
        }
Ali Yar Khan
  • 1,231
  • 2
  • 11
  • 33