Skip to content

Commit

Permalink
feat(TypeScript): Add session to handler req + example (#356)
Browse files Browse the repository at this point in the history
Co-authored-by: Vandivier <[email protected]>
  • Loading branch information
vvo and Vandivier committed Jun 8, 2021
1 parent ebf2424 commit 3f506f7
Show file tree
Hide file tree
Showing 33 changed files with 150,334 additions and 11 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
examples/next.js/
examples/next-typescript/
examples/express/node_modules
dist/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ yarn-debug.log*
yarn-error.log*

.now
.next
48 changes: 41 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ _Table of contents:_

- [Installation](#installation)
- [Usage](#usage)
- [TypeScript usage](#typescript-usage)
- [Examples](#examples)
- [Handle password rotation/update the password](#handle-password-rotationupdate-the-password)
- [Express / Connect middleware: `ironSession`](#express--connect-middleware-ironsession)
Expand Down Expand Up @@ -58,15 +59,15 @@ yarn add next-iron-session

## Usage

You can find real-world examples (Next.js, Express) in the [examples folder](./examples/).
You can find full featured examples (Next.js, Express) in the [examples folder](./examples/).

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.

⚠️ Always store passwords in secret environment variables on your platform.

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

```js
// pages/api/login.js

import { withIronSession } from "next-iron-session";

async function handler(req, res) {
Expand All @@ -89,9 +90,9 @@ export default withIronSession(handler, {
});
```

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

```js
// pages/api/user.js

import { withIronSession } from "next-iron-session";

function handler(req, res, session) {
Expand All @@ -109,9 +110,9 @@ export default withIronSession(handler, {
});
```

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

```js
// pages/api/logout.js

import { withIronSession } from "next-iron-session";

function handler(req, res, session) {
Expand All @@ -135,6 +136,39 @@ export default withIronSession(handler, {
- a wrong password was used
- we can't find back the password id in the current list

## TypeScript usage

Also see the [full TypeScript example](./examples/next-typescript).

```ts
// pages/api/login.ts
import { NextApiRequest, NextApiResponse } from "next";
import { withIronSession, Session } from "next-iron-session";
type NextIronRequest = NextApiRequest & { session: Session };

async function handler(
req: NextIronRequest,
res: NextApiResponse,
): Promise<void> {
// get user from database then:
req.session.set("user", {
id: 230,
admin: true,
});
await req.session.save();
res.send("Logged in");
}

export default withIronSession(handler, {
password: "complex_password_at_least_32_characters_long",
cookieName: "myapp_cookiename",
// if your localhost is served on http:// then disable the secure flag
cookieOptions: {
secure: process.env.NODE_ENV === "production",
},
});
```

## Examples

### Handle password rotation/update the password
Expand Down
2 changes: 1 addition & 1 deletion examples/express/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "express",
"name": "next-iron-session-express-example",
"version": "0.0.0",
"private": true,
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions examples/next-typescript/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ⚠️ The SECRET_COOKIE_PASSWORD should never be inside your repository directly, it's here only to ease
# the example deployment
# For local development, you should store it inside a `.env.local` gitignored file
# See https://nextjs.org/docs/basic-features/environment-variables#loading-environment-variables

SECRET_COOKIE_PASSWORD=2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8
6 changes: 6 additions & 0 deletions examples/next-typescript/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ⚠️ The SECRET_COOKIE_PASSWORD should never be inside your repository directly, it's here only to ease
# the example deployment
# For production you should use https://vercel.com/blog/environment-variables-ui if you're hosted on Vercel or
# any other secret environment variable mean

SECRET_COOKIE_PASSWORD=2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8
Loading

1 comment on commit 3f506f7

@vercel
Copy link

@vercel vercel bot commented on 3f506f7 Jun 8, 2021

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.