mavend/octoboard

View on GitHub
src/utils/user/handleAuthorization.js

Summary

Maintainability
A
0 mins
Test Coverage
F
12%
const handleAuthorization = (loginFunc, setError, setLoading, history, setSuccess = null) => {
  return async () => {
    try {
      setError(null);
      setLoading(true);
      let from = { from: { pathname: "/" } };
      if (history.location.state && history.location.state.from) {
        from = { ...history.location.state.from };
      }

      await loginFunc();
      history.replace(from);
    } catch (e) {
      setLoading(false);
      setError(e.message);
      return;
    }
    setLoading(false);
    if (setSuccess) {
      setSuccess();
    }
  };
};

export default handleAuthorization;