Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-cookie

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-cookie - npm Package Compare versions

Comparing version 1.0.5 to 2.0.1

lib/Cookies.js

56

package.json
{
"name": "react-cookie",
"version": "1.0.5",
"description": "Load and save cookies within your React application",
"main": "build/cookie.js",
"version": "2.0.1",
"description": "Universal cookies for React",
"main": "lib/index.js",
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "https://github.com/thereactivestack/react-cookie.git"
"url": "https://github.com/reactivestack/cookies.git"
},
"bugs": "https://github.com/thereactivestack/react-cookie/issues",
"homepage": "https://github.com/thereactivestack/react-cookie#readme",
"bugs": "https://github.com/reactivestack/cookies/issues",
"homepage": "https://github.com/reactivestack/cookies/packages/react-cookie/#readme",
"keywords": [
"universal",
"isomophic",
"cookie",
"cookies",
"react",
"reactjs",
"jsx"
"react"
],
"author": "Benoit Tremblay <benoit@thereactivestack.com>",
"author": "Benoit Tremblay <benoit@reactivestack.com>",
"license": "MIT",
"scripts": {
"lint": "eslint --ext .js .",
"test": "npm run lint && jest",
"watch": "jest --watch",
"build": "babel src -d build --ignore __tests__"
"build": "rimraf lib && babel src -d lib --ignore __tests__"
},
"config": {
"ghooks": {
"pre-commit": "npm test"
}
},
"dependencies": {
"cookie": "^0.3.1",
"is-node": "^1.0.2",
"object-assign": "^4.1.0"
"hoist-non-react-statics": "^1.2.0",
"prop-types": "^15.0.0",
"react": "^15.0.0",
"universal-cookie": "^2.0.1"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-eslint": "^7.1.1",
"babel-jest": "^17.0.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-latest": "^6.16.0",
"babel-preset-stage-2": "^6.18.0",
"babel-register": "^6.18.0",
"eslint": "^3.10.2",
"eslint-config-cyprex": "^1.1.1",
"eslint-plugin-flowtype": "^2.30.3",
"ghooks": "^1.3.2",
"jest": "^17.0.3"
"babel-cli": "^6.24.1",
"react-dom": "^15.0.0",
"rimraf": "^2.6.1"
}
}

@@ -1,104 +0,151 @@

# react-cookie
[![npm version](https://badge.fury.io/js/react-cookie.svg)](https://badge.fury.io/js/react-cookie)
[![Build Status](https://travis-ci.org/thereactivestack/react-cookie.svg?branch=master)](https://travis-ci.org/thereactivestack/react-cookie)
<br />
Load, save and remove cookies on the browser or Node.js
<h3 align="center">
<a href="packages/react-cookie">react-cookie</a>
</h3>
## Install
`npm install react-cookie --save`
<p align="center">
Universal cookies for <a href="https://facebook.github.io/react">React</a><br />
<a href="https://badge.fury.io/js/react-cookie"><img src="https://badge.fury.io/js/react-cookie.svg" /></a>
</p>
## Isomorphic cookies!
To be able to access user cookies while doing server-rendering, you can use [`plugToRequest`](#user-content-plugtorequestreq-res-unplug) or [`setRawCookie`](#user-content-setrawcookiecookies).
## API
### load(name, [doNotParse])
Load the cookie value.<br />
[![Build Status](https://travis-ci.org/reactivestack/cookies.svg?branch=master)](https://travis-ci.org/reactivestack/cookies)
<br />
Returns `undefined` if the cookie does not exist.<br />
Deserialize any cookie starting with { or [ unless `dotNotParse` is `true`.
[![Sauce Test Status](https://saucelabs.com/browser-matrix/coookies.svg)](https://saucelabs.com/u/coookies)
## Integrations
- [`universal-cookie-express`](https://www.npmjs.com/package/universal-cookie-express)
### select([regex])
Find all the cookies with a name that match the regex.<br />
<br />
Returns an `object` with the cookie name as the key.
## Getting started
### save(name, val, [[options]](#user-content-options))
Set a cookie
`npm install react-cookie`
### remove(name, [[options]](#user-content-options))
Remove a cookie
## `<CookiesProvider />`
Set the user cookies
### plugToRequest(req, res): unplug()
Load the user cookies so you can do server-rendering and match the same result.<br />
Also send back to the user the new cookies.<br />
Work with connect or express.js by using the cookieParser middleware first.<br />
Use `const unplug = plugToRequest(req, res)` just before your `renderToString`.<br />
<br />
Returns `unplug()` function so it stops setting cookies on the response.
On the server, the `cookies` props must be set using `req.universalCookie` or `new Cookie(cookieHeader)`
## `withCookies(Component)`
Give access to cookies by providing it using the `cookies` prop.
### setRawCookie(cookies)
Load the user cookies so you can do server-rendering and match the same result.<br />
Use `setRawCookie(headers.cookie)` just before your `renderToString`.<br />
Make sure it is the raw string from the request headers.<br />
## Cookies
## Options
Support all the cookie options from the [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.2.1).
### `get(name, [options])`
Get a cookie value
- name (string): cookie name
- options (object):
- doNotParse (boolean): do not convert the cookie into an object no matter what
### path
> cookie path<br />
> Use `/` as the path if you want your cookie to be accessible on all pages.
### `getAll([options])`
Get all cookies
- options (object):
- doNotParse (boolean): do not convert the cookie into an object no matter what
### expires
> absolute expiration date for the cookie **(Date object)**
### `set(name, value, [options])`
Set a cookie value
- name (string): cookie name
- value (string|object): save the value and stringify the object if needed
- options (object): Support all the cookie options from RFC 6265
- path (string): cookie path, use `/` as the path if you want your cookie to be accessible on all pages
- expires (Date): absolute expiration date for the cookie
- maxAge (number): relative max age of the cookie from when the client receives it in second
- domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
- secure (boolean): Is only accessible through HTTPS?
- httpOnly (boolean): Is only the server can access the cookie?
### maxAge
> relative max age of the cookie from when the client receives it **(seconds)**
## Example
```js
// src/components/App.js
import React, { Component } from 'react';
import { instanceOf } from 'prop-types';
import { withCookies, Cookies } from 'react-cookie';
### domain
> domain for the cookie<br />
> Use `sub.domain.com` if you want to access the cookie on a specific sub-domain only.<br />
> Use `.yourdomain.com` if you want to access the cookie in all your subdomains.
import NameForm from './NameForm';
### secure
> Is only accessible through HTTPS? **true or false**
class App extends Component {
static propTypes = {
cookies: instanceOf(Cookies).isRequired
};
### httpOnly
> Is only the server can access the cookie? **true or false**
componentWillMount() {
const { cookies } = this.props;
# Example
this.state = {
name: cookies.get('name') || 'Ben'
};
}
```js
import { Component } from 'react';
import cookie from 'react-cookie';
handleNameChange(name) {
const { cookies } = this.props;
import LoginPanel from './LoginPanel';
import Dashboard from './Dashboard';
cookies.set('name', name, { path: '/' });
this.setState({ name });
}
export default class MyApp extends Component {
componentWillMount() {
this.state = { userId: cookie.load('userId') };
}
render() {
const { name } = this.state;
onLogin(userId) {
this.setState({ userId });
cookie.save('userId', userId, { path: '/' });
return (
<div>
<NameForm name={name} onChange={this.handleNameChange.bind(this)} />
{this.state.name && <h1>Hello {this.state.name}!</h1>}
</div>
);
}
}
onLogout() {
cookie.remove('userId', { path: '/' });
}
export default withCookies(App);
render() {
if (!this.state.userId) {
return <LoginPanel onSuccess={this.onLogin.bind(this)} />;
}
// src/server.js
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { CookiesProvider } from 'react-cookie';
return <Dashboard userId={this.state.userId} />;
import Html from './components/Html';
import App from './components/App';
export default function middleware(req, res) {
const markup = ReactDOMServer.renderToString(
<CookiesProvider cookies={req.universalCookies}>
<App />
</CookiesProvider>
);
const html = ReactDOMServer.renderToStaticMarkup(<Html markup={markup} />);
res.send('<!DOCTYPE html>' + html);
}
}
```
## License
This project is under the MIT license. You are free to do whatever you want with it.
// src/client.js
import React from 'react';
import ReactDOM from 'react-dom';
import { CookiesProvider } from 'react-cookie';
import App from './components/App';
const appEl = document.getElementById('main-app');
ReactDOM.render(
<CookiesProvider>
<App />
</CookiesProvider>,
appEl
);
// server.js
require('babel-register');
const express = require('express');
const serverMiddleware = require('./src/server').default;
const cookiesMiddleware = require('universal-cookie-express');
const app = express();
app
.use('/assets', express.static('dist'))
.use(cookiesMiddleware())
.use(serverMiddleware);
app.listen(8080, function() {
console.log('Listening on 8080...'); // eslint-disable-line no-console
});
```
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