Cookies
Skip the hard steps to manage the cookie in the front-end, use the standard from W3C.
Install
$ npm install --save @katherineheadshall/cookies
Usage
setObject
import { setObject } from '@katherineheadshall/cookies';
let user = setObject('user', {id: 1, username: 'example user'});
console.log(user); // returns the parsed stored cookie in cookie format
Example
getObject
import { getObject } from '@katherineheadshall/cookies';
// can also be imported as comonjs module:
// const cookies = require('@katherineheadshall/simple-storage');
// Getting an already stored user object
getObject('user').then(user => {
console.log(user);
// => {object}
});
API
// you can provide an default value if the cookie cannot be found, this also works for getObject
storage.get('cookieName', null)
cookieName
:
string
cookies-available-methods |
Output |
---|---|
get |
string or defaultValue |
set |
string |
setObject |
any |
getObject |
string or defaultValue |
remove |
string |
Remove an already created Cookie
// commonjs
cookies.remove('cookieName')
// es6
remove('cookieName')