mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-28 07:50:32 +05:30
Маленьки рефакторинг. Добавил сохранение целевой страницы юзера в том случае, если он не авторизирован
This commit is contained in:
parent
d0b064bc50
commit
8b3e57925c
@ -1,6 +1,6 @@
|
|||||||
import { routeActions } from 'react-router-redux';
|
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';
|
import request from 'services/request';
|
||||||
|
|
||||||
export function login({login = '', password = '', rememberMe = false}) {
|
export function login({login = '', password = '', rememberMe = false}) {
|
||||||
@ -18,8 +18,7 @@ export function login({login = '', password = '', rememberMe = false}) {
|
|||||||
token: resp.jwt
|
token: resp.jwt
|
||||||
}));
|
}));
|
||||||
|
|
||||||
request.setAuthToken(resp.jwt);
|
dispatch(authenticate(resp.jwt));
|
||||||
dispatch(fetchUserData());
|
|
||||||
|
|
||||||
dispatch(redirectToGoal());
|
dispatch(redirectToGoal());
|
||||||
})
|
})
|
||||||
@ -37,6 +36,8 @@ export function login({login = '', password = '', rememberMe = false}) {
|
|||||||
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
||||||
dispatch(setError(errorMessage));
|
dispatch(setError(errorMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: log unexpected errors
|
||||||
})
|
})
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
@ -64,6 +65,8 @@ export function register({
|
|||||||
.catch((resp) => {
|
.catch((resp) => {
|
||||||
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
||||||
dispatch(setError(errorMessage));
|
dispatch(setError(errorMessage));
|
||||||
|
|
||||||
|
// TODO: log unexpected errors
|
||||||
})
|
})
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
@ -84,12 +87,31 @@ export function activate({key = ''}) {
|
|||||||
.catch((resp) => {
|
.catch((resp) => {
|
||||||
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
||||||
dispatch(setError(errorMessage));
|
dispatch(setError(errorMessage));
|
||||||
|
|
||||||
|
// TODO: log unexpected errors
|
||||||
})
|
})
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
function redirectToGoal() {
|
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';
|
export const ERROR = 'error';
|
||||||
|
@ -21,8 +21,10 @@ export default class User {
|
|||||||
username: '',
|
username: '',
|
||||||
email: '',
|
email: '',
|
||||||
avatar: '',
|
avatar: '',
|
||||||
|
goal: null, // the goal with wich user entered site
|
||||||
isGuest: true,
|
isGuest: true,
|
||||||
isActive: false
|
isActive: false,
|
||||||
|
shouldChangePassword: false
|
||||||
};
|
};
|
||||||
|
|
||||||
const user = Object.keys(defaults).reduce((user, key) => {
|
const user = Object.keys(defaults).reduce((user, key) => {
|
||||||
|
@ -44,3 +44,15 @@ export function fetchUserData() {
|
|||||||
console.log(resp);
|
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';
|
import User from './User';
|
||||||
|
|
||||||
|
// TODO: возможно есть смысл инитить обьект User снаружи, так как редусер не должен столько знать
|
||||||
export default function user(
|
export default function user(
|
||||||
state = new User(),
|
state = new User(),
|
||||||
{type, payload = null}
|
{type, payload = null}
|
||||||
@ -16,6 +17,7 @@ export default function user(
|
|||||||
...state,
|
...state,
|
||||||
...payload
|
...payload
|
||||||
});
|
});
|
||||||
|
|
||||||
case SET:
|
case SET:
|
||||||
return new User(payload || {});
|
return new User(payload || {});
|
||||||
|
|
||||||
|
@ -5,8 +5,7 @@ import RootPage from 'pages/root/RootPage';
|
|||||||
import IndexPage from 'pages/index/IndexPage';
|
import IndexPage from 'pages/index/IndexPage';
|
||||||
import AuthPage from 'pages/auth/AuthPage';
|
import AuthPage from 'pages/auth/AuthPage';
|
||||||
|
|
||||||
import request from 'services/request';
|
import { authenticate, updateUser } from 'components/user/actions';
|
||||||
import { fetchUserData } from 'components/user/actions';
|
|
||||||
|
|
||||||
import OAuthInit from 'components/auth/OAuthInit';
|
import OAuthInit from 'components/auth/OAuthInit';
|
||||||
import Register from 'components/auth/Register';
|
import Register from 'components/auth/Register';
|
||||||
@ -20,13 +19,15 @@ import PasswordChange from 'components/auth/PasswordChange';
|
|||||||
export default function routesFactory(store) {
|
export default function routesFactory(store) {
|
||||||
function checkAuth(nextState, replace) {
|
function checkAuth(nextState, replace) {
|
||||||
const state = store.getState();
|
const state = store.getState();
|
||||||
|
const pathname = state.routing.location.pathname;
|
||||||
|
|
||||||
let forcePath;
|
let forcePath;
|
||||||
|
let goal;
|
||||||
if (!state.user.isGuest) {
|
if (!state.user.isGuest) {
|
||||||
if (!state.user.isActive) {
|
if (!state.user.isActive) {
|
||||||
forcePath = '/activation';
|
forcePath = '/activation';
|
||||||
} else {
|
} else if (!state.user.shouldChangePassword) {
|
||||||
forcePath = '/oauth/permissions';
|
forcePath = '/password-change';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (state.user.email || state.user.username) {
|
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});
|
replace({pathname: forcePath});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -44,8 +61,7 @@ export default function routesFactory(store) {
|
|||||||
const state = store.getState();
|
const state = store.getState();
|
||||||
if (state.user.token) {
|
if (state.user.token) {
|
||||||
// authorizing user if it is possible
|
// authorizing user if it is possible
|
||||||
request.setAuthToken(state.user.token);
|
store.dispatch(authenticate(state.user.token));
|
||||||
store.dispatch(fetchUserData());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Loading…
Reference in New Issue
Block a user