JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 21025
  • Score
    100M100P100Q135518F
  • License MIT

Load and save cookies on the client and server

Package Exports

  • isomorphic-cookie

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (isomorphic-cookie) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

isomorphic-cookie

Load, save and remove cookies within your isomorphic application

Isomorphic cookies!

You can also plug it directly with a Node.js request by adding just before the renderToString: var unplug = isomorphicCookie.plugToRequest(req, res);
(require the cookieParser middleware)

To ensure long running async operations do not attempt to alter cookies after the request has been sent, call the unplug function that is returned in a finally block in your router.

If you are within a non-browser or Node.js environment, you can use isomorphicCookie.setRawCookie(req.headers.cookie)

This supports Express and Hapi. If you need to support another framework, feel free to send a PR.

Download

npm install isomorphic-cookie

Examples

import { Component } from 'react';
import cookie from 'isomorphic-cookie';

export default class MyApp extends Component {
  constructor(props) {
    super(props);

    this.state =  { userId: cookie.load('userId') };
  }

  onLogin(userId) {
    this.setState({ userId });
    cookie.save('userId', userId, { path: '/' });
  }

  onLogout() {
    cookie.remove('userId', { path: '/' });

    /** Clear all cookies starting with 'session' (to get all cookies, omit regex argument) */
    Object.keys(cookie.select(/^session.*/i)).forEach(name => cookie.remove(name, { path: '/' }))
  }

  render() {
    return (
      <LoginPanel onSuccess={this.onLogin.bind(this)} />
    );
  }
}

Without CommonJS

You can use isomorphic-cookie with anything by using the global variable isomorphicCookie.

Note that window needs to exist to use isomorphicCookie.

Usage

isomorphicCookie.load(name, [doNotParse])

isomorphicCookie.select([regex])

isomorphicCookie.save(name, val, [opt])

isomorphicCookie.remove(name, [opt])

isomorphicCookie.plugToRequest(req, res): unplug()

isomorphicCookie.setRawCookie(cookies)

opt

Support all the cookie options from the RFC 6265.

path

cookie path

expires

absolute expiration date for the cookie (Date object)

maxAge

relative max age of the cookie from when the client receives it (seconds)

domain

domain for the cookie

secure

true or false

httpOnly

true or false

License

This project is under the MIT license. You are free to do whatever you want with it.