From 8b3e57925c54bf0fbd59015f3a837f47acb5cfb5 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Fri, 26 Feb 2016 20:13:41 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B0=D0=BB=D0=B5=D0=BD=D1=8C=D0=BA?= =?UTF-8?q?=D0=B8=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D0=BD=D0=B3.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=86=D0=B5=D0=BB=D0=B5=D0=B2=D0=BE=D0=B9=20=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=86=D1=8B=20=D1=8E=D0=B7=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D1=82=D0=BE=D0=BC=20=D1=81=D0=BB=D1=83=D1=87=D0=B0?= =?UTF-8?q?=D0=B5,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BE=D0=BD=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/auth/actions.js | 30 ++++++++++++++++++++++++++---- src/components/user/User.js | 4 +++- src/components/user/actions.js | 12 ++++++++++++ src/components/user/reducer.js | 2 ++ src/routes.js | 30 +++++++++++++++++++++++------- 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/components/auth/actions.js b/src/components/auth/actions.js index 1cacca9..4388d97 100644 --- a/src/components/auth/actions.js +++ b/src/components/auth/actions.js @@ -1,6 +1,6 @@ import { routeActions } from 'react-router-redux'; -import { updateUser, logout as logoutUser, fetchUserData } from 'components/user/actions'; +import { updateUser, logout as logoutUser, authenticate } from 'components/user/actions'; import request from 'services/request'; export function login({login = '', password = '', rememberMe = false}) { @@ -18,8 +18,7 @@ export function login({login = '', password = '', rememberMe = false}) { token: resp.jwt })); - request.setAuthToken(resp.jwt); - dispatch(fetchUserData()); + dispatch(authenticate(resp.jwt)); dispatch(redirectToGoal()); }) @@ -37,6 +36,8 @@ export function login({login = '', password = '', rememberMe = false}) { const errorMessage = resp.errors[Object.keys(resp.errors)[0]]; dispatch(setError(errorMessage)); } + + // TODO: log unexpected errors }) ; } @@ -64,6 +65,8 @@ export function register({ .catch((resp) => { const errorMessage = resp.errors[Object.keys(resp.errors)[0]]; dispatch(setError(errorMessage)); + + // TODO: log unexpected errors }) ; } @@ -84,12 +87,31 @@ export function activate({key = ''}) { .catch((resp) => { const errorMessage = resp.errors[Object.keys(resp.errors)[0]]; dispatch(setError(errorMessage)); + + // TODO: log unexpected errors }) ; } function redirectToGoal() { - return routeActions.push('/oauth/permissions'); + return (dispatch, getState) => { + const {user} = getState(); + + switch (user.goal) { + case 'oauth': + dispatch(routeActions.push('/oauth/permissions')); + break; + + case 'account': + default: + dispatch(routeActions.push('/')); + break; + } + + // dispatch(updateUser({ // TODO: mb create action resetGoal? + // goal: null + // })); + }; } export const ERROR = 'error'; diff --git a/src/components/user/User.js b/src/components/user/User.js index c40982f..2239191 100644 --- a/src/components/user/User.js +++ b/src/components/user/User.js @@ -21,8 +21,10 @@ export default class User { username: '', email: '', avatar: '', + goal: null, // the goal with wich user entered site isGuest: true, - isActive: false + isActive: false, + shouldChangePassword: false }; const user = Object.keys(defaults).reduce((user, key) => { diff --git a/src/components/user/actions.js b/src/components/user/actions.js index cf29950..76027d4 100644 --- a/src/components/user/actions.js +++ b/src/components/user/actions.js @@ -44,3 +44,15 @@ export function fetchUserData() { console.log(resp); }); } + + +export function authenticate(token) { + if (!token || token.split('.').length !== 3) { + throw new Error('Invalid token'); + } + + return (dispatch) => { + request.setAuthToken(token); + dispatch(fetchUserData()); + }; +} diff --git a/src/components/user/reducer.js b/src/components/user/reducer.js index cfd2753..5ba7f16 100644 --- a/src/components/user/reducer.js +++ b/src/components/user/reducer.js @@ -2,6 +2,7 @@ import { UPDATE, SET } from './actions'; import User from './User'; +// TODO: возможно есть смысл инитить обьект User снаружи, так как редусер не должен столько знать export default function user( state = new User(), {type, payload = null} @@ -16,6 +17,7 @@ export default function user( ...state, ...payload }); + case SET: return new User(payload || {}); diff --git a/src/routes.js b/src/routes.js index 16530e3..69db693 100644 --- a/src/routes.js +++ b/src/routes.js @@ -5,8 +5,7 @@ import RootPage from 'pages/root/RootPage'; import IndexPage from 'pages/index/IndexPage'; import AuthPage from 'pages/auth/AuthPage'; -import request from 'services/request'; -import { fetchUserData } from 'components/user/actions'; +import { authenticate, updateUser } from 'components/user/actions'; import OAuthInit from 'components/auth/OAuthInit'; import Register from 'components/auth/Register'; @@ -20,13 +19,15 @@ import PasswordChange from 'components/auth/PasswordChange'; 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 { - forcePath = '/oauth/permissions'; + } else if (!state.user.shouldChangePassword) { + forcePath = '/password-change'; } } else { if (state.user.email || state.user.username) { @@ -36,7 +37,23 @@ export default function routesFactory(store) { } } - if (forcePath && state.routing.location.pathname !== forcePath) { + 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}); } } @@ -44,8 +61,7 @@ export default function routesFactory(store) { const state = store.getState(); if (state.user.token) { // authorizing user if it is possible - request.setAuthToken(state.user.token); - store.dispatch(fetchUserData()); + store.dispatch(authenticate(state.user.token)); } return (