Skip to content
This repository has been archived by the owner on Feb 24, 2018. It is now read-only.

Commit

Permalink
Add cookie storage support
Browse files Browse the repository at this point in the history
  • Loading branch information
marlon_trapp committed Nov 13, 2017
1 parent 11ee8ae commit bc54513
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -60,7 +60,8 @@
"jsnext:main": "es/index.js",
"types": "./index.d.ts",
"dependencies": {
"aws-sdk": "^2.6.0"
"aws-sdk": "^2.6.0",
"js-cookie": "^2.1.4"
},
"devDependencies": {
"babel-cli": "^6.23.0",
Expand All @@ -80,6 +81,6 @@
"react": "^16.0.0-alpha.6",
"react-native": "^0.44.0",
"rimraf": "^2.5.4",
"webpack": "^1.13.1"
"webpack": "^1.15.0"
}
}
70 changes: 70 additions & 0 deletions src/CookieStorage.js
@@ -0,0 +1,70 @@

import * as Cookies from "js-cookie";

/** @class */
export default class CookieStorage {

/**
* Constructs a new CookieStorage object
* @param {object} data Creation options.
* @param {string} data.domain Cookies domain (mandatory).
* @param {string} data.path Cookies path (default: '/')
* @param {integer} data.expires Cookie expiration (in days, default: 365)
*/
constructor(data) {
this.domain = data.domain;
if (data.path) {
this.path = data.path;
} else {
this.path = '/';
}
if (data.expire){
this.expires = data.expires;
} else {
this.expires = 365;
}
}

/**
* This is used to set a specific item in storage
* @param {string} key - the key for the item
* @param {object} value - the value
* @returns {string} value that was set
*/
setItem(key, value) {
Cookies.set(key, value, {path: this.path, expires: this.expires, domain: this.domain});
return Cookies.get(key);
}

/**
* This is used to get a specific key from storage
* @param {string} key - the key for the item
* This is used to clear the storage
* @returns {string} the data item
*/
getItem(key) {
return Cookies.get(key)
}

/**
* This is used to remove an item from storage
* @param {string} key - the key being set
* @returns {string} value - value that was deleted
*/
removeItem(key) {
return Cookies.remove(key, { path: this.path, domain: this.domain, secure: true});
}

/**
* This is used to clear the storage
* @returns {string} nothing
*/
clear() {
var cookies = Cookies.get();
var index;
for (index = 0; index < cookies.length; ++index) {
Cookies.remove(cookies[index]);
}
return {};
}
}
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -24,6 +24,7 @@ export { default as CognitoUser } from './CognitoUser';
export { default as CognitoUserAttribute } from './CognitoUserAttribute';
export { default as CognitoUserPool } from './CognitoUserPool';
export { default as CognitoUserSession } from './CognitoUserSession';
export { default as CookieStorage } from './CookieStorage';
export { default as DateHelper } from './DateHelper';

// The version of crypto-browserify included by aws-sdk only
Expand Down

0 comments on commit bc54513

Please sign in to comment.