-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Socket Authentication & Authorization
- Loading branch information
1 parent
0de312f
commit 1bc8a2c
Showing
20 changed files
with
83 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import { config } from 'dotenv'; | ||
import * as process from 'node:process'; | ||
import { ServerOptions } from 'socket.io'; | ||
|
||
config(); | ||
|
||
const SERVER_PORT = parseInt(process.env.PORT || '8080'); | ||
const CLIENT_PORT = parseInt(process.env.CLIENT_PORT || '5173'); | ||
const ORIGIN = ['http://localhost:5173']; | ||
const PORT = parseInt(process.env.PORT || '8080'); | ||
const ORIGIN = 'http://localhost:5173'; | ||
|
||
const SERVER_OPTIONS = { | ||
const SERVER_OPTIONS: Partial<ServerOptions> = { | ||
cors: { | ||
origin: ORIGIN, | ||
credentials: true, // allow credentials (cookies, authorization headers, etc.) | ||
allowedMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], | ||
credentials: true, // allow credentials (cookies) | ||
allowedHeaders: ['Authorization', 'Content-Type'], | ||
}, | ||
connectionStateRecovery: {}, | ||
connectionStateRecovery: {}, // enable connection state recovery | ||
}; | ||
|
||
export default { | ||
SERVER_PORT, | ||
CLIENT_PORT, | ||
PORT, | ||
ORIGIN, | ||
SERVER_OPTIONS, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { SocketHandler } from '@controllers/ws/types'; | ||
import { DocumentsService } from '@services/DocumentsService'; | ||
import onOperation from '@controllers/ws/events/document/onOperation'; | ||
import onCursorChange from '@controllers/ws/events/document/onCursorChange'; | ||
import onJoinDocument from '@controllers/ws/events/document/onJoinDocument'; | ||
import onLeaveDocument from '@controllers/ws/events/document/onLeaveDocument'; | ||
import onJoinWorkspace from '@controllers/ws/events/workspace/onJoinWorkspace'; | ||
import onLeaveWorkspace from '@controllers/ws/events/workspace/onLeaveWorkspace'; | ||
import { Services } from '@services/Services'; | ||
|
||
export default function events(service: DocumentsService): Record<string, SocketHandler> { | ||
if (!service) throw new Error('Service parameter is required'); | ||
export default function events(services: Services): Record<string, SocketHandler> { | ||
if (!services) throw new Error('Services parameter is required'); | ||
|
||
return { | ||
// document events | ||
operations: onOperation(service), | ||
operations: onOperation(services.documents), | ||
cursorChange: onCursorChange(), | ||
joinDocument: onJoinDocument(), | ||
leaveDocument: onLeaveDocument(), | ||
|
||
// workspace events | ||
joinWorkspace: onJoinWorkspace(), | ||
joinWorkspace: onJoinWorkspace(services.workspaces), | ||
leaveWorkspace: onLeaveWorkspace(), | ||
}; | ||
} |
2 changes: 2 additions & 0 deletions
2
code/server/src/controllers/ws/events/document/onJoinDocument.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 14 additions & 4 deletions
18
code/server/src/controllers/ws/events/workspace/onJoinWorkspace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { LoggedUser } from '@notespace/shared/src/users/types'; | ||
import { Socket } from 'socket.io'; | ||
|
||
export function getUserFromSocket(socket: Socket) { | ||
return (socket.request as any).user as LoggedUser | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.