Search code examples
javascriptparse-platformparse-server

Parse.User.logIn gets stuck


I have made a user login function:

export const userLogin = (email, password) => (dispatch) => {
    console.log(email, password);
  dispatch({ type: actionTypes.AUTH_LOGIN_STARTED });
  console.log("after dispatch");
  Parse.User.logIn(email, password, {
    success(user) {
        console.log("in success");
      dispatch({
        type: actionTypes.AUTH_LOGIN_SUCCESS,
        user: user.toJSON(),
      });
     window.location.replace('/');
    },
    error(user, error) {
        console.log("in error")
      console.log({ error });
      // The login failed. Check error to see why.
      dispatch({
        type: actionTypes.AUTH_LOGIN_ERROR,
        error,
      });
    },
  });
};

but it always gets stuck after Parse.User.logIn it doesn't go in a success or in error. I have logged the email and password and they are correct.

So what am I missing here?


Solution

  • Parse.User.Login does not have any third parameter. You should be using the promise functions:

    Parse.User.logIn(email, password)
      .then((user) => {
        console.log("in success");
        dispatch({
          type: actionTypes.AUTH_LOGIN_SUCCESS,
          user: user.toJSON(),
        });
        window.location.replace('/');
      })
      .error((user, error) => {
        console.log("in error")
        console.log({ error });
        // The login failed. Check error to see why.
        dispatch({
          type: actionTypes.AUTH_LOGIN_ERROR,
          error,
        });
      });
    

    Or, if you're fancy enough, you can use the new await syntax (Which I think is a bit cleaner):

    export const userLogin = (email, password) => async (dispatch) => {
      console.log(email, password);
      dispatch({ type: actionTypes.AUTH_LOGIN_STARTED });
      console.log("after dispatch");
    
      try {
        const user = await Parse.User.logIn(email, password);
        console.log("in success");
        dispatch({
          type: actionTypes.AUTH_LOGIN_SUCCESS,
          user: user.toJSON(),
        });
        window.location.replace('/');
      } catch (error) {
        console.log("in error")
        console.log({ error });
        // The login failed. Check error to see why.
        dispatch({
          type: actionTypes.AUTH_LOGIN_ERROR,
          error,
        });
      }
    };