continue dispatch-server
This commit is contained in:
28
apps/dispatch-server/modules/socketJWT.ts
Normal file
28
apps/dispatch-server/modules/socketJWT.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { ExtendedError, Server, Socket } from "socket.io";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { pubClient } from "modules/redis";
|
||||
|
||||
if (!process.env.DISPATCH_APP_TOKEN)
|
||||
throw new Error("DISPATCH_APP_TOKEN is not defined");
|
||||
|
||||
export const jwtMiddleware = async (
|
||||
socket: Socket,
|
||||
next: (err?: ExtendedError) => void,
|
||||
) => {
|
||||
try {
|
||||
const token = socket.handshake.auth?.token;
|
||||
console.log(socket.handshake);
|
||||
if (!token) return new Error("Authentication error");
|
||||
const decoded = jwt.verify(token, process.env.DISPATCH_APP_TOKEN!);
|
||||
// socket.data.userId = decoded.; // User ID lokal speichern
|
||||
|
||||
// Prüfen, ob der Nutzer ein Disponent ist
|
||||
const userId: any = decoded;
|
||||
socket.data.user = decoded; // Lokal speichern
|
||||
|
||||
// In Redis speichern: Key = 'connected_dispatchers', Feld = socket.id, Wert = JSON-String des Users
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
next(new Error("Authentication error"));
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user