2015-10-15 23:05:57 -04:00
|
|
|
|
var cookie = require('cookie');
|
|
|
|
|
var xhr = require('xhr');
|
|
|
|
|
|
2016-01-14 10:25:03 -05:00
|
|
|
|
/**
|
|
|
|
|
* Module that handles coookie interactions.
|
|
|
|
|
* (Cookies?!?! Jar?!?! Get it?!?! WE'RE AMAZING!!!!)
|
|
|
|
|
*
|
|
|
|
|
* get(name, callback) – can be sync or async, as callback is optional
|
|
|
|
|
* set(name, value) – synchronously sets the cookie
|
|
|
|
|
* use(name, uri, callback) – can by sync or async, gets cookie from the uri if not there.
|
|
|
|
|
*/
|
2015-12-07 17:43:04 -05:00
|
|
|
|
var Jar = {
|
|
|
|
|
get: function (name, callback) {
|
2015-10-15 23:05:57 -04:00
|
|
|
|
// Get cookie by name
|
|
|
|
|
var obj = cookie.parse(document.cookie) || {};
|
|
|
|
|
|
2015-12-07 17:43:04 -05:00
|
|
|
|
// Handle optional callback
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
if (typeof obj === 'undefined') return callback('Cookie not found.');
|
|
|
|
|
return callback(null, obj[name]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return obj[name];
|
|
|
|
|
},
|
|
|
|
|
use: function (name, uri, callback) {
|
|
|
|
|
// Attempt to get cookie
|
|
|
|
|
Jar.get(name, function (err, obj) {
|
|
|
|
|
if (typeof obj !== 'undefined') return callback(null, obj);
|
|
|
|
|
|
|
|
|
|
// Make XHR request to cookie setter uri
|
|
|
|
|
xhr({
|
|
|
|
|
uri: uri
|
|
|
|
|
}, function (err) {
|
|
|
|
|
if (err) return callback(err);
|
|
|
|
|
Jar.get(name, callback);
|
|
|
|
|
});
|
2015-10-15 23:05:57 -04:00
|
|
|
|
});
|
2015-12-07 17:43:04 -05:00
|
|
|
|
},
|
|
|
|
|
set: function (name, value) {
|
|
|
|
|
var obj = cookie.serialize(name, value);
|
|
|
|
|
var expires = '; expires=' + new Date(new Date().setYear(new Date().getFullYear() + 1)).toUTCString();
|
|
|
|
|
var path = '; path=/';
|
|
|
|
|
document.cookie = obj + expires + path;
|
|
|
|
|
}
|
2015-10-16 10:59:55 -04:00
|
|
|
|
};
|
2015-10-16 15:10:17 -04:00
|
|
|
|
|
|
|
|
|
module.exports = Jar;
|