Skip to content

Commit 3f506f7

Browse files
vvoVandivier
andauthored
feat(TypeScript): Add session to handler req + example (#356)
Co-authored-by: Vandivier <john@afterecon.com>
1 parent ebf2424 commit 3f506f7

33 files changed

+150334
-11
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
examples/next.js/
2+
examples/next-typescript/
23
examples/express/node_modules
34
dist/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ yarn-debug.log*
1313
yarn-error.log*
1414

1515
.now
16+
.next

README.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ _Table of contents:_
2727

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

5960
## Usage
6061

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

6364
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.
6465

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

67-
**pages/api/login.js**:
68-
6968
```js
69+
// pages/api/login.js
70+
7071
import { withIronSession } from "next-iron-session";
7172

7273
async function handler(req, res) {
@@ -89,9 +90,9 @@ export default withIronSession(handler, {
8990
});
9091
```
9192

92-
**pages/api/user.js**:
93-
9493
```js
94+
// pages/api/user.js
95+
9596
import { withIronSession } from "next-iron-session";
9697

9798
function handler(req, res, session) {
@@ -109,9 +110,9 @@ export default withIronSession(handler, {
109110
});
110111
```
111112

112-
**pages/api/logout.js**:
113-
114113
```js
114+
// pages/api/logout.js
115+
115116
import { withIronSession } from "next-iron-session";
116117

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

139+
## TypeScript usage
140+
141+
Also see the [full TypeScript example](./examples/next-typescript).
142+
143+
```ts
144+
// pages/api/login.ts
145+
import { NextApiRequest, NextApiResponse } from "next";
146+
import { withIronSession, Session } from "next-iron-session";
147+
type NextIronRequest = NextApiRequest & { session: Session };
148+
149+
async function handler(
150+
req: NextIronRequest,
151+
res: NextApiResponse,
152+
): Promise<void> {
153+
// get user from database then:
154+
req.session.set("user", {
155+
id: 230,
156+
admin: true,
157+
});
158+
await req.session.save();
159+
res.send("Logged in");
160+
}
161+
162+
export default withIronSession(handler, {
163+
password: "complex_password_at_least_32_characters_long",
164+
cookieName: "myapp_cookiename",
165+
// if your localhost is served on http:// then disable the secure flag
166+
cookieOptions: {
167+
secure: process.env.NODE_ENV === "production",
168+
},
169+
});
170+
```
171+
138172
## Examples
139173

140174
### Handle password rotation/update the password

examples/express/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "express",
2+
"name": "next-iron-session-express-example",
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ⚠️ The SECRET_COOKIE_PASSWORD should never be inside your repository directly, it's here only to ease
2+
# the example deployment
3+
# For local development, you should store it inside a `.env.local` gitignored file
4+
# See https://nextjs.org/docs/basic-features/environment-variables#loading-environment-variables
5+
6+
SECRET_COOKIE_PASSWORD=2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ⚠️ The SECRET_COOKIE_PASSWORD should never be inside your repository directly, it's here only to ease
2+
# the example deployment
3+
# For production you should use https://vercel.com/blog/environment-variables-ui if you're hosted on Vercel or
4+
# any other secret environment variable mean
5+
6+
SECRET_COOKIE_PASSWORD=2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8

0 commit comments

Comments
 (0)