next-session
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -6,6 +6,8 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
exports.default = exports.useSession = void 0; | ||
var _crypto = _interopRequireDefault(require("crypto")); | ||
var _cookie = require("cookie"); | ||
var Promise = _interopRequireWildcard(require("bluebird")); | ||
@@ -17,3 +19,3 @@ | ||
var _cookie = _interopRequireDefault(require("./session/cookie")); | ||
var _cookie2 = _interopRequireDefault(require("./session/cookie")); | ||
@@ -132,4 +134,16 @@ var _session = _interopRequireDefault(require("./session/session")); | ||
const useSession = (req, res, opts) => { | ||
if (!req) return; | ||
if (!res) { | ||
throw new TypeError('Res not found! If you are passing handler, use the default import.'); | ||
} | ||
req.cookies = req.cookies || (0, _cookie.parse)(req.headers.cookie); | ||
return session(() => null, opts)(req, res); | ||
}; | ||
exports.useSession = useSession; | ||
session.Store = _store.default; | ||
session.Cookie = _cookie.default; | ||
session.Cookie = _cookie2.default; | ||
session.Session = _session.default; | ||
@@ -136,0 +150,0 @@ session.MemoryStore = _memory.default; |
{ | ||
"name": "next-session", | ||
"version": "1.0.0", | ||
"description": "Simple promise-based session middleware for Next.js API Routes", | ||
"version": "1.1.0", | ||
"description": "Simple promise-based session middleware for Next.js", | ||
"keywords": [ | ||
@@ -20,3 +20,4 @@ "javascript", | ||
"lint:fix": "eslint src -c .eslintrc --ext js --ignore-path .gitignore --fix", | ||
"test": "jest --coverage --coverageReporters=text-lcov | coveralls" | ||
"test": "jest --coverage --coverageReporters=text-lcov | coveralls", | ||
"test:local": "jest --coverage" | ||
}, | ||
@@ -23,0 +24,0 @@ "repository": { |
@@ -5,3 +5,3 @@ # next-session | ||
Simple *promise-based* session middleware for [Next.js](https://nextjs.org/) 9 API Routes. | ||
Simple *promise-based* session middleware for [Next.js](https://nextjs.org/). | ||
@@ -11,3 +11,3 @@ ## Installation | ||
```sh | ||
npm install --save next-session | ||
npm install next-session | ||
``` | ||
@@ -17,2 +17,4 @@ | ||
See a real-life usage in [nextjs-mongodb-app](https://github.com/hoangvvo/nextjs-mongodb-app). | ||
```javascript | ||
@@ -69,2 +71,32 @@ import session from 'next-session'; | ||
### Usage with `getInitialProps` | ||
*This is a temporary implementation for `getInitialProps`. Please be aware that it **will** be different in the next major version.* | ||
`next-session` may be used in `getInitialProps` by `await` calling `useSession` with arguments of `req` and `res`. | ||
```javascript | ||
import { useSession } from 'next-session'; | ||
const Page = () => { | ||
// React components | ||
} | ||
Page.getInitialProps = async ({ req, res }) => { | ||
await useSession(req, res); | ||
if (req.session.views) { | ||
// On later visits, increase # of views by one on every request | ||
req.session.views += 1; | ||
} else { | ||
// On first visit, set # of views to 1 | ||
req.session.views = 1; | ||
} | ||
return { | ||
views: req.session.views | ||
} | ||
} | ||
``` | ||
## API | ||
### session(handler, options) | ||
@@ -71,0 +103,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22851
286
175
0