diff --git a/packages/api/.env.development b/packages/api/.env.development deleted file mode 100644 index 95d58c9..0000000 --- a/packages/api/.env.development +++ /dev/null @@ -1,3 +0,0 @@ -MONGO_CONNECTION_STRING=mongodb+srv://default:M9iZXokJlZpN4KLX@cluster0.mkuqa.mongodb.net/default?retryWrites=true&w=majority - -JWT_TOKEN_SECRET=afajdslfwk1@lkkasdfl21ASDF!2 \ No newline at end of file diff --git a/packages/api/index.ts b/packages/api/index.ts index e42bb34..22e6615 100644 --- a/packages/api/index.ts +++ b/packages/api/index.ts @@ -4,6 +4,7 @@ import * as Tracing from '@sentry/tracing'; /* eslint-disable @typescript-eslint/no-var-requires */ import express, { Application, NextFunction, Request, Response } from 'express'; +import InitializeWs from './src/socket'; import cors from 'cors'; import morgan from 'morgan'; @@ -31,9 +32,11 @@ const server = app.listen(PORT, () => // won't catch any errors here and we don't want it to console.log(); -require('socket.io')(server, { +const io = require('socket.io')(server, { // todo: add authentication cors: { origin: '*', }, }); + +InitializeWs(io); diff --git a/packages/api/src/socket.ts b/packages/api/src/socket.ts index e69de29..a5d8f49 100644 --- a/packages/api/src/socket.ts +++ b/packages/api/src/socket.ts @@ -0,0 +1,26 @@ +export default function InitializeWs(io: any) { + console.log('initializing web sockets'); + + return io + .use(function (socket: any, next: any) { + console.log('authenticating user...'); + + try { + // const { token } = socket.handshake.query; + // console.log(token); + + // verify jwt token with our api secret + // var decoded: JwtClaims = jwt.verify(token, config.JWT_TOKEN_SECRET); + + // socket.userInfo = decoded; + + next(); + } catch (error) { + console.error(error); + next(new Error('WS Authentication Error')); + } + }) + .on('connection', (socket: any) => { + console.log(`connected`, socket.id); + }); +}