Socket
Socket
Sign inDemoInstall

isomorphic-cookie

Package Overview
Dependencies
Maintainers
3
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

isomorphic-cookie

Load and save cookies on the client and server


Version published
Weekly downloads
13K
increased by3.05%
Maintainers
3
Weekly downloads
 
Created
Source

Load, save and remove cookies within your isomorphic application

Isomorphic cookies!

The initial idea for this package came from the popular react-cookie package, but we've made it a little harder to make serious mistakes using plugToRequest() with async server operations. Instead of providing that function, that allows you to only read/write to one request at a time (the current plugged request), we've allowed for passing the request to the load() function and the response to the save() and remove() functions. This way it's easier to reason about exactly which request/response you're interacting with.

This currently supports Express and Hapi servers. If you need to support another server framework, feel free to send a PR or make a feature request in issues.

Download

npm install @discodigital/isomorphic-cookie

Examples

On the client:

const isomorphicCookie = require('isomorphic-cookie');

console.log(isomorphicCookie.load('serverCookie'));
console.log(isomorphicCookie.load('clientCookie'));

isomorphicCookie.save('clientCookie', 'clientCookie value here');

On the server (Hapi):

'use strict';

const Hapi = require('hapi');
const path = require('path');
const isomorphicCookie = require('isomorphic-cookie');

// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
  host: 'localhost',
  port: 8000,
});

let callIndex = 0;

server.register(require('inert'), (err) => {
  if (err) {
    throw err;
  }

  server.route({
    method: 'GET',
    path:'/',
    handler: (request, reply) => {
      callIndex++;

      console.log(`client cookie: ${isomorphicCookie.load('clientCookie', request)}`);
      console.log(`server cookie: ${isomorphicCookie.load('serverCookie', request)}`);

      isomorphicCookie.save('serverCookie', `serverCookie, call #: ${callIndex}`, {}, reply);

      reply.file(path.join(__dirname, 'index.html'));
    },
  });

  server.start((err) => {
    if (err) {
      throw err;
    }
    console.log('Server running at:', server.info.uri);
  });
});

On the server (Express):

const express = require('express');
const cookieParser = require('cookie-parser');
const path = require('path');

const isomorphicCookie = require('isomorphic-cookie');

const app = express();

app.use(express.static('dist'));
app.use(cookieParser());

app.get('/', (req, res) => {
  console.log(`client cookie: ${isomorphicCookie.load('clientCookie', req)}`);
  console.log(`server cookie: ${isomorphicCookie.load('serverCookie', req)}`);

  isomorphicCookie.save('serverCookie', `serverCookie, call #: ${callIndex}`, {}, res);

  res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(8000);

Usage

isomorphicCookie.load(name, [request], [doNotParse])

isomorphicCookie.save(name, val, [options], [response])

isomorphicCookie.remove(name, [options], [response])

options (object)

path

cookie path

expires

absolute expiration date for the cookie (Date object)

domain

domain for the cookie

secure

defaults to true

License

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

Keywords

FAQs

Package last updated on 14 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc