You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536
  1. import { batchActions } from "redux-batched-actions";
  2. import { removeData, storeData } from "../service/asyncStorage";
  3. import { loginApi } from "../service/user";
  4. import { logInSuccess, setToken, setUsername } from "../store/actions";
  5. export const login = (email, password, callback) => {
  6. return async (dispatch) => {
  7. const result = await loginApi({ identifier: email, password });
  8. if (result.data === null) {
  9. callback(result);
  10. }else {
  11. await removeData("TOKEN");
  12. const token = await storeData('TOKEN', result.jwt);
  13. console.log(result.user);
  14. if (token) {
  15. dispatch(batchActions([
  16. setToken(result.jwt),
  17. setUsername(result.user.username),
  18. logInSuccess(true)
  19. ]))
  20. }
  21. }
  22. };
  23. };
  24. export const logout = () => {
  25. return async (dispatch) => {
  26. await removeData('TOKEN');
  27. dispatch(batchActions([
  28. setToken(null),
  29. setUsername(null),
  30. logInSuccess(false)
  31. ]))
  32. };
  33. };