more logging stuff
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { desc } from "drizzle-orm";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { logs } from "../db/schema/logs.schema.js";
|
||||
import type { RoomId } from "./types.socket.js";
|
||||
@@ -6,6 +7,11 @@ type RoomDefinition<T = unknown> = {
|
||||
seed: (limit: number) => Promise<T[]>;
|
||||
};
|
||||
|
||||
export const protectedRooms: any = {
|
||||
logs: { requiresAuth: true, role: "admin" },
|
||||
admin: { requiresAuth: true, role: "admin" },
|
||||
};
|
||||
|
||||
export const roomDefinition: Record<RoomId, RoomDefinition> = {
|
||||
logs: {
|
||||
seed: async (limit) => {
|
||||
@@ -13,7 +19,7 @@ export const roomDefinition: Record<RoomId, RoomDefinition> = {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(logs)
|
||||
.orderBy(logs.createdAt)
|
||||
.orderBy(desc(logs.createdAt))
|
||||
.limit(limit);
|
||||
|
||||
return rows; //.reverse();
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Server as HttpServer } from "node:http";
|
||||
//import { fileURLToPath } from "node:url";
|
||||
import { instrument } from "@socket.io/admin-ui";
|
||||
import { Server } from "socket.io";
|
||||
import { auth } from "utils/auth.utils.js";
|
||||
import { createLogger } from "../logger/logger.controller.js";
|
||||
import { allowedOrigins } from "../utils/cors.utils.js";
|
||||
import { registerEmitter } from "./roomEmitter.socket.js";
|
||||
@@ -12,6 +13,16 @@ import { createRoomEmitter, preseedRoom } from "./roomService.socket.js";
|
||||
//const __dirname = dirname(__filename);
|
||||
const log = createLogger({ module: "socket.io", subModule: "setup" });
|
||||
|
||||
//import type { Session, User } from "better-auth"; // adjust if needed
|
||||
import { protectedRooms } from "./roomDefinitions.socket.js";
|
||||
|
||||
// declare module "socket.io" {
|
||||
// interface Socket {
|
||||
// user?: User | any;
|
||||
// session?: Session;
|
||||
// }
|
||||
// }
|
||||
|
||||
export const setupSocketIORoutes = (baseUrl: string, server: HttpServer) => {
|
||||
const io = new Server(server, {
|
||||
path: `${baseUrl}/api/socket.io`,
|
||||
@@ -25,6 +36,38 @@ export const setupSocketIORoutes = (baseUrl: string, server: HttpServer) => {
|
||||
const { addDataToRoom } = createRoomEmitter(io);
|
||||
registerEmitter(addDataToRoom);
|
||||
|
||||
io.use(async (socket, next) => {
|
||||
try {
|
||||
//const cookieHeader = socket.handshake.headers.cookie;
|
||||
const headers = new Headers();
|
||||
|
||||
for (const [key, value] of Object.entries(socket.request.headers)) {
|
||||
if (typeof value === "string") {
|
||||
headers.set(key, value);
|
||||
} else if (Array.isArray(value)) {
|
||||
headers.set(key, value.join(", "));
|
||||
}
|
||||
}
|
||||
|
||||
const session = await auth.api.getSession({
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return next(); // allow connection, but no auth
|
||||
}
|
||||
|
||||
if (session) {
|
||||
socket.user = session.user;
|
||||
socket.session = session as any;
|
||||
}
|
||||
|
||||
next();
|
||||
} catch (err) {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
io.on("connection", (s) => {
|
||||
log.info({}, `User connected: ${s.id}`);
|
||||
|
||||
@@ -35,6 +78,21 @@ export const setupSocketIORoutes = (baseUrl: string, server: HttpServer) => {
|
||||
});
|
||||
|
||||
s.on("join-room", async (rn) => {
|
||||
const config = protectedRooms[rn];
|
||||
|
||||
if (config?.requiresAuth && !s.user) {
|
||||
return s.emit("room-error", {
|
||||
room: rn,
|
||||
message: "Authentication required",
|
||||
});
|
||||
}
|
||||
|
||||
if (config?.role && s.user?.role !== config.role) {
|
||||
return s.emit("room-error", {
|
||||
room: rn,
|
||||
message: `Not authorized to be in room: ${rn}`,
|
||||
});
|
||||
}
|
||||
s.join(rn);
|
||||
|
||||
// get room seeded
|
||||
|
||||
9
backend/types/socket.d.ts
vendored
Normal file
9
backend/types/socket.d.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
import "socket.io";
|
||||
import type { Session, User } from "better-auth"; // adjust if needed
|
||||
|
||||
declare module "socket.io" {
|
||||
interface Socket {
|
||||
user?: User | any;
|
||||
session?: Session;
|
||||
}
|
||||
}
|
||||
@@ -32,15 +32,15 @@ export const auth = betterAuth({
|
||||
schema,
|
||||
}),
|
||||
trustedOrigins: allowedOrigins,
|
||||
// user: {
|
||||
// additionalFields: {
|
||||
// role: {
|
||||
// type: "string",
|
||||
// //required: false,
|
||||
// input: false,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
user: {
|
||||
additionalFields: {
|
||||
role: {
|
||||
type: "string",
|
||||
//required: false,
|
||||
input: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
jwt({ jwt: { expirationTime: "1h" } }),
|
||||
//apiKey(),
|
||||
@@ -137,3 +137,5 @@ export const auth = betterAuth({
|
||||
// },
|
||||
},
|
||||
});
|
||||
|
||||
type Session = typeof auth.$Infer.Session;
|
||||
|
||||
Reference in New Issue
Block a user