Skip to content

Commit ad3c6c4

Browse files
committed
Verify user's auth status
1 parent 297bbd8 commit ad3c6c4

File tree

13 files changed

+137
-2
lines changed

13 files changed

+137
-2
lines changed

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@
3636

3737
![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

4141
![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

4545
![image4.png](./docs/images/postman-setup4.png)
4646

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)
50+
4751
## Events Available
4852

4953
| Event Name | Description | Parameters | Response Event |
9.76 KB
Loading
-4.64 KB
Loading
60.6 KB
Loading

backend/communication-service/package-lock.json

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/communication-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"license": "ISC",
1515
"description": "",
1616
"dependencies": {
17+
"axios": "^1.7.7",
1718
"cors": "^2.8.5",
1819
"dotenv": "^16.4.5",
1920
"express": "^4.21.1",

backend/communication-service/src/handlers/websocketHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const handleWebsocketCommunicationEvents = (socket: Socket) => {
99
CommunicationEvents.JOIN,
1010
async ({ roomId, username }: { roomId: string; username: string }) => {
1111
connectUser(username);
12+
console.log(username, roomId);
1213
const room = io.sockets.adapter.rooms.get(roomId);
1314
if (room?.has(socket.id)) {
1415
socket.emit(CommunicationEvents.ALREADY_JOINED);

backend/communication-service/src/server.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@ import app, { allowedOrigins } from "./app";
22
import { createServer } from "http";
33
import { Server } from "socket.io";
44
import { handleWebsocketCommunicationEvents } from "./handlers/websocketHandler";
5+
import { verifyToken } from "./utils/userServiceApi";
56

67
const PORT = process.env.SERVICE_PORT || 3005;
78

89
const server = createServer(app);
910

1011
export const io = new Server(server, {
11-
cors: { origin: allowedOrigins, methods: ["GET", "POST"] },
12+
cors: { origin: allowedOrigins, methods: ["GET", "POST"], credentials: true },
1213
connectionStateRecovery: {},
1314
});
1415

16+
io.use((socket, next) => {
17+
const token =
18+
socket.handshake.headers.authorization || socket.handshake.auth.token;
19+
verifyToken(token)
20+
.then(() => {
21+
console.log("Valid credentials");
22+
next();
23+
})
24+
.catch((err) => {
25+
console.error(err);
26+
next(new Error("Unauthorized"));
27+
});
28+
});
29+
1530
io.on("connection", handleWebsocketCommunicationEvents);
1631

1732
server.listen(PORT, () => {
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+
};

0 commit comments

Comments
 (0)