spee.ch/react/utils/cookies.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-01-18 21:43:02 +01:00
const CHANNEL_NAME = 'CHANNEL_NAME';
const CHANNEL_SHORT_ID = 'CHANNEL_SHORT_ID';
const CHANNEL_LONG_ID = 'CHANNEL_LONG_ID';
2018-01-10 03:25:38 +01:00
module.exports = {
2018-01-18 21:43:02 +01:00
setCookie (key, value) {
document.cookie = `${key}=${value}`;
},
2018-01-10 03:25:38 +01:00
getCookie (cname) {
const name = cname + '=';
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
},
2018-01-18 21:43:02 +01:00
clearCookie (name) {
document.cookie = `${name}=; expires=Thu, 01-Jan-1970 00:00:01 GMT;`;
},
2018-01-10 20:26:01 +01:00
setUserCookies (channelName, shortChannelId, channelClaimId) {
2018-01-18 21:43:02 +01:00
module.exports.setCookie('CHANNEL_NAME', channelName);
module.exports.setCookie('CHANNEL_SHORT_ID', shortChannelId);
module.exports.setCookie('CHANNEL_LONG_ID', channelClaimId);
},
clearUserCookies () {
module.exports.clearCookie('CHANNEL_NAME');
module.exports.clearCookie('CHANNEL_SHORT_ID');
module.exports.clearCookie('CHANNEL_LONG_ID');
},
getUserCookies () {
return {
channelName : module.exports.getCookie('CHANNEL_NAME'),
channelShortId: module.exports.getCookie('CHANNEL_SHORT_ID'),
channelLongId : module.exports.getCookie('CHANNEL_LONG_ID'),
};
},
2018-01-18 21:43:02 +01:00
};