2016-02-13 20:58:47 +05:30
|
|
|
import { routeActions } from 'react-router-redux';
|
|
|
|
|
2016-02-26 23:43:41 +05:30
|
|
|
import { updateUser, logout as logoutUser, authenticate } from 'components/user/actions';
|
2016-02-13 20:58:47 +05:30
|
|
|
import request from 'services/request';
|
|
|
|
|
|
|
|
export function login({login = '', password = '', rememberMe = false}) {
|
|
|
|
const PASSWORD_REQUIRED = 'error.password_required';
|
|
|
|
const LOGIN_REQUIRED = 'error.login_required';
|
|
|
|
|
|
|
|
return (dispatch) =>
|
|
|
|
request.post(
|
|
|
|
'/api/authentication/login',
|
|
|
|
{login, password, rememberMe}
|
|
|
|
)
|
2016-02-26 11:55:47 +05:30
|
|
|
.then((resp) => {
|
2016-02-13 20:58:47 +05:30
|
|
|
dispatch(updateUser({
|
2016-02-26 11:55:47 +05:30
|
|
|
isGuest: false,
|
|
|
|
token: resp.jwt
|
2016-02-13 20:58:47 +05:30
|
|
|
}));
|
|
|
|
|
2016-02-26 23:43:41 +05:30
|
|
|
dispatch(authenticate(resp.jwt));
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
dispatch(redirectToGoal());
|
|
|
|
})
|
|
|
|
.catch((resp) => {
|
|
|
|
if (resp.errors.password === PASSWORD_REQUIRED) {
|
|
|
|
dispatch(updateUser({
|
|
|
|
username: login,
|
|
|
|
email: login
|
|
|
|
}));
|
|
|
|
dispatch(routeActions.push('/password'));
|
|
|
|
} else {
|
|
|
|
if (resp.errors.login === LOGIN_REQUIRED && password) {
|
|
|
|
dispatch(logout());
|
|
|
|
}
|
|
|
|
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
|
|
|
dispatch(setError(errorMessage));
|
|
|
|
}
|
2016-02-26 23:43:41 +05:30
|
|
|
|
|
|
|
// TODO: log unexpected errors
|
2016-02-13 20:58:47 +05:30
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function register({
|
|
|
|
email = '',
|
|
|
|
username = '',
|
|
|
|
password = '',
|
|
|
|
rePassword = '',
|
|
|
|
rulesAgreement = false
|
|
|
|
}) {
|
|
|
|
return (dispatch) =>
|
|
|
|
request.post(
|
2016-02-24 03:50:10 +05:30
|
|
|
'/api/signup',
|
2016-02-13 20:58:47 +05:30
|
|
|
{email, username, password, rePassword, rulesAgreement}
|
|
|
|
)
|
|
|
|
.then(() => {
|
2016-02-13 21:04:29 +05:30
|
|
|
dispatch(updateUser({
|
|
|
|
username,
|
|
|
|
email,
|
|
|
|
isGuest: false
|
|
|
|
}));
|
2016-02-13 20:58:47 +05:30
|
|
|
dispatch(routeActions.push('/activation'));
|
|
|
|
})
|
|
|
|
.catch((resp) => {
|
|
|
|
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
|
|
|
dispatch(setError(errorMessage));
|
2016-02-26 23:43:41 +05:30
|
|
|
|
|
|
|
// TODO: log unexpected errors
|
2016-02-13 20:58:47 +05:30
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function activate({key = ''}) {
|
|
|
|
return (dispatch) =>
|
|
|
|
request.post(
|
|
|
|
'/api/signup/confirm',
|
|
|
|
{key}
|
|
|
|
)
|
2016-02-27 14:06:06 +05:30
|
|
|
.then((resp) => {
|
2016-02-13 20:58:47 +05:30
|
|
|
dispatch(updateUser({
|
|
|
|
isActive: true
|
|
|
|
}));
|
|
|
|
|
2016-02-27 14:06:06 +05:30
|
|
|
dispatch(authenticate(resp.jwt));
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
dispatch(redirectToGoal());
|
|
|
|
})
|
|
|
|
.catch((resp) => {
|
|
|
|
const errorMessage = resp.errors[Object.keys(resp.errors)[0]];
|
|
|
|
dispatch(setError(errorMessage));
|
2016-02-26 23:43:41 +05:30
|
|
|
|
|
|
|
// TODO: log unexpected errors
|
2016-02-13 20:58:47 +05:30
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
function redirectToGoal() {
|
2016-02-26 23:43:41 +05:30
|
|
|
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
|
|
|
|
// }));
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export const ERROR = 'error';
|
|
|
|
export function setError(error) {
|
|
|
|
return {
|
|
|
|
type: ERROR,
|
|
|
|
payload: error,
|
|
|
|
error: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearErrors() {
|
|
|
|
return setError(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logout() {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(logoutUser());
|
|
|
|
dispatch(routeActions.push('/login'));
|
|
|
|
};
|
|
|
|
}
|
2016-02-23 11:27:16 +05:30
|
|
|
|
|
|
|
// TODO: move to oAuth actions?
|
|
|
|
// test request: /oauth?client_id=ely&redirect_uri=http%3A%2F%2Fely.by&response_type=code&scope=minecraft_server_session
|
2016-02-27 16:23:58 +05:30
|
|
|
export function oAuthValidate(oauth) {
|
2016-02-23 11:27:16 +05:30
|
|
|
return (dispatch) =>
|
|
|
|
request.get(
|
|
|
|
'/api/oauth/validate',
|
2016-02-27 16:23:58 +05:30
|
|
|
getOAuthRequest(oauth)
|
2016-02-23 11:27:16 +05:30
|
|
|
)
|
|
|
|
.then((resp) => {
|
|
|
|
dispatch(setClient(resp.client));
|
2016-02-27 16:23:58 +05:30
|
|
|
dispatch(setOAuthRequest(resp.oAuth));
|
2016-02-23 11:27:16 +05:30
|
|
|
})
|
|
|
|
.catch((resp = {}) => { // TODO
|
2016-02-27 16:23:58 +05:30
|
|
|
handleOauthParamsValidation(resp);
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'accept_required') {
|
|
|
|
alert('Accept required.');
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function oAuthComplete(params = {}) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const oauth = getState().auth.oauth;
|
|
|
|
const query = request.buildQuery(getOAuthRequest(oauth));
|
|
|
|
|
|
|
|
return request.post(
|
|
|
|
`/api/oauth/complete?${query}`,
|
|
|
|
typeof params.accept === 'undefined' ? {} : {accept: params.accept}
|
|
|
|
)
|
|
|
|
.then((resp) => {
|
2016-02-28 01:28:36 +05:30
|
|
|
if (resp.status === 401 && resp.name === 'Unauthorized') {
|
|
|
|
// TODO: temporary solution for oauth init by guest
|
|
|
|
// TODO: request serivce should handle http status codes
|
|
|
|
dispatch(routeActions.push('/oauth/permissions'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-27 16:23:58 +05:30
|
|
|
if (resp.redirectUri) {
|
|
|
|
location.href = resp.redirectUri;
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
})
|
|
|
|
.catch((resp = {}) => { // TODO
|
|
|
|
handleOauthParamsValidation(resp);
|
|
|
|
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'accept_required') {
|
|
|
|
dispatch(routeActions.push('/oauth/permissions'));
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'access_denied') {
|
|
|
|
// user declined permissions
|
|
|
|
location.href = resp.redirectUri;
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
|
|
|
});
|
2016-02-27 16:23:58 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOAuthRequest(oauth) {
|
|
|
|
return {
|
|
|
|
client_id: oauth.clientId,
|
|
|
|
redirect_uri: oauth.redirectUrl,
|
|
|
|
response_type: oauth.responseType,
|
|
|
|
scope: oauth.scope,
|
|
|
|
state: oauth.state
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleOauthParamsValidation(resp = {}) {
|
|
|
|
if (resp.statusCode === 400 && resp.error === 'invalid_request') {
|
|
|
|
alert(`Invalid request (${resp.parameter} required).`);
|
|
|
|
}
|
|
|
|
if (resp.statusCode === 400 && resp.error === 'unsupported_response_type') {
|
|
|
|
alert(`Invalid response type '${resp.parameter}'.`);
|
|
|
|
}
|
|
|
|
if (resp.statusCode === 400 && resp.error === 'invalid_scope') {
|
|
|
|
alert(`Invalid scope '${resp.parameter}'.`);
|
|
|
|
}
|
|
|
|
if (resp.statusCode === 401 && resp.error === 'invalid_client') {
|
|
|
|
alert('Can not find application you are trying to authorize.');
|
|
|
|
}
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export const SET_CLIENT = 'set_client';
|
|
|
|
export function setClient({id, name, description}) {
|
|
|
|
return {
|
|
|
|
type: SET_CLIENT,
|
|
|
|
payload: {id, name, description}
|
|
|
|
};
|
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
|
|
|
export const SET_OAUTH = 'set_oauth';
|
|
|
|
export function setOAuthRequest(oauth) {
|
|
|
|
return {
|
|
|
|
type: SET_OAUTH,
|
|
|
|
payload: {
|
|
|
|
clientId: oauth.client_id,
|
|
|
|
redirectUrl: oauth.redirect_uri,
|
|
|
|
responseType: oauth.response_type,
|
|
|
|
scope: oauth.scope,
|
|
|
|
state: oauth.state
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|