mirror of
				https://github.com/elyby/accounts-frontend.git
				synced 2025-05-31 14:11:58 +05:30 
			
		
		
		
	Маленьки рефакторинг. Добавил сохранение целевой страницы юзера в том случае, если он не авторизирован
This commit is contained in:
		| @@ -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'; | ||||
|   | ||||
| @@ -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) => { | ||||
|   | ||||
| @@ -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()); | ||||
|     }; | ||||
| } | ||||
|   | ||||
| @@ -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 || {}); | ||||
|  | ||||
|   | ||||
| @@ -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 ( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user