Skip to content

Commit

Permalink
update socket auth middlewareto use httpOnly cookie from header
Browse files Browse the repository at this point in the history
  • Loading branch information
huytran17 committed May 28, 2024
1 parent 1bb5ec3 commit 382ae07
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions core/server/src/config/socket.io/middlewares/verify-client.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { NextFunction, Request, Response } from "express";
import { NextFunction } from "express";
import { Socket } from "socket.io";
import { VerifyAccessToken } from "../../accessTokenManager/verify-access-token";

export default function makeVerifyClient({
verifyAccessToken,
}: {
verifyAccessToken: VerifyAccessToken;
}) {
return async function verifyClient(
req: Request & { _query: { sid: string | undefined } },
res: Response,
next: NextFunction
) {
return async function verifyClient(socket: Socket, next: NextFunction) {
try {
const isHandshake = req._query.sid === undefined;
if (!isHandshake) {
return next();
}
const cookies = socket.handshake?.headers?.cookie?.split(";") || [];
const access_token = cookies
.filter((cookie) => cookie.includes("access_token"))
?.join()
?.replace("access_token=", "");

const access_token = req.cookies?.access_token;
verifyAccessToken(access_token);
verifyAccessToken(access_token.trim());

next();
} catch (error) {
Expand Down

0 comments on commit 382ae07

Please sign in to comment.