accounts-frontend/src/routes.js

89 lines
3.1 KiB
JavaScript
Raw Normal View History

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';
import AuthPage from 'pages/auth/AuthPage';
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';
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';
import Logout from 'components/auth/Logout';
import PasswordChange from 'components/auth/PasswordChange';
import ForgotPassword from 'components/auth/ForgotPassword';
export default function routesFactory(store) {
function checkAuth(nextState, replace) {
const state = store.getState();
const pathname = state.routing.location.pathname;
let forcePath;
let goal;
if (!state.user.isGuest) {
if (!state.user.isActive) {
forcePath = '/activation';
} else if (!state.user.shouldChangePassword) {
forcePath = '/password-change';
}
} 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
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
}));
}
replace({pathname: forcePath});
}
}
2016-02-26 11:55:47 +05:30
const state = store.getState();
if (state.user.token) {
// authorizing user if it is possible
store.dispatch(authenticate(state.user.token));
2016-02-26 11:55: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} />
<Route path="/password-change" components={new PasswordChange()} />
<Route path="/forgot-password" components={new ForgotPassword()} />
</Route>
2016-02-23 11:27:16 +05:30
<Route path="oauth" component={OAuthInit} />
<Route path="logout" component={Logout} />
</Route>
);
}