Skip to content

Commit

Permalink
enable websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Varkoff committed Sep 21, 2024
1 parent 57f9c00 commit b04c9dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
7 changes: 4 additions & 3 deletions product.http
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
PUT http://localhost:1337/api/users/1 HTTP/1.1
POST https://ecommercecms.algomax.fr/api/auth/local HTTP/1.1
Content-Type: application/json
Authorization: bearer 86131120c2401cf0e477491a0768b11437c8e13e7bc1ad1781b0dba951c0cdd69bb6cf36b480d8f947536ab7bd528e81dfb99639572c96a9d72c12eeeea56afcf31b7441541d48aa5b6f25831796c61c603391de08c796c403f8373328c68ef9d9165754ce03b882d19a31ec1999b016a9bbbf59dac663b13a76639599689d81
Authorization: bearer 2b2ad6720307563b389fbec622cddfb7e0ea7b7c70f38ba184877758efd45c9b668b2838d6621ec2ff0ab67bb9c53424d5a69e6a1674ddc5b3bc505ae62d1cea010e35241e2774b3152c0c0750154a3d070dc7d891fc1db017dc7f75f19f6966f2d90d29d5bd296f6093ab412df6a45a34cfe1d3647be46859bf362783e4f63e
# Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNzI2NDg5NjQ3LCJleHAiOjE3MjkwODE2NDd9.lzHPjirpqqLJW35RvfSxbzYOUfHxDzttzGupYNCSRrQ


{
"username": "Alfred"
"identifier": "[email protected]",
"password":"abc123"
}
4 changes: 2 additions & 2 deletions src/api/produit/controllers/produit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Core, factories } from "@strapi/strapi";
import type { ExtendableContext } from "koa";
import Stripe from "stripe";
import { z } from "zod";
// import { io } from "../../..";
import { io } from "../../..";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2024-06-20",
Expand Down Expand Up @@ -343,7 +343,7 @@ const createStripeCheckout = async ({
},
});

// io.to(existingOrder.user.id).emit("checkout", session.url);
io.to(existingOrder.user.id).emit("checkout", session.url);
await strapi.plugins.email.services.email.send({
to: existingOrder.user.email,
from: "Strapi Ecommerce <[email protected]>",
Expand Down
54 changes: 27 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Core } from "@strapi/strapi";
// import { Server } from "socket.io";
import { Server } from "socket.io";

// export const io = new Server(strapi.server.httpServer, {
// cors: {
// origin: `${process.env.FRONTEND_URL}`,
// methods: ["GET", "POST"],
// },
// });
export const io = new Server(strapi.server.httpServer, {
cors: {
origin: `${process.env.FRONTEND_URL}`,
methods: ["GET", "POST"],
},
});

export default {
/**
Expand All @@ -25,26 +25,26 @@ export default {
* run jobs, or perform some special logic.
*/
bootstrap({ strapi }: { strapi: Core.Strapi }) {
// io.use(async (socket, next) => {
// try {
// //Socket Authentication
// const result = await strapi.plugins[
// "users-permissions"
// ].services.jwt.verify(socket.handshake.query.token);
io.use(async (socket, next) => {
try {
//Socket Authentication
const result = await strapi.plugins[
"users-permissions"
].services.jwt.verify(socket.handshake.query.token);

// console.log(result);
// //Save the User ID to the socket connection
// // @ts-ignore
// socket.user = result.id;
// next();
// } catch (error) {
// console.log(error);
// }
// }).on("connection", (socket) => {
// // @ts-ignore
// socket.join(socket.user)
// console.log(`socket connected ${socket.id}`);
// socket.emit('confirmation')
// });
console.log(result);
//Save the User ID to the socket connection
// @ts-ignore
socket.user = result.id;
next();
} catch (error) {
console.log(error);
}
}).on("connection", (socket) => {
// @ts-ignore
socket.join(socket.user)
console.log(`socket connected ${socket.id}`);
socket.emit('confirmation')
});
},
};

0 comments on commit b04c9dd

Please sign in to comment.