add user authentication when first opening editor

This commit is contained in:
catarak 2016-06-14 19:11:42 -04:00
parent d672166b87
commit 5e631dcb16
7 changed files with 45 additions and 8 deletions

View file

@ -13,7 +13,7 @@ const store = configureStore(initialState)
render(
<Provider store={store}>
<Router history={history} routes={routes} />
<Router history={history} routes={routes(store)} />
</Provider>,
document.getElementById('root')
)

View file

@ -28,3 +28,13 @@ 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'});
}

View file

@ -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

View file

@ -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,

View file

@ -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';

View file

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

View file

@ -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 = (
<Route path="/" component={App}>
<IndexRoute component={IDEView} />
<Route path="/login" component={LoginView}/>
<Route path="/signup" component={SignupView}/>
</Route>
);
const routes = (store) => {
return (
<Route path="/" component={App}>
<IndexRoute component={IDEView} onEnter={checkAuth(store)}/>
<Route path="/login" component={LoginView}/>
<Route path="/signup" component={SignupView}/>
</Route>
);
}
const checkAuth = (store) => {
store.dispatch(getUser());
}
export default routes;