Still working on Auth State

This commit is contained in:
2019-10-02 17:53:20 -04:00
parent dcd03d7888
commit 5df28e0e77
12 changed files with 363 additions and 60 deletions

View File

@@ -0,0 +1,35 @@
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI} from '../types';
import axios from 'axios';
export const loginUser = (loginData, history) => (dispatch) => {
dispatch({ type: LOADING_UI });
axios
.post("/login", loginData)
.then((res) => {
// Save the login token
const FBIdToken = `Bearer ${res.data.token}`;
localStorage.setItem('FBIdToken', FBIdToken);
axios.defaults.headers.common['Authorization'] = FBIdToken;
dispatch(getProfileInfo());
dispatch({ type: CLEAR_ERRORS })
// Redirects to home page
history.push('/home');
})
.catch((err) => {
dispatch ({
type: SET_ERRORS,
payload: err.response.data,
})
});
}
export const getProfileInfo = () => (dispatch) => {
axios.get('/getProfileInfo')
.then((res) => {
dispatch({
type: SET_USER,
payload: res.data,
})
})
.catch((err) => console.error(err));
}