From 5e631dcb162338d46a7666099f61950df47503b3 Mon Sep 17 00:00:00 2001 From: catarak Date: Tue, 14 Jun 2016 19:11:42 -0400 Subject: [PATCH] add user authentication when first opening editor --- client/index.js | 2 +- server/controllers/session.controller.js | 10 ++++++++++ server/routes/session.routes.js | 2 ++ shared/redux/actions/user.js | 12 ++++++++++++ shared/redux/constants/constants.js | 2 ++ shared/redux/reducers/user.js | 4 ++++ shared/routes.js | 21 ++++++++++++++------- 7 files changed, 45 insertions(+), 8 deletions(-) diff --git a/client/index.js b/client/index.js index 5f1067c1..a1a1bdf2 100644 --- a/client/index.js +++ b/client/index.js @@ -13,7 +13,7 @@ const store = configureStore(initialState) render( - + , document.getElementById('root') ) \ No newline at end of file diff --git a/server/controllers/session.controller.js b/server/controllers/session.controller.js index dcd3ebd1..be145524 100644 --- a/server/controllers/session.controller.js +++ b/server/controllers/session.controller.js @@ -27,4 +27,14 @@ export function createSession(req, res, next) { }); }); })(req, res, next); +} + +export function getSession(req, res, next) { + if (req.user) { + return res.json({ + email: req.user.email, + username: req.user.username + }); + } + res.status(404).send({message: 'Session does not exist'}); } \ No newline at end of file diff --git a/server/routes/session.routes.js b/server/routes/session.routes.js index 241a6146..f6e7ae81 100644 --- a/server/routes/session.routes.js +++ b/server/routes/session.routes.js @@ -10,6 +10,8 @@ router.route('/login').post(SessionController.createSession); router.route('/logout').get(SessionController.destroySession); +router.route('/session').get(SessionController.getSession); + export default router; //TODO add github authentication stuff diff --git a/shared/redux/actions/user.js b/shared/redux/actions/user.js index 59317e90..8651e1d6 100644 --- a/shared/redux/actions/user.js +++ b/shared/redux/actions/user.js @@ -31,6 +31,18 @@ export function loginUser(formValues) { } } +export function getUser() { + return function(dispatch) { + axios.get(`${ROOT_URL}/session`, {withCredentials: true}) + .then(response => { + dispatch({type: ActionTypes.AUTH_USER, + user: response.data + }); + }) + .catch(response => dispatch(authError(response.data.error))); + } +} + export function authError(error) { return { type: ActionTypes.AUTH_ERROR, diff --git a/shared/redux/constants/constants.js b/shared/redux/constants/constants.js index a42e9a13..7525dc92 100644 --- a/shared/redux/constants/constants.js +++ b/shared/redux/constants/constants.js @@ -11,3 +11,5 @@ export const DECREASE_FONTSIZE = 'DECREASE_FONTSIZE'; export const AUTH_USER = 'AUTH_USER'; export const UNAUTH_USER = 'UNAUTH_USER'; + +export const AUTH_ERROR = 'AUTH_ERROR'; diff --git a/shared/redux/reducers/user.js b/shared/redux/reducers/user.js index 71d1b870..84c90f4c 100644 --- a/shared/redux/reducers/user.js +++ b/shared/redux/reducers/user.js @@ -5,6 +5,10 @@ const user = (state = {authenticated: false}, action) => { case ActionTypes.AUTH_USER: return { ...action.user, authenticated: true }; + case ActionTypes.AUTH_ERROR: + return { + authenticated: false + } default: return state; } diff --git a/shared/routes.js b/shared/routes.js index 6ff24e19..1e4d9c3b 100644 --- a/shared/routes.js +++ b/shared/routes.js @@ -4,13 +4,20 @@ import App from './containers/App' import IDEView from './containers/IDEView/IDEView' import LoginView from './containers/LoginView/LoginView' import SignupView from './containers/SignupView/SignupView' +import { getUser } from './redux/actions/user'; -const routes = ( - - - - - -); +const routes = (store) => { + return ( + + + + + + ); +} + +const checkAuth = (store) => { + store.dispatch(getUser()); +} export default routes; \ No newline at end of file