This commit is contained in:
Oleg Maslov
2026-09-02 10:14:22 +02:00
parent 0c3e2ead3b
commit b20b138fe4
771 changed files with 161561 additions and 9027 deletions

View File

@@ -4,6 +4,7 @@ import { createClerkClient, verifyToken } from '@clerk/fastify';
import { ConnectionManager } from './connection-manager.js';
import { teams, messages, users } from '../db/schema.js';
import { eq } from 'drizzle-orm';
import { TeamService } from '../services/team-service.js';
export const connectionManager = new ConnectionManager();
@@ -29,6 +30,8 @@ export function setWsTokenVerifier(verifier: WsTokenVerifier | null): void {
}
export async function wsGateway(fastify: FastifyInstance) {
const teamService = new TeamService(fastify.db);
// Create Clerk client for JWT verification if secret key is available
const clerkSecretKey = fastify.config.clerkSecretKey;
const clerk = clerkSecretKey
@@ -121,6 +124,10 @@ export async function wsGateway(fastify: FastifyInstance) {
return;
}
if (teamId && userId) {
connectionManager.remove(teamId, userId);
}
teamId = null;
userId = user.id;
socket.send(JSON.stringify({ type: 'authenticated', userId }));
} catch {
@@ -150,6 +157,12 @@ export async function wsGateway(fastify: FastifyInstance) {
return;
}
const membership = await teamService.getMembership(team.id, userId);
if (!membership) {
socket.send(JSON.stringify({ type: 'error', message: 'Team not found' }));
return;
}
// Leave previous team if any
if (teamId && userId) {
connectionManager.remove(teamId, userId);