2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
/* global axios */
|
|
|
|
/* eslint no-undef: "error" */
|
|
|
|
|
|
|
|
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);
|
2019-08-21 07:29:56 +00:00
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.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,
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
isLoggedIn() {
|
2021-04-02 10:01:22 +00:00
|
|
|
const hasAuthCookie = !!Cookies.getJSON('auth_data');
|
|
|
|
const hasUserCookie = !!Cookies.getJSON('user');
|
|
|
|
return hasAuthCookie && hasUserCookie;
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
isAdmin() {
|
|
|
|
if (this.isLoggedIn()) {
|
|
|
|
return Cookies.getJSON('user').role === 'administrator';
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
getAuthData() {
|
|
|
|
if (this.isLoggedIn()) {
|
|
|
|
return Cookies.getJSON('auth_data');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2019-10-16 21:48:07 +00:00
|
|
|
getPubSubToken() {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (this.isLoggedIn()) {
|
2021-04-02 10:01:22 +00:00
|
|
|
const user = Cookies.getJSON('user') || {};
|
|
|
|
const { pubsub_token: pubsubToken } = user;
|
|
|
|
return pubsubToken;
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
getCurrentUser() {
|
|
|
|
if (this.isLoggedIn()) {
|
|
|
|
return Cookies.getJSON('user');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
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,
|
|
|
|
...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 => {
|
|
|
|
const value = profileAttributes[key];
|
|
|
|
if (value) {
|
|
|
|
formData.append(`profile[${key}]`, value);
|
|
|
|
}
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
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 },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-10-02 05:46:12 +00:00
|
|
|
updateAvailability({ availability }) {
|
|
|
|
return axios.put(endPoints('profileUpdate').url, {
|
|
|
|
profile: { availability },
|
|
|
|
});
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|