summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authordittonjs <jditton.atomic@gmail.com>2022-03-08 18:25:58 -0700
committerGitHub <noreply@github.com>2022-03-08 18:25:58 -0700
commit5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6 (patch)
tree3e9cd6e10f81a65f76478910f6feb1b89d6ce1dc /server
parent15800f84a42b0aa53311ff88fa3d554653a2511a (diff)
parent1b7c6ee006b50ce29c9cb3ee42fad29b7db0f6f3 (diff)
downloadlocchat-5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6.tar.gz
locchat-5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6.zip
Merge pull request #4 from dittonjs/jd/add-websockets
Jd/add websockets
Diffstat (limited to 'server')
-rw-r--r--server/app.module.ts2
-rw-r--r--server/decorators/gateway_jwt_body.decorator.ts6
-rw-r--r--server/providers/gateways/ping.gateway.ts67
-rw-r--r--server/providers/guards/gatewayauth.guard.ts27
4 files changed, 102 insertions, 0 deletions
diff --git a/server/app.module.ts b/server/app.module.ts
index d0135c1..bbc3c1c 100644
--- a/server/app.module.ts
+++ b/server/app.module.ts
@@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { config } from './database/config';
import { UsersModule } from './modules/users.module';
+import { PingGateway } from './providers/gateways/ping.gateway';
import { AuthGuard } from './providers/guards/auth.guard';
import { RolesGuard } from './providers/guards/roles.guard';
import { JwtService } from './providers/services/jwt.service';
@@ -15,6 +16,7 @@ import { GuardUtil } from './providers/util/guard.util';
imports: [TypeOrmModule.forRoot(config), UsersModule],
controllers: [AppController],
providers: [
+ PingGateway,
UsersService,
RolesService,
JwtService,
diff --git a/server/decorators/gateway_jwt_body.decorator.ts b/server/decorators/gateway_jwt_body.decorator.ts
new file mode 100644
index 0000000..c31b47e
--- /dev/null
+++ b/server/decorators/gateway_jwt_body.decorator.ts
@@ -0,0 +1,6 @@
+import { createParamDecorator, ExecutionContext } from '@nestjs/common';
+import { Socket } from 'socket.io';
+export const GatewayJwtBody = createParamDecorator((data: unknown, ctx: ExecutionContext) => {
+ const req = ctx.switchToHttp().getRequest() as Socket;
+ return req.handshake.auth.jwtBody;
+});
diff --git a/server/providers/gateways/ping.gateway.ts b/server/providers/gateways/ping.gateway.ts
new file mode 100644
index 0000000..27fe785
--- /dev/null
+++ b/server/providers/gateways/ping.gateway.ts
@@ -0,0 +1,67 @@
+import { UseGuards } from '@nestjs/common';
+import { ConnectedSocket, MessageBody, OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit, SubscribeMessage, WebSocketGateway, WebSocketServer, WsException } from '@nestjs/websockets';
+import { GatewayJwtBody } from 'server/decorators/gateway_jwt_body.decorator';
+import { JwtBodyDto } from 'server/dto/jwt_body.dto';
+import { Server, Socket } from 'socket.io';
+import { GatewayAuthGuard } from '../guards/gatewayauth.guard';
+import { JwtService } from '../services/jwt.service';
+
+class JoinPayload {
+ currentRoom?: string;
+ newRoom: string;
+}
+
+class PingPayload {
+ currentRoom: string;
+}
+
+@WebSocketGateway()
+@UseGuards(GatewayAuthGuard)
+export class PingGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
+ @WebSocketServer()
+ server: Server;
+
+ constructor(private jwtService: JwtService) {}
+
+ afterInit(server: Server) {
+ console.log('Sockets initialized');
+ }
+
+ handleConnection(client: Socket) {
+ // you can do things like add users to rooms
+ // or emit events here.
+ // IMPORTANT! The GatewayAuthGuard doesn't trigger on these handlers
+ // if you need to do anything in this method you need to authenticate the JWT
+ // manually.
+ try {
+ const jwt = client.handshake.auth.token;
+ const jwtBody = this.jwtService.parseToken(jwt);
+ console.log(client.handshake.query);
+ console.log('Client Connected: ', jwtBody.userId);
+ } catch (e) {
+ throw new WsException('Invalid token');
+ }
+ }
+
+ handleDisconnect(client: Socket) {
+ console.log('Client Disconnected');
+ }
+
+ @SubscribeMessage('ping')
+ public handlePing(
+ @ConnectedSocket() client: Socket,
+ @MessageBody() payload: PingPayload,
+ @GatewayJwtBody() jwtBody: JwtBodyDto,
+ ) {
+ this.server.to(payload.currentRoom).emit('pong', { message: { userId: jwtBody.userId } });
+ console.log(client.rooms);
+ }
+
+ @SubscribeMessage('join-room')
+ public async joinRoom(client: Socket, payload: JoinPayload) {
+ console.log(payload);
+ payload.currentRoom && (await client.leave(payload.currentRoom));
+ await client.join(payload.newRoom);
+ return { msg: 'Joined room', room: payload.newRoom };
+ }
+}
diff --git a/server/providers/guards/gatewayauth.guard.ts b/server/providers/guards/gatewayauth.guard.ts
new file mode 100644
index 0000000..0843752
--- /dev/null
+++ b/server/providers/guards/gatewayauth.guard.ts
@@ -0,0 +1,27 @@
+import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
+import { JwtService } from '../services/jwt.service';
+import { GuardUtil } from '../util/guard.util';
+import { Socket } from 'socket.io';
+import { WsException } from '@nestjs/websockets';
+
+@Injectable()
+export class GatewayAuthGuard implements CanActivate {
+ constructor(private guardUtil: GuardUtil, private jwtService: JwtService) {}
+
+ canActivate(context: ExecutionContext) {
+ // Handlers and Controllers can both skip this guard in the event that
+ if (this.guardUtil.shouldSkip(this, context)) {
+ return true;
+ }
+
+ const req = context.switchToHttp().getRequest() as Socket;
+ const jwt = req.handshake.auth.token;
+ if (!jwt) throw new WsException('Invalid auth token');
+ try {
+ req.handshake.auth.jwtBody = this.jwtService.parseToken(jwt);
+ } catch (e) {
+ throw new WsException('Invalid auth token');
+ }
+ return true;
+ }
+}