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.3 to 1.0.4

2

package.json
{
"name": "react-cookie",
"version": "1.0.3",
"version": "1.0.4",
"description": "Load and save cookies within your React application",

@@ -5,0 +5,0 @@ "main": "build/cookie.js",

@@ -5,19 +5,68 @@ # react-cookie

<br />
Load, save and remove cookies within your React application
Load, save and remove cookies on the browser or Node.js
## Install
`npm install react-cookie --save`
## Isomorphic cookies!
You can also plug it directly with a Node.js request by adding just before the renderToString: `var unplug = cookie.plugToRequest(req, res);`<br />
*(require the cookieParser middleware)*
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).
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.
## API
### load(name, [doNotParse])
Load the cookie value.<br />
<br />
Returns `undefined` if the cookie does not exist.<br />
Deserialize any cookie starting with { or [ unless `dotNotParse` is `true`.
If you are within a non-browser or Node.js environment, you can use `cookie.setRawCookie(req.headers.cookie)`
### 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.
### save(name, val, [[options]](#user-content-options))
Set a cookie
## Install
`npm install react-cookie`
### remove(name, [[options]](#user-content-options))
Remove a cookie
# Examples
### 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.
### 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 />
### Options
Support all the cookie options from the [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.2.1).
### path
> cookie path<br />
> Use `/` as the path if you want your cookie to be accessible on all pages.
### 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<br />
> Use `https://*.yourdomain.com` if you want to access the cookie in all your subdomains.
### secure
> Is only accessible through HTTPS? **true or false**
### httpOnly
> Is only the server can access the cookie? **true or false**
# Example
```js

@@ -27,6 +76,7 @@ import { Component } from 'react';

import LoginPanel from './LoginPanel';
import Dashboard from './Dashboard';
export default class MyApp extends Component {
constructor(props) {
super(props);
componentWillMount() {
this.state = { userId: cookie.load('userId') };

@@ -42,11 +92,10 @@ }

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)} />
);
if (!this.state.userId) {
return <LoginPanel onSuccess={this.onLogin.bind(this)} />;
}
return <Dashboard userId={this.state.userId} />;
}

@@ -56,33 +105,3 @@ }

## Usage
### `cookie.load(name, [doNotParse])`
### `cookie.select([regex])`
### `cookie.save(name, val, [opt])`
### `cookie.remove(name, [opt])`
### `cookie.plugToRequest(req, res): unplug()`
### `cookie.setRawCookie(cookies)`
## opt
Support all the cookie options from the [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.2.1).
### 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.
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