Added Connection Component, Store, JWT logic
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import "dotenv/config";
|
||||
import express from "express";
|
||||
import { createServer } from "http";
|
||||
import { handle } from "socket-events/connect-dispatch";
|
||||
import { Server } from "socket.io";
|
||||
import { createAdapter } from "@socket.io/redis-adapter";
|
||||
import { jwtMiddleware } from "modules/socketJWT";
|
||||
import { jwtMiddleware } from "modules/socketJWTmiddleware";
|
||||
import { pubClient, subClient } from "modules/redis";
|
||||
|
||||
const app = express();
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { ExtendedError, Server, Socket } from "socket.io";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { pubClient } from "modules/redis";
|
||||
|
||||
import { prisma } from "@repo/db";
|
||||
if (!process.env.DISPATCH_APP_TOKEN)
|
||||
throw new Error("DISPATCH_APP_TOKEN is not defined");
|
||||
|
||||
@@ -10,17 +8,19 @@ export const jwtMiddleware = async (
|
||||
next: (err?: ExtendedError) => void,
|
||||
) => {
|
||||
try {
|
||||
const token = socket.handshake.auth?.token;
|
||||
console.log(socket.handshake);
|
||||
const { uid } = socket.handshake.auth;
|
||||
if (!uid) return new Error("Authentication error");
|
||||
/* const token = socket.handshake.auth?.token;
|
||||
if (!token) return new Error("Authentication error");
|
||||
const decoded = jwt.verify(token, process.env.DISPATCH_APP_TOKEN!);
|
||||
const decoded = jwt.verify(token, process.env.DISPATCH_APP_TOKEN!); */
|
||||
// socket.data.userId = decoded.; // User ID lokal speichern
|
||||
const user = await prisma.user.findUniqueOrThrow({
|
||||
where: { id: uid },
|
||||
});
|
||||
|
||||
// Prüfen, ob der Nutzer ein Disponent ist
|
||||
const userId: any = decoded;
|
||||
socket.data.user = decoded; // Lokal speichern
|
||||
socket.data.user = user;
|
||||
|
||||
// In Redis speichern: Key = 'connected_dispatchers', Feld = socket.id, Wert = JSON-String des Users
|
||||
next();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
next(new Error("Authentication error"));
|
||||
Reference in New Issue
Block a user