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:
0
twistter-frontend/src/redux/actions/dataActions.js
Normal file
0
twistter-frontend/src/redux/actions/dataActions.js
Normal file
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));
|
||||
}
|
||||
0
twistter-frontend/src/redux/reducers/dataReducer.js
Normal file
0
twistter-frontend/src/redux/reducers/dataReducer.js
Normal file
30
twistter-frontend/src/redux/reducers/uiReducer.js
Normal file
30
twistter-frontend/src/redux/reducers/uiReducer.js
Normal 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;
|
||||
}
|
||||
}
|
||||
28
twistter-frontend/src/redux/reducers/userReducer.js
Normal file
28
twistter-frontend/src/redux/reducers/userReducer.js
Normal 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;
|
||||
}
|
||||
}
|
||||
27
twistter-frontend/src/redux/store.js
Normal file
27
twistter-frontend/src/redux/store.js
Normal 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;
|
||||
12
twistter-frontend/src/redux/types.js
Normal file
12
twistter-frontend/src/redux/types.js
Normal 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
|
||||
Reference in New Issue
Block a user