2016-01-03 01:54:07 +05:30
|
|
|
import React from 'react';
|
|
|
|
import { Route, IndexRoute } from 'react-router';
|
|
|
|
|
2016-01-04 02:48:42 +05:30
|
|
|
import RootPage from 'pages/root/RootPage';
|
|
|
|
import IndexPage from 'pages/index/IndexPage';
|
2016-01-16 17:36:22 +05:30
|
|
|
import AuthPage from 'pages/auth/AuthPage';
|
|
|
|
|
2016-02-26 23:43:41 +05:30
|
|
|
import { authenticate, updateUser } from 'components/user/actions';
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-02-23 11:27:16 +05:30
|
|
|
import OAuthInit from 'components/auth/OAuthInit';
|
2016-01-16 17:36:22 +05:30
|
|
|
import Register from 'components/auth/Register';
|
|
|
|
import Login from 'components/auth/Login';
|
|
|
|
import Permissions from 'components/auth/Permissions';
|
|
|
|
import Activation from 'components/auth/Activation';
|
|
|
|
import Password from 'components/auth/Password';
|
2016-02-13 20:58:47 +05:30
|
|
|
import Logout from 'components/auth/Logout';
|
2016-02-26 04:12:20 +05:30
|
|
|
import PasswordChange from 'components/auth/PasswordChange';
|
2016-02-28 02:56:13 +05:30
|
|
|
import ForgotPassword from 'components/auth/ForgotPassword';
|
2016-01-03 01:54:07 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export default function routesFactory(store) {
|
|
|
|
function checkAuth(nextState, replace) {
|
|
|
|
const state = store.getState();
|
2016-02-26 23:43:41 +05:30
|
|
|
const pathname = state.routing.location.pathname;
|
2016-02-13 20:58:47 +05:30
|
|
|
|
|
|
|
let forcePath;
|
2016-02-26 23:43:41 +05:30
|
|
|
let goal;
|
2016-02-13 20:58:47 +05:30
|
|
|
if (!state.user.isGuest) {
|
|
|
|
if (!state.user.isActive) {
|
|
|
|
forcePath = '/activation';
|
2016-02-26 23:43:41 +05:30
|
|
|
} else if (!state.user.shouldChangePassword) {
|
|
|
|
forcePath = '/password-change';
|
2016-01-18 10:58:43 +05:30
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
} else {
|
|
|
|
if (state.user.email || state.user.username) {
|
|
|
|
forcePath = '/password';
|
|
|
|
} else {
|
|
|
|
forcePath = '/login';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 16:23:58 +05:30
|
|
|
// TODO: validate that we have all required data on premissions page
|
|
|
|
|
2016-02-26 23:43:41 +05:30
|
|
|
if (forcePath && pathname !== forcePath) {
|
|
|
|
switch (pathname) {
|
|
|
|
case '/':
|
|
|
|
goal = 'account';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/oauth/permissions':
|
|
|
|
goal = 'oauth';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (goal) {
|
|
|
|
store.dispatch(updateUser({ // TODO: mb create action resetGoal?
|
|
|
|
goal
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
replace({pathname: forcePath});
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 10:58:43 +05:30
|
|
|
|
2016-02-26 11:55:47 +05:30
|
|
|
const state = store.getState();
|
|
|
|
if (state.user.token) {
|
|
|
|
// authorizing user if it is possible
|
2016-02-26 23:43:41 +05:30
|
|
|
store.dispatch(authenticate(state.user.token));
|
2016-02-26 11:55:47 +05:30
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
return (
|
|
|
|
<Route path="/" component={RootPage}>
|
|
|
|
<IndexRoute component={IndexPage} onEnter={checkAuth} />
|
|
|
|
|
|
|
|
<Route path="auth" component={AuthPage}>
|
|
|
|
<Route path="/login" components={new Login()} onEnter={checkAuth} />
|
|
|
|
<Route path="/password" components={new Password()} onEnter={checkAuth} />
|
|
|
|
<Route path="/register" components={new Register()} />
|
|
|
|
<Route path="/activation" components={new Activation()} />
|
|
|
|
<Route path="/oauth/permissions" components={new Permissions()} onEnter={checkAuth} />
|
2016-02-26 04:12:20 +05:30
|
|
|
<Route path="/password-change" components={new PasswordChange()} />
|
2016-02-28 02:56:13 +05:30
|
|
|
<Route path="/forgot-password" components={new ForgotPassword()} />
|
2016-02-13 20:58:47 +05:30
|
|
|
</Route>
|
|
|
|
|
2016-02-23 11:27:16 +05:30
|
|
|
<Route path="oauth" component={OAuthInit} />
|
2016-02-13 20:58:47 +05:30
|
|
|
<Route path="logout" component={Logout} />
|
|
|
|
</Route>
|
|
|
|
);
|
|
|
|
}
|