import { batchActions } from "redux-batched-actions"; import { removeData, storeData } from "../service/asyncStorage"; import { loginApi } from "../service/user"; import { logInSuccess, setToken, setUsername } from "../store/actions"; export const login = (email, password, callback) => { return async (dispatch) => { const result = await loginApi({ identifier: email, password }); if (result.data === null) { callback(result); }else { await removeData("TOKEN"); const token = await storeData('TOKEN', result.jwt); console.log(result.user); if (token) { dispatch(batchActions([ setToken(result.jwt), setUsername(result.user.username), logInSuccess(true) ])) } } }; }; export const logout = () => { return async (dispatch) => { await removeData('TOKEN'); dispatch(batchActions([ setToken(null), setUsername(null), logInSuccess(false) ])) }; };