Keys auf SSE umgestellt
This commit is contained in:
38
api/src/modules/realtime/sse/sse.controller.ts
Normal file
38
api/src/modules/realtime/sse/sse.controller.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user