Skip to content

Commit 640e575

Browse files
authored
Merge pull request #110 from nicolelim02/feat/communication
Verify user auth status
2 parents 8580bea + 70c3c6d commit 640e575

33 files changed

+303
-53
lines changed

backend/collab-service/.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ SERVICE_PORT=3003
44
# Origins for cors
55
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
66

7+
# Other service APIs
8+
USER_SERVICE_URL=http://user-service:3001/api
9+
710
# Redis configuration
811
REDIS_URI=redis://collab-service-redis:6379
912

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import axios from "axios";
2+
3+
const USER_SERVICE_URL =
4+
process.env.USER_SERVICE_URL || "http://localhost:3001/api";
5+
6+
const userClient = axios.create({
7+
baseURL: USER_SERVICE_URL,
8+
withCredentials: true,
9+
});
10+
11+
export const verifyToken = (token: string | undefined) => {
12+
return userClient.get("/auth/verify-token", {
13+
headers: { authorization: token },
14+
});
15+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ExtendedError, Socket } from "socket.io";
2+
import { verifyToken } from "../api/userService.ts";
3+
4+
export const verifyUserToken = (
5+
socket: Socket,
6+
next: (err?: ExtendedError) => void
7+
) => {
8+
const token =
9+
socket.handshake.headers.authorization || socket.handshake.auth.token;
10+
verifyToken(token)
11+
.then(() => {
12+
console.log("Valid credentials");
13+
next();
14+
})
15+
.catch((err) => {
16+
console.error(err);
17+
next(new Error("Unauthorized"));
18+
});
19+
};

backend/collab-service/src/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import app, { allowedOrigins } from "./app.ts";
33
import { handleWebsocketCollabEvents } from "./handlers/websocketHandler.ts";
44
import { Server, Socket } from "socket.io";
55
import { connectRedis } from "./config/redis.ts";
6+
import { verifyUserToken } from "./middlewares/basicAccessControl.ts";
67

78
const server = http.createServer(app);
9+
810
export const io = new Server(server, {
911
cors: {
1012
origin: allowedOrigins,
@@ -13,6 +15,8 @@ export const io = new Server(server, {
1315
connectionStateRecovery: {},
1416
});
1517

18+
io.use(verifyUserToken);
19+
1620
io.on("connection", (socket: Socket) => {
1721
handleWebsocketCollabEvents(socket);
1822
});

backend/communication-service/.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ SERVER_PORT=3005
33

44
# Origins for cors
55
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
6+
7+
# Other service APIs
8+
USER_SERVICE_URL=http://user-service:3001/api

backend/communication-service/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,29 @@
3030

3131
- Select the `Socket.IO` option and set URL to `http://localhost:3005`. Click `Connect`.
3232

33-
![image1.png](./docs/image1.png)
33+
![image1.png](./docs/images/postman-setup1.png)
3434

3535
- Add the following events in the `Events` tab and listen to them.
3636

37-
![image2.png](./docs/image2.png)
37+
![image2.png](./docs/images/postman-setup2.png)
3838

39-
- To send a message, go to the `Message` tab and ensure that your message is being parsed as `JSON`.
39+
- Add a valid JWT token in the `Authorization` header.
4040

41-
![image3.png](./docs/image3.png)
41+
![image3.png](./docs/images/postman-setup3.png)
4242

4343
- In the `Event name` input, input the correct event name. Click on `Send` to send a message.
4444

45-
![image4.png](./docs/image4.png)
45+
![image4.png](./docs/images/postman-setup4.png)
46+
47+
- To send a message, go to the `Message` tab and ensure that your message is being parsed as `JSON`.
48+
49+
![image5.png](./docs/images/postman-setup5.png)
4650

4751
## Events Available
4852

4953
| Event Name | Description | Parameters | Response Event |
5054
| --------------------- | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
5155
| **join** | Joins a communication rooms | `roomId` (string): ID of the room. <br> `username` (string): Username of the user that joined. | **user_joined**: Notify the other user that a new user has joined the room. |
5256
| **send_text_message** | Sends a message to the other user | `roomId` (string): ID of the room. <br> `message` (string): Message to send. <br> `username` (string): User that sent the message. <br> `createdTime` (number): Time that the user sent the message. | **text_message_received**: Notify the user that a message is sent |
53-
| **leave** | Leaves the communication room. | `roomId` (string): ID of the room to leave. <br> `username` (string): User that wants to leave. | **user_left**: To notify the user when one user leaves. |
57+
| **user_disconnect** | User disconnection. | None | **user_left**: To notify the user when one user leaves. |
5458
| **disconnect** | Disconnects from the server. | None | **disconnected**: To notify the user when one user gets disconnected from the server. |
-60.3 KB
Binary file not shown.
File renamed without changes.
70 KB
Loading
55.9 KB
Loading

0 commit comments

Comments
 (0)