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, {
|
|
|
|
account_name: creds.name,
|
|
|
|
email: creds.email,
|
|
|
|
})
|
|
|
|
.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() {
|
2019-08-21 07:29:56 +00:00
|
|
|
return !!Cookies.getJSON('auth_data');
|
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()) {
|
2019-10-16 21:48:07 +00:00
|
|
|
return Cookies.getJSON('user').pubsub_token;
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
getCurrentUser() {
|
|
|
|
if (this.isLoggedIn()) {
|
|
|
|
return Cookies.getJSON('user');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
verifyPasswordToken({ confirmationToken }) {
|
2019-12-21 17:24:35 +00:00
|
|
|
return axios.post('auth/confirmation', {
|
|
|
|
confirmation_token: confirmationToken,
|
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
|
|
|
|
|
|
|
profileUpdate({ name, email, password, password_confirmation, avatar }) {
|
|
|
|
const formData = new FormData();
|
|
|
|
if (name) {
|
|
|
|
formData.append('profile[name]', name);
|
|
|
|
}
|
|
|
|
if (email) {
|
|
|
|
formData.append('profile[email]', email);
|
|
|
|
}
|
|
|
|
if (password && password_confirmation) {
|
|
|
|
formData.append('profile[password]', password);
|
|
|
|
formData.append('profile[password_confirmation]', password_confirmation);
|
|
|
|
}
|
|
|
|
if (avatar) {
|
|
|
|
formData.append('profile[avatar]', avatar);
|
|
|
|
}
|
|
|
|
return axios.put(endPoints('profileUpdate').url, formData);
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|