Skip to content

Commit

Permalink
fix: being able to read cookie on socket
Browse files Browse the repository at this point in the history
fixing #133
  • Loading branch information
roman-ojha committed Jul 12, 2022
1 parent 66b389e commit 062adc1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client/src/pages/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const SignUp = (): JSX.Element => {
<input
className="SignUp_Page_Conform_Password_Input_Field"
type="password"
placeholder="Conform Password"
placeholder="Confirm Password"
name="cpassword"
value={userData.cpassword}
onChange={getUserData}
Expand Down
4 changes: 3 additions & 1 deletion client/src/services/socket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import io from "socket.io-client";

const socket = io(process.env.REACT_APP_API_BASE_URL as string);
const socket = io(process.env.REACT_APP_API_BASE_URL as string, {
withCredentials: true,
});

export default socket;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"connect-mongo": "^4.6.0",
"cookie": "^0.5.0",
"cookie-parser": "^1.4.5",
"cookie-session": "^2.0.0",
"cors": "^2.8.5",
Expand Down Expand Up @@ -59,6 +60,7 @@
"request": "^2.88.2",
"sharp": "^0.30.4",
"socket.io": "^4.4.1",
"socket.io-cookie-parser": "^1.0.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.4",
"usetube": "^2.1.4",
Expand Down
22 changes: 22 additions & 0 deletions socket/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@ import { createServer } from "http";
import express, { Express } from "express";
import { Server } from "socket.io";
import userDetail from "../models/userDetail_model.js";
import passport from "passport";
import session from "express-session";
import cookie from "cookie";
import socketCookieParse from "socket.io-cookie-parser";

// const sessionMiddleware = session({
// secret: process.env.SECRET_KEY as string,
// resave: false,
// saveUninitialized: false,
// });

const app: Express = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: { credentials: true, origin: process.env.CLIENT_BASE_URL },
cookie: true,
});

// const wrap = (middleware) => (socket, next) =>
// middleware(socket.request, {}, next);
// io.use(wrap(sessionMiddleware));
// io.use(wrap(passport.initialize()));
// io.use(wrap(passport.session()));
io.use(socketCookieParse());

// io.use((socket, next) => {
// next();
// });

io.on("connect", (socket) => {
socket.on("send-message", async (messageInfo, cb) => {
// NOTE : I have to authenticate before saving the message
Expand Down
11 changes: 9 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ conventional-commits-filter@^2.0.2, conventional-commits-filter@^2.0.7:
lodash.ismatch "^4.4.0"
modify-values "^1.0.0"

cookie-parser@^1.4.5:
cookie-parser@^1.3.2, cookie-parser@^1.4.5:
version "1.4.6"
resolved "https://registry.yarnpkg.com/cookie-parser/-/cookie-parser-1.4.6.tgz#3ac3a7d35a7a03bbc7e365073a26074824214594"
integrity sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==
Expand Down Expand Up @@ -2252,7 +2252,7 @@ cookie@0.4.2, cookie@^0.4.1, cookie@~0.4.1:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==

cookie@0.5.0:
cookie@0.5.0, cookie@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==
Expand Down Expand Up @@ -6607,6 +6607,13 @@ socket.io-adapter@~2.4.0:
resolved "https://registry.yarnpkg.com/socket.io-adapter/-/socket.io-adapter-2.4.0.tgz#b50a4a9ecdd00c34d4c8c808224daa1a786152a6"
integrity sha512-W4N+o69rkMEGVuk2D/cvca3uYsvGlMwsySWV447y99gUPghxq42BxqLNMndb+a1mm/5/7NeXVQS7RLa2XyXvYg==

socket.io-cookie-parser@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/socket.io-cookie-parser/-/socket.io-cookie-parser-1.0.0.tgz#41abd6cb557992d7b474b23e1b7d07c964817c61"
integrity sha512-XBr6UAVQBmI5EE0A019CNgeV7H7QVMHGeWt6OfVS8XH0Pjv9HOmH0/Ak5LX2j6xmH0fKfv6YFXAtjtfp3xmBlQ==
dependencies:
cookie-parser "^1.3.2"

socket.io-parser@~4.0.4:
version "4.0.5"
resolved "https://registry.yarnpkg.com/socket.io-parser/-/socket.io-parser-4.0.5.tgz#cb404382c32324cc962f27f3a44058cf6e0552df"
Expand Down

0 comments on commit 062adc1

Please sign in to comment.