2016-06-24 00:29:55 +02:00
|
|
|
import * as ActionTypes from '../../constants';
|
|
|
|
import { browserHistory } from 'react-router';
|
|
|
|
import axios from 'axios';
|
2016-06-09 22:28:21 +02:00
|
|
|
|
|
|
|
|
2016-06-20 19:29:32 +02:00
|
|
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
2016-06-09 22:28:21 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export function authError(error) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.AUTH_ERROR,
|
|
|
|
payload: error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-09 22:28:21 +02:00
|
|
|
export function signUpUser(formValues) {
|
2016-06-24 00:29:55 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.post(`${ROOT_URL}/signup`, formValues, { withCredentials: true })
|
2016-06-09 22:28:21 +02:00
|
|
|
.then(response => {
|
2016-06-14 20:46:40 +02:00
|
|
|
dispatch({ type: ActionTypes.AUTH_USER,
|
|
|
|
user: response.data
|
|
|
|
});
|
2016-06-09 22:28:21 +02:00
|
|
|
browserHistory.push('/');
|
|
|
|
})
|
|
|
|
.catch(response => dispatch(authError(response.data.error)));
|
2016-06-24 00:29:55 +02:00
|
|
|
};
|
2016-06-10 00:41:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-02 23:31:07 +02:00
|
|
|
// export function loginUser(formValues) {
|
|
|
|
// return (dispatch) => {
|
|
|
|
// axios.post(`${ROOT_URL}/login`, formValues, { withCredentials: true })
|
|
|
|
// .then(response => {
|
|
|
|
// dispatch({ type: ActionTypes.AUTH_USER,
|
|
|
|
// user: response.data
|
|
|
|
// });
|
|
|
|
// browserHistory.push('/');
|
|
|
|
// })
|
|
|
|
// .catch(response => {
|
|
|
|
// return Promise.reject({ email: response.data.message, _error: 'Login failed!' });
|
|
|
|
// });
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
|
2016-06-14 22:48:16 +02:00
|
|
|
export function loginUser(formValues) {
|
2016-09-02 23:31:07 +02:00
|
|
|
return axios.post(`${ROOT_URL}/login`, formValues, { withCredentials: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loginUserSuccess(user) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.AUTH_USER,
|
|
|
|
user
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loginUserFailure(error) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.AUTH_ERROR,
|
|
|
|
error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateAndLoginUser(formProps, dispatch) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
loginUser(formProps)
|
2016-06-14 22:48:16 +02:00
|
|
|
.then(response => {
|
|
|
|
dispatch({ type: ActionTypes.AUTH_USER,
|
2016-09-02 23:31:07 +02:00
|
|
|
user: response.data
|
2016-06-14 22:48:16 +02:00
|
|
|
});
|
|
|
|
browserHistory.push('/');
|
2016-09-02 23:31:07 +02:00
|
|
|
resolve();
|
2016-06-14 22:48:16 +02:00
|
|
|
})
|
2016-09-02 23:31:07 +02:00
|
|
|
.catch(response => {
|
2016-09-30 04:02:06 +02:00
|
|
|
reject({ password: response.data.message, _error: 'Login failed!' });
|
2016-09-02 23:31:07 +02:00
|
|
|
});
|
|
|
|
});
|
2016-06-14 22:48:16 +02:00
|
|
|
}
|
2016-06-10 00:41:40 +02:00
|
|
|
|
2016-06-15 01:11:42 +02:00
|
|
|
export function getUser() {
|
2016-06-24 00:29:55 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.get(`${ROOT_URL}/session`, { withCredentials: true })
|
2016-06-15 01:11:42 +02:00
|
|
|
.then(response => {
|
2016-06-24 00:29:55 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.AUTH_USER,
|
2016-06-15 01:11:42 +02:00
|
|
|
user: response.data
|
|
|
|
});
|
2016-08-09 23:50:45 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_PREFERENCES,
|
|
|
|
preferences: response.data.preferences
|
|
|
|
});
|
2016-06-15 01:11:42 +02:00
|
|
|
})
|
2016-08-10 00:45:59 +02:00
|
|
|
.catch(response => {
|
|
|
|
dispatch(authError(response.data.error));
|
|
|
|
});
|
2016-06-10 00:41:40 +02:00
|
|
|
};
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
2016-08-28 02:46:20 +02:00
|
|
|
|
|
|
|
export function logoutUser() {
|
|
|
|
return (dispatch) => {
|
|
|
|
axios.get(`${ROOT_URL}/logout`, { withCredentials: true })
|
|
|
|
.then(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.UNAUTH_USER
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => dispatch(authError(response.data.error)));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-12 23:19:43 +02:00
|
|
|
export function initiateResetPassword(formValues) {
|
2016-10-12 20:25:24 +02:00
|
|
|
return (dispatch) => {
|
2016-10-19 17:34:08 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.RESET_PASSWORD_INITIATE
|
|
|
|
});
|
2016-10-12 23:19:43 +02:00
|
|
|
axios.post(`${ROOT_URL}/reset-password`, formValues, { withCredentials: true })
|
|
|
|
.then(() => {
|
2016-10-19 17:34:08 +02:00
|
|
|
// do nothing
|
2016-10-12 20:25:24 +02:00
|
|
|
})
|
2016-10-12 23:19:43 +02:00
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
message: response.data
|
|
|
|
}));
|
|
|
|
};
|
2016-10-12 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 23:19:43 +02:00
|
|
|
export function resetPasswordReset() {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.RESET_PASSWORD_RESET
|
|
|
|
};
|
|
|
|
}
|
2016-10-18 22:07:25 +02:00
|
|
|
|
|
|
|
export function validateResetPasswordToken(token) {
|
|
|
|
return (dispatch) => {
|
|
|
|
axios.get(`${ROOT_URL}/reset-password/${token}`)
|
|
|
|
.then(() => {
|
|
|
|
// do nothing if the token is valid
|
|
|
|
})
|
|
|
|
.catch(() => dispatch({
|
|
|
|
type: ActionTypes.INVALID_RESET_PASSWORD_TOKEN
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updatePassword(token, formValues) {
|
|
|
|
return (dispatch) => {
|
|
|
|
axios.post(`${ROOT_URL}/reset-password/${token}`, formValues)
|
|
|
|
.then((response) => {
|
|
|
|
dispatch(loginUserSuccess(response.data));
|
|
|
|
browserHistory.push('/');
|
|
|
|
})
|
|
|
|
.catch(() => dispatch({
|
|
|
|
type: ActionTypes.INVALID_RESET_PASSWORD_TOKEN
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|