2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
/* global axios */
|
|
|
|
/* eslint no-undef: "error" */
|
|
|
|
/* eslint-env browser */
|
|
|
|
/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true }] */
|
|
|
|
|
|
|
|
import moment from 'moment';
|
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import endPoints from './endPoints';
|
2019-08-25 14:29:28 +00:00
|
|
|
import { frontendURL } from '../helper/URLHelper';
|
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 => {
|
|
|
|
const expiryDate = moment.unix(response.headers.expiry);
|
|
|
|
Cookies.set('auth_data', response.headers, {
|
|
|
|
expires: expiryDate.diff(moment(), 'days'),
|
|
|
|
});
|
|
|
|
Cookies.set('user', response.data.data, {
|
|
|
|
expires: expiryDate.diff(moment(), 'days'),
|
|
|
|
});
|
|
|
|
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 => {
|
|
|
|
const expiryDate = moment.unix(response.headers.expiry);
|
|
|
|
Cookies.set('auth_data', response.headers, {
|
|
|
|
expires: expiryDate.diff(moment(), 'days'),
|
|
|
|
});
|
|
|
|
Cookies.set('user', response.data.data, {
|
|
|
|
expires: expiryDate.diff(moment(), 'days'),
|
|
|
|
});
|
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
return fetchPromise;
|
|
|
|
},
|
|
|
|
validityCheck() {
|
|
|
|
const urlData = endPoints('validityCheck');
|
|
|
|
const fetchPromise = new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
axios
|
|
|
|
.get(urlData.url)
|
|
|
|
.then(response => {
|
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (error.response.status === 401) {
|
|
|
|
Cookies.remove('auth_data');
|
|
|
|
Cookies.remove('user');
|
2019-08-25 14:29:28 +00:00
|
|
|
window.location = frontendURL('login');
|
2019-08-21 07:29:56 +00:00
|
|
|
}
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
return fetchPromise;
|
|
|
|
},
|
|
|
|
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 => {
|
|
|
|
Cookies.remove('auth_data');
|
|
|
|
Cookies.remove('user');
|
2019-08-25 14:29:28 +00:00
|
|
|
window.location = frontendURL('login');
|
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 }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
axios
|
|
|
|
.post('auth/confirmation', {
|
|
|
|
confirmation_token: confirmationToken,
|
|
|
|
})
|
|
|
|
.then(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 => {
|
|
|
|
const expiryDate = moment.unix(response.headers.expiry);
|
|
|
|
Cookies.set('auth_data', response.headers, {
|
|
|
|
expires: expiryDate.diff(moment(), 'days'),
|
|
|
|
});
|
|
|
|
Cookies.set('user', response.data.data, {
|
|
|
|
expires: expiryDate.diff(moment(), 'days'),
|
|
|
|
});
|
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error.response);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
resetPassword({ email }) {
|
|
|
|
const urlData = endPoints('resetPassword');
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
axios
|
|
|
|
.post(urlData.url, { email })
|
|
|
|
.then(response => {
|
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error.response);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|