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 || {});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user