Skip to content

Commit 0bbe8ae

Browse files
docs: only execute the passport middleware once
Before this change, the session and user context were retrieved once per HTTP request and not once per session.
1 parent 914a8bd commit 0bbe8ae

File tree

4 files changed

+80
-19
lines changed

4 files changed

+80
-19
lines changed

examples/passport-example/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,33 @@ $ npm ci && npm start
1414
```
1515

1616
And point your browser to `http://localhost:3000`. Optionally, specify a port by supplying the `PORT` env variable.
17+
18+
## How it works
19+
20+
The Socket.IO server retrieves the user context from the session:
21+
22+
```js
23+
function onlyForHandshake(middleware) {
24+
return (req, res, next) => {
25+
const isHandshake = req._query.sid === undefined;
26+
if (isHandshake) {
27+
middleware(req, res, next);
28+
} else {
29+
next();
30+
}
31+
};
32+
}
33+
34+
io.engine.use(onlyForHandshake(sessionMiddleware));
35+
io.engine.use(onlyForHandshake(passport.session()));
36+
io.engine.use(
37+
onlyForHandshake((req, res, next) => {
38+
if (req.user) {
39+
next();
40+
} else {
41+
res.writeHead(401);
42+
res.end();
43+
}
44+
}),
45+
);
46+
```

examples/passport-example/cjs/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const sessionMiddleware = session({
2020

2121
app.use(sessionMiddleware);
2222
app.use(bodyParser.urlencoded({ extended: false }));
23-
app.use(passport.initialize());
2423
app.use(passport.session());
2524

2625
app.get("/", (req, res) => {
@@ -78,19 +77,28 @@ passport.deserializeUser((user, cb) => {
7877

7978
const io = new Server(httpServer);
8079

81-
io.engine.use(sessionMiddleware);
82-
io.engine.use(passport.initialize());
83-
io.engine.use(passport.session());
80+
function onlyForHandshake(middleware) {
81+
return (req, res, next) => {
82+
const isHandshake = req._query.sid === undefined;
83+
if (isHandshake) {
84+
middleware(req, res, next);
85+
} else {
86+
next();
87+
}
88+
};
89+
}
8490

91+
io.engine.use(onlyForHandshake(sessionMiddleware));
92+
io.engine.use(onlyForHandshake(passport.session()));
8593
io.engine.use(
86-
(req, res, next) => {
94+
onlyForHandshake((req, res, next) => {
8795
if (req.user) {
8896
next();
8997
} else {
9098
res.writeHead(401);
9199
res.end();
92100
}
93-
},
101+
}),
94102
);
95103

96104
io.on("connection", (socket) => {

examples/passport-example/esm/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const sessionMiddleware = session({
2121

2222
app.use(sessionMiddleware);
2323
app.use(bodyParser.urlencoded({ extended: false }));
24-
app.use(passport.initialize());
2524
app.use(passport.session());
2625

2726
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -81,19 +80,28 @@ passport.deserializeUser((user, cb) => {
8180

8281
const io = new Server(httpServer);
8382

84-
io.engine.use(sessionMiddleware);
85-
io.engine.use(passport.initialize());
86-
io.engine.use(passport.session());
83+
function onlyForHandshake(middleware) {
84+
return (req, res, next) => {
85+
const isHandshake = req._query.sid === undefined;
86+
if (isHandshake) {
87+
middleware(req, res, next);
88+
} else {
89+
next();
90+
}
91+
};
92+
}
8793

94+
io.engine.use(onlyForHandshake(sessionMiddleware));
95+
io.engine.use(onlyForHandshake(passport.session()));
8896
io.engine.use(
89-
(req, res, next) => {
97+
onlyForHandshake((req, res, next) => {
9098
if (req.user) {
9199
next();
92100
} else {
93101
res.writeHead(401);
94102
res.end();
95103
}
96-
},
104+
}),
97105
);
98106

99107
io.on("connection", (socket) => {

examples/passport-example/ts/index.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import express = require("express");
2-
import { createServer, ServerResponse } from "http";
2+
import { createServer } from "http";
33
import { Server } from "socket.io";
44
import session from "express-session";
5-
import { type Request } from "express";
5+
import { type Request, type Response } from "express";
66
import bodyParser = require("body-parser");
77
import passport = require("passport");
88
import { Strategy as LocalStrategy } from "passport-local";
@@ -91,19 +91,34 @@ passport.deserializeUser((user: Express.User, cb) => {
9191

9292
const io = new Server(httpServer);
9393

94-
io.engine.use(sessionMiddleware);
95-
io.engine.use(passport.initialize());
96-
io.engine.use(passport.session());
94+
function onlyForHandshake(
95+
middleware: (req: Request, res: Response, next: any) => void,
96+
) {
97+
return (
98+
req: Request & { _query: Record<string, string> },
99+
res: Response,
100+
next: (err?: Error) => void,
101+
) => {
102+
const isHandshake = req._query.sid === undefined;
103+
if (isHandshake) {
104+
middleware(req, res, next);
105+
} else {
106+
next();
107+
}
108+
};
109+
}
97110

111+
io.engine.use(onlyForHandshake(sessionMiddleware));
112+
io.engine.use(onlyForHandshake(passport.session()));
98113
io.engine.use(
99-
(req: { user: Express.User }, res: ServerResponse, next: Function) => {
114+
onlyForHandshake((req, res, next) => {
100115
if (req.user) {
101116
next();
102117
} else {
103118
res.writeHead(401);
104119
res.end();
105120
}
106-
},
121+
}),
107122
);
108123

109124
io.on("connection", (socket) => {

0 commit comments

Comments
 (0)