Commit eaac296f authored by Matija Obreza's avatar Matija Obreza

Cookie handling

parent 62d0a96a
......@@ -142,26 +142,30 @@ const fixNumberFilter = (filter) => {
};
export function saveCookies(resp, expireOn: number, apiUrl: string) {
const domain = apiUrl.includes('.') ? `.${apiUrl.split('.').filter((item, index, arr) => index > arr.length - 3).join('.')}` : 'localhost';
// const domain = apiUrl.includes('.') ? `.${apiUrl.split('.').filter((item, index, arr) => index > arr.length - 3).join('.')}` : 'localhost';
const expDate = new Date(expireOn);
log(`Saving cookies to expire on ${expDate}`);
cookies.set('access_token', resp.access_token, {domain, path: '/', expires: expDate });
cookies.set('authorities', JSON.stringify(resp.authorities), {domain, path: '/', expires: expDate });
cookies.set('access_token', resp.access_token, {path: '/', expires: expDate });
cookies.set('authorities', JSON.stringify(resp.authorities), {path: '/', expires: expDate });
if (resp.refresh_token) {
cookies.set('refresh_token', resp.refresh_token, {domain, path: '/', expires: expDate });
cookies.set('refresh_token', resp.refresh_token, {path: '/', expires: expDate });
} else {
document.cookie = `refresh_token=; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
document.cookie = `refresh_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
cookies.remove('refresh_token');
}
}
export function clearCookies() {
const domain = typeof document !== 'undefined' && document.location.origin.includes('.')
? `.${document.location.origin.split('.').filter((item, index, arr) => index > arr.length - 3).join('.')}`
: 'localhost';
// const domain = typeof document !== 'undefined' && document.location.origin.includes('.')
// ? `.${document.location.origin.split('.').filter((item, index, arr) => index > arr.length - 3).join('.')}`
// : 'localhost';
log('Clearing cookies');
['authorities', 'access_token', 'refresh_token'].forEach((key) => document.cookie = `${key}=; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`);
['authorities', 'access_token', 'refresh_token'].forEach((key) => {
cookies.remove(key);
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
});
}
export function isNumeric(value: any): boolean {
......
......@@ -32,13 +32,13 @@ export default function checkAuthToken(req, res, dispatch) {
console.log('Init checkAuthToken method');
return checkAuthTokenRequest(req, dispatch)
.then((data) => {
const domain = config.apiUrl.includes('.') ? `.${config.apiUrl.split('.').filter((item, index, arr) => index > arr.length - 3).join('.')}` : 'localhost';
// const domain = config.apiUrl.includes('.') ? `.${config.apiUrl.split('.').filter((item, index, arr) => index > arr.length - 3).join('.')}` : 'localhost';
console.log(`Setting cookie to expire in ${(data.exp || data.expires_in) / 60}min from`, data);
res.cookie('access_token', data.access_token, {domain, path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
res.cookie('access_token', data.access_token, { path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
if (data.authorities) {
console.log(`Setting authorities cookies: `, data.authorities);
res.cookie('authorities', JSON.stringify(data.authorities), {domain, path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
res.cookie('authorities', JSON.stringify(data.authorities), { path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
} else {
res.clearCookie('authorities');
}
......
......@@ -24,16 +24,16 @@ export const createApiCaller = (method, payloadType: string, config: {withoutTim
dispatch({ type: payloadType, payload: { apiCall: ApiCall.start() } }); // Loading
const accessToken = getState().applicationConfig.anonToken.access_token;
// const accessToken = getState().applicationConfig.anonToken.access_token;
const xhrConfig: any = {
cancelToken: cancelToken && cancelToken.token,
baseURL: getState().applicationConfig.apiUrl,
headers: {
common: {
Authorization: `Bearer ${accessToken}`,
},
},
// baseURL: getState().applicationConfig.apiUrl,
// headers: {
// common: {
// Authorization: `Bearer ${accessToken}`,
// },
// },
dispatch,
};
......@@ -92,16 +92,16 @@ export const createPureApiCaller = (method, config: {direct?: boolean, withoutTi
cancelToken = axios.CancelToken.source();
}
const accessToken = getState().applicationConfig.anonToken.access_token;
// const accessToken = getState().applicationConfig.anonToken.access_token;
const xhrConfig: any = {
cancelToken: cancelToken && cancelToken.token,
baseURL: getState().applicationConfig.apiUrl,
headers: {
common: {
Authorization: `Bearer ${accessToken}`,
},
},
// baseURL: getState().applicationConfig.apiUrl,
// headers: {
// common: {
// Authorization: `Bearer ${accessToken}`,
// },
// },
dispatch,
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment