Keys auf SSE umgestellt

This commit is contained in:
Bastian Wagner
2026-03-05 14:51:45 +01:00
parent f88fe93182
commit ac2117b64b
13 changed files with 217 additions and 15 deletions

View File

@@ -0,0 +1,38 @@
import { Controller, Get, Param, Query, Req, Sse, UnauthorizedException, UseGuards } from '@nestjs/common';
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
import { SseTicketService } from './sse-ticket.service';
import { AuthGuard } from 'src/core/guards/auth.guard';
import { Observable, interval, map } from 'rxjs';
import { KeyService } from 'src/modules/key/key.service';
import { AuthService } from 'src/modules/auth/auth.service';
import { User } from 'src/model/entitites';
import { UserService } from 'src/modules/user/user.service';
import { SseService } from './sse.service';
@Controller('sse')
export class SseController {
constructor(private ticketService: SseTicketService, private sseService: SseService, private userService: UserService) {}
@UseGuards(AuthGuard)
@Get('ticket')
getTicket(@Req() req: AuthenticatedRequest) {
return this.ticketService.generateTicket(req.user.id)
}
@Sse('key')
async sse(@Query('ticket') ticket: string): Promise<Observable<any>> {
const userId = this.ticketService.getUserIdToTicket(ticket);
if (!userId) throw new UnauthorizedException('Invalid/expired ticket');
const user = await this.getUserToId(userId);
if (!userId) throw new UnauthorizedException('Invalid/expired ticket');
return this.sseService.register(userId);
}
private async getUserToId(userId: string): Promise<User> {
const user = await this.userService.getUserById(userId);
return Promise.resolve(user);
}
}