Skip to content

Commit

Permalink
docs: only execute the passport middleware once
Browse files Browse the repository at this point in the history
Before this change, the session and user context were retrieved once
per HTTP request and not once per session.
  • Loading branch information
darrachequesne committed Jan 13, 2024
1 parent 914a8bd commit 0bbe8ae
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
30 changes: 30 additions & 0 deletions examples/passport-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,33 @@ $ npm ci && npm start
```

And point your browser to `http://localhost:3000`. Optionally, specify a port by supplying the `PORT` env variable.

## How it works

The Socket.IO server retrieves the user context from the session:

```js
function onlyForHandshake(middleware) {
return (req, res, next) => {
const isHandshake = req._query.sid === undefined;
if (isHandshake) {
middleware(req, res, next);
} else {
next();
}
};
}

io.engine.use(onlyForHandshake(sessionMiddleware));
io.engine.use(onlyForHandshake(passport.session()));
io.engine.use(
onlyForHandshake((req, res, next) => {
if (req.user) {
next();
} else {
res.writeHead(401);
res.end();
}
}),
);
```
20 changes: 14 additions & 6 deletions examples/passport-example/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const sessionMiddleware = session({

app.use(sessionMiddleware);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(passport.initialize());
app.use(passport.session());

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

const io = new Server(httpServer);

io.engine.use(sessionMiddleware);
io.engine.use(passport.initialize());
io.engine.use(passport.session());
function onlyForHandshake(middleware) {
return (req, res, next) => {
const isHandshake = req._query.sid === undefined;
if (isHandshake) {
middleware(req, res, next);
} else {
next();
}
};
}

io.engine.use(onlyForHandshake(sessionMiddleware));
io.engine.use(onlyForHandshake(passport.session()));
io.engine.use(
(req, res, next) => {
onlyForHandshake((req, res, next) => {
if (req.user) {
next();
} else {
res.writeHead(401);
res.end();
}
},
}),
);

io.on("connection", (socket) => {
Expand Down
20 changes: 14 additions & 6 deletions examples/passport-example/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const sessionMiddleware = session({

app.use(sessionMiddleware);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(passport.initialize());
app.use(passport.session());

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

const io = new Server(httpServer);

io.engine.use(sessionMiddleware);
io.engine.use(passport.initialize());
io.engine.use(passport.session());
function onlyForHandshake(middleware) {
return (req, res, next) => {
const isHandshake = req._query.sid === undefined;
if (isHandshake) {
middleware(req, res, next);
} else {
next();
}
};
}

io.engine.use(onlyForHandshake(sessionMiddleware));
io.engine.use(onlyForHandshake(passport.session()));
io.engine.use(
(req, res, next) => {
onlyForHandshake((req, res, next) => {
if (req.user) {
next();
} else {
res.writeHead(401);
res.end();
}
},
}),
);

io.on("connection", (socket) => {
Expand Down
29 changes: 22 additions & 7 deletions examples/passport-example/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import express = require("express");
import { createServer, ServerResponse } from "http";
import { createServer } from "http";
import { Server } from "socket.io";
import session from "express-session";
import { type Request } from "express";
import { type Request, type Response } from "express";
import bodyParser = require("body-parser");
import passport = require("passport");
import { Strategy as LocalStrategy } from "passport-local";
Expand Down Expand Up @@ -91,19 +91,34 @@ passport.deserializeUser((user: Express.User, cb) => {

const io = new Server(httpServer);

io.engine.use(sessionMiddleware);
io.engine.use(passport.initialize());
io.engine.use(passport.session());
function onlyForHandshake(
middleware: (req: Request, res: Response, next: any) => void,
) {
return (
req: Request & { _query: Record<string, string> },
res: Response,
next: (err?: Error) => void,
) => {
const isHandshake = req._query.sid === undefined;
if (isHandshake) {
middleware(req, res, next);
} else {
next();
}
};
}

io.engine.use(onlyForHandshake(sessionMiddleware));
io.engine.use(onlyForHandshake(passport.session()));
io.engine.use(
(req: { user: Express.User }, res: ServerResponse, next: Function) => {
onlyForHandshake((req, res, next) => {
if (req.user) {
next();
} else {
res.writeHead(401);
res.end();
}
},
}),
);

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

0 comments on commit 0bbe8ae

Please sign in to comment.