Skip to content

Commit

Permalink
feat(ssr): allow session to be used in ssr (#23)
Browse files Browse the repository at this point in the history
You can now use `withSession` in `getServerSideProps` functions, like this:

```js
export const getServerSideProps = withSession(async function({ req, res }) {
  const user = req.session.get("user");

  if (user === undefined) {
    res.setHeader("location", "/login");
    res.statusCode = 302;
    res.end();
    return;
  }

  return {
    props: { user: req.session.get("user") }
  };
});
```

fixes #10
  • Loading branch information
vvo authored Mar 20, 2020
1 parent c93ecc6 commit 1797edb
Show file tree
Hide file tree
Showing 20 changed files with 147,623 additions and 67 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.now
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"editor.formatOnSave": true,
"search.exclude": {
".yarn": true,
"example/.yarn": true,
"yarn.lock": true
},
"eslint.packageManager": "yarn",
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ This method of storing session data is the same technique used by **frameworks l

**By default the cookie has an ⏰ expiration time of 15 days**, set via [`maxAge`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#Directives). After that, even if someone tries to reuse the cookie, `next-iron-session` will not accept the underlying seal because the expiration is part of the seal value. See https://hapi.dev/family/iron for more information on @hapi/iron mechanisms.

**Next.js's** 🗿[Static generation](https://nextjs.org/docs/basic-features/pages#static-generation-recommended) (SG) and ⚙️[Server-side Rendering](https://nextjs.org/docs/basic-features/pages#server-side-rendering) (SSG) are both supported.

_Table of contents:_

- [Installation](#installation)
Expand All @@ -44,11 +46,13 @@ npm add next-iron-session

## Usage

You can find a more complete real-world example in the [example folder](./example/).

The password is a private key you must pass at runtime, it has to be at least 32 characters long. Use https://1password.com/password-generator/ to generate strong passwords.

Store passwords in secret environment variables on your platform.
⚠️ Store passwords in secret environment variables on your platform.

**login.js**:
**pages/api/login.js**:

```js
import withIronSession from "iron-session";
Expand All @@ -67,7 +71,7 @@ export default withIronSession(handler, {
});
```

**user.js**:
**pages/user.js**:

```js
import withIronSession from "iron-session";
Expand All @@ -82,7 +86,7 @@ export default withIronSession(handler, {
});
```

**logout.js**:
**pages/api/logout.js**:

```js
import withIronSession from "iron-session";
Expand Down
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.now
Loading

1 comment on commit 1797edb

@vercel
Copy link

@vercel vercel bot commented on 1797edb Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.