add user authentication when first opening editor
This commit is contained in:
parent
d672166b87
commit
5e631dcb16
7 changed files with 45 additions and 8 deletions
|
@ -13,7 +13,7 @@ const store = configureStore(initialState)
|
||||||
|
|
||||||
render(
|
render(
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<Router history={history} routes={routes} />
|
<Router history={history} routes={routes(store)} />
|
||||||
</Provider>,
|
</Provider>,
|
||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
)
|
)
|
|
@ -28,3 +28,13 @@ export function createSession(req, res, next) {
|
||||||
});
|
});
|
||||||
})(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'});
|
||||||
|
}
|
|
@ -10,6 +10,8 @@ router.route('/login').post(SessionController.createSession);
|
||||||
|
|
||||||
router.route('/logout').get(SessionController.destroySession);
|
router.route('/logout').get(SessionController.destroySession);
|
||||||
|
|
||||||
|
router.route('/session').get(SessionController.getSession);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|
||||||
//TODO add github authentication stuff
|
//TODO add github authentication stuff
|
||||||
|
|
|
@ -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) {
|
export function authError(error) {
|
||||||
return {
|
return {
|
||||||
type: ActionTypes.AUTH_ERROR,
|
type: ActionTypes.AUTH_ERROR,
|
||||||
|
|
|
@ -11,3 +11,5 @@ export const DECREASE_FONTSIZE = 'DECREASE_FONTSIZE';
|
||||||
|
|
||||||
export const AUTH_USER = 'AUTH_USER';
|
export const AUTH_USER = 'AUTH_USER';
|
||||||
export const UNAUTH_USER = 'UNAUTH_USER';
|
export const UNAUTH_USER = 'UNAUTH_USER';
|
||||||
|
|
||||||
|
export const AUTH_ERROR = 'AUTH_ERROR';
|
||||||
|
|
|
@ -5,6 +5,10 @@ const user = (state = {authenticated: false}, action) => {
|
||||||
case ActionTypes.AUTH_USER:
|
case ActionTypes.AUTH_USER:
|
||||||
return { ...action.user,
|
return { ...action.user,
|
||||||
authenticated: true };
|
authenticated: true };
|
||||||
|
case ActionTypes.AUTH_ERROR:
|
||||||
|
return {
|
||||||
|
authenticated: false
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,20 @@ import App from './containers/App'
|
||||||
import IDEView from './containers/IDEView/IDEView'
|
import IDEView from './containers/IDEView/IDEView'
|
||||||
import LoginView from './containers/LoginView/LoginView'
|
import LoginView from './containers/LoginView/LoginView'
|
||||||
import SignupView from './containers/SignupView/SignupView'
|
import SignupView from './containers/SignupView/SignupView'
|
||||||
|
import { getUser } from './redux/actions/user';
|
||||||
|
|
||||||
const routes = (
|
const routes = (store) => {
|
||||||
<Route path="/" component={App}>
|
return (
|
||||||
<IndexRoute component={IDEView} />
|
<Route path="/" component={App}>
|
||||||
<Route path="/login" component={LoginView}/>
|
<IndexRoute component={IDEView} onEnter={checkAuth(store)}/>
|
||||||
<Route path="/signup" component={SignupView}/>
|
<Route path="/login" component={LoginView}/>
|
||||||
</Route>
|
<Route path="/signup" component={SignupView}/>
|
||||||
);
|
</Route>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkAuth = (store) => {
|
||||||
|
store.dispatch(getUser());
|
||||||
|
}
|
||||||
|
|
||||||
export default routes;
|
export default routes;
|
Loading…
Reference in a new issue