mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 21:25:04 +00:00
Still working on Auth State
This commit is contained in:
35
twistter-frontend/src/redux/actions/userActions.js
Normal file
35
twistter-frontend/src/redux/actions/userActions.js
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user