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));
}

View File

@@ -0,0 +1,30 @@
import { SET_ERRORS, CLEAR_ERRORS, LOADING_UI } from '../types';
const initialState = {
loading: false,
errors: null
};
export default function(state = initialState, action) {
switch(action.type) {
case SET_ERRORS:
return {
...state,
loading: false,
errors: action.payload
};
case CLEAR_ERRORS:
return {
...state,
loading: false,
errors: null
};
case LOADING_UI:
return {
...state,
loading: true
}
default:
return state;
}
}

View File

@@ -0,0 +1,28 @@
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI, SET_AUTHENTICATED, SET_UNAUTHENTICATED} from '../types';
const initialState = {
authenticated: false,
credentials: {},
likes: [],
notifications: []
};
export default function(state = initialState, action) {
switch(action.type) {
case SET_AUTHENTICATED:
return {
...state,
authenticated: true,
};
case SET_UNAUTHENTICATED:
return initialState;
case SET_USER:
return {
authenticated: true,
...action.payload,
};
default:
return state;
}
}

View File

@@ -0,0 +1,27 @@
import { createStore, combineReducers, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import userReducer from "./reducers/userReducer";
import dataReducer from "./reducers/dataReducer";
import uiReducer from "./reducers/uiReducer";
const initialState = {};
const middleWare = [thunk];
const reducers = combineReducers({
user: userReducer,
data: dataReducer,
UI: uiReducer
});
const store = createStore(
reducers,
initialState,
compose(
applyMiddleware(...middleWare),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
export default store;

View File

@@ -0,0 +1,12 @@
// User reducer types
export const SET_AUTHENTICATED = 'SET_AUTHENTICATED';
export const SET_UNAUTHENTICATED = 'SET_UNAUTHENTICATED';
export const SET_USER = 'SET_USER';
export const LOADING_USER = 'LOADING_USER';
// UI reducer types
export const SET_ERRORS = 'SET_ERRORS';
export const LOADING_UI = 'LOADING_UI';
export const CLEAR_ERRORS = 'CLEAR_ERRORS';
// Data reducer types