2019-10-22 07:58:45 +00:00
|
|
|
/* eslint no-param-reassign: 0 */
|
2020-09-14 09:44:26 +00:00
|
|
|
import fromUnixTime from 'date-fns/fromUnixTime';
|
|
|
|
import differenceInDays from 'date-fns/differenceInDays';
|
2020-02-16 11:50:38 +00:00
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import { frontendURL } from '../../helper/URLHelper';
|
2019-10-22 07:58:45 +00:00
|
|
|
|
2020-08-09 12:43:17 +00:00
|
|
|
Cookies.defaults = { sameSite: 'Lax' };
|
|
|
|
|
2019-10-22 07:58:45 +00:00
|
|
|
export const getLoadingStatus = state => state.fetchAPIloadingStatus;
|
|
|
|
export const setLoadingStatus = (state, status) => {
|
|
|
|
state.fetchAPIloadingStatus = status;
|
|
|
|
};
|
2020-02-16 11:50:38 +00:00
|
|
|
|
|
|
|
export const setUser = (userData, expiryDate) =>
|
|
|
|
Cookies.set('user', userData, {
|
2020-09-14 09:44:26 +00:00
|
|
|
expires: differenceInDays(expiryDate, new Date()),
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|
|
|
|
|
2020-09-14 09:44:26 +00:00
|
|
|
export const getHeaderExpiry = response =>
|
|
|
|
fromUnixTime(response.headers.expiry);
|
2020-02-16 11:50:38 +00:00
|
|
|
|
|
|
|
export const setAuthCredentials = response => {
|
|
|
|
const expiryDate = getHeaderExpiry(response);
|
|
|
|
Cookies.set('auth_data', response.headers, {
|
2020-09-14 09:44:26 +00:00
|
|
|
expires: differenceInDays(expiryDate, new Date()),
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|
|
|
|
setUser(response.data.data, expiryDate);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const clearCookiesOnLogout = () => {
|
|
|
|
Cookies.remove('auth_data');
|
|
|
|
Cookies.remove('user');
|
|
|
|
window.location = frontendURL('login');
|
|
|
|
};
|