2019-08-14 09:48:44 +00:00
|
|
|
/* global axios */
|
|
|
|
|
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import endPoints from './endPoints';
|
2020-02-16 11:50:38 +00:00
|
|
|
import { setAuthCredentials, clearCookiesOnLogout } from '../store/utils/api';
|
2019-12-21 17:24:35 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
export default {
|
|
|
|
login(creds) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
axios
|
|
|
|
.post('auth/sign_in', creds)
|
|
|
|
.then(response => {
|
2019-12-21 17:24:35 +00:00
|
|
|
setAuthCredentials(response);
|
2022-03-03 15:19:51 +00:00
|
|
|
resolve(response.data);
|
2019-08-21 07:29:56 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error.response);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
register(creds) {
|
|
|
|
const urlData = endPoints('register');
|
|
|
|
const fetchPromise = new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
axios
|
|
|
|
.post(urlData.url, {
|
2020-12-21 08:35:19 +00:00
|
|
|
account_name: creds.accountName.trim(),
|
|
|
|
user_full_name: creds.fullName.trim(),
|
2019-08-21 07:29:56 +00:00
|
|
|
email: creds.email,
|
2021-06-07 11:56:08 +00:00
|
|
|
password: creds.password,
|
2022-02-18 14:32:50 +00:00
|
|
|
h_captcha_client_response: creds.hCaptchaClientResponse,
|
2019-08-21 07:29:56 +00:00
|
|
|
})
|
|
|
|
.then(response => {
|
2019-12-21 17:24:35 +00:00
|
|
|
setAuthCredentials(response);
|
2019-08-21 07:29:56 +00:00
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
return fetchPromise;
|
|
|
|
},
|
|
|
|
validityCheck() {
|
|
|
|
const urlData = endPoints('validityCheck');
|
2020-02-16 11:50:38 +00:00
|
|
|
return axios.get(urlData.url);
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
logout() {
|
|
|
|
const urlData = endPoints('logout');
|
|
|
|
const fetchPromise = new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
axios
|
|
|
|
.delete(urlData.url)
|
|
|
|
.then(response => {
|
2019-12-21 17:24:35 +00:00
|
|
|
clearCookiesOnLogout();
|
2019-08-21 07:29:56 +00:00
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
return fetchPromise;
|
|
|
|
},
|
2022-04-14 15:24:26 +00:00
|
|
|
hasAuthCookie() {
|
|
|
|
return !!Cookies.getJSON('cw_d_session_info');
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
getAuthData() {
|
2022-04-14 15:24:26 +00:00
|
|
|
if (this.hasAuthCookie()) {
|
|
|
|
return Cookies.getJSON('cw_d_session_info');
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
verifyPasswordToken({ confirmationToken }) {
|
2021-06-07 11:56:08 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post('auth/confirmation', {
|
|
|
|
confirmation_token: confirmationToken,
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
setAuthCredentials(response);
|
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error.response);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setNewPassword({ resetPasswordToken, password, confirmPassword }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
axios
|
|
|
|
.put('auth/password', {
|
|
|
|
reset_password_token: resetPasswordToken,
|
|
|
|
password_confirmation: confirmPassword,
|
|
|
|
password,
|
|
|
|
})
|
|
|
|
.then(response => {
|
2020-02-16 11:50:38 +00:00
|
|
|
setAuthCredentials(response);
|
2019-08-21 07:29:56 +00:00
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error.response);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
resetPassword({ email }) {
|
|
|
|
const urlData = endPoints('resetPassword');
|
2019-12-21 17:24:35 +00:00
|
|
|
return axios.post(urlData.url, { email });
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
2020-02-16 11:50:38 +00:00
|
|
|
|
2020-07-27 16:49:26 +00:00
|
|
|
profileUpdate({
|
|
|
|
password,
|
|
|
|
password_confirmation,
|
|
|
|
displayName,
|
2022-02-15 06:40:53 +00:00
|
|
|
avatar,
|
2020-07-27 16:49:26 +00:00
|
|
|
...profileAttributes
|
|
|
|
}) {
|
2020-02-16 11:50:38 +00:00
|
|
|
const formData = new FormData();
|
2020-07-04 06:12:47 +00:00
|
|
|
Object.keys(profileAttributes).forEach(key => {
|
2022-02-15 05:08:24 +00:00
|
|
|
const hasValue = profileAttributes[key] === undefined;
|
|
|
|
if (!hasValue) {
|
|
|
|
formData.append(`profile[${key}]`, profileAttributes[key]);
|
2020-07-04 06:12:47 +00:00
|
|
|
}
|
|
|
|
});
|
2020-07-27 16:49:26 +00:00
|
|
|
formData.append('profile[display_name]', displayName || '');
|
2020-02-16 11:50:38 +00:00
|
|
|
if (password && password_confirmation) {
|
|
|
|
formData.append('profile[password]', password);
|
|
|
|
formData.append('profile[password_confirmation]', password_confirmation);
|
|
|
|
}
|
2022-02-15 06:40:53 +00:00
|
|
|
if (avatar) {
|
|
|
|
formData.append('profile[avatar]', avatar);
|
|
|
|
}
|
2020-02-16 11:50:38 +00:00
|
|
|
return axios.put(endPoints('profileUpdate').url, formData);
|
|
|
|
},
|
2020-10-02 05:46:12 +00:00
|
|
|
|
2021-01-10 13:55:33 +00:00
|
|
|
updateUISettings({ uiSettings }) {
|
|
|
|
return axios.put(endPoints('profileUpdate').url, {
|
|
|
|
profile: { ui_settings: uiSettings },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-10-07 07:51:46 +00:00
|
|
|
updateAvailability(availabilityData) {
|
|
|
|
return axios.post(endPoints('availabilityUpdate').url, {
|
|
|
|
profile: { ...availabilityData },
|
2020-10-02 05:46:12 +00:00
|
|
|
});
|
|
|
|
},
|
2021-11-26 19:26:07 +00:00
|
|
|
|
|
|
|
deleteAvatar() {
|
|
|
|
return axios.delete(endPoints('deleteAvatar').url);
|
|
|
|
},
|
2022-10-06 00:31:12 +00:00
|
|
|
|
|
|
|
setActiveAccount({ accountId }) {
|
|
|
|
const urlData = endPoints('setActiveAccount');
|
|
|
|
return axios.put(urlData.url, {
|
|
|
|
profile: {
|
|
|
|
account_id: accountId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|