Skip to content

Commit a596acd

Browse files
authored
Merge pull request #42 from CS3219-AY2526Sem1/david/user-middleware
David/user middleware
2 parents 3c3a0aa + 6ab0bb2 commit a596acd

File tree

8 files changed

+2913
-4
lines changed

8 files changed

+2913
-4
lines changed

.gitignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
**/__pycache__/
2-
.vscode/
2+
3+
# Ignore environment variables file
4+
.env
5+
6+
# Ignore service account key file
7+
services/**/serviceAccountKey.json
8+
9+
# Ignore node modules
10+
**/node_modules/
11+
.vscode/

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
### Note:
55
- You are required to develop individual microservices within separate folders within this repository.
66
- The teaching team should be given access to the repositories as we may require viewing the history of the repository in case of any disputes or disagreements.
7+
8+
9+
### for user-service
10+
- go to firebase project settings > service account > generate new private key.
11+
- rename the downloaded json file into serviceAccountKey.json and paste it the same dir as .env.

services/collaboration-service/requirements.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

services/user-service/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copy this file to .env and fill in your own values
2+
GOOGLE_APPLICATION_CREDENTIALS="path/to/your/serviceAccountKey.json"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require("dotenv").config();
2+
const admin = require("firebase-admin");
3+
4+
const serviceAccount = process.env.GOOGLE_APPLICATION_CREDENTIALS;
5+
6+
admin.initializeApp({
7+
credential: admin.credential.cert(serviceAccount),
8+
});
9+
10+
module.exports = { admin };

services/user-service/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const express = require("express");
2+
const cors = require("cors");
3+
const { createProxyMiddleware } = require("http-proxy-middleware");
4+
const { admin } = require("./firebase-admin-config");
5+
6+
const app = express();
7+
app.use(cors());
8+
9+
const MATCHING_SERVICE_URL = process.env.MATCHING_SERVICE_URL || "http://localhost:4001";
10+
const QUESTION_SERVICE_URL = process.env.QUESTION_SERVICE_URL || "http://localhost:4002";
11+
const COLLABORATION_SERVICE_URL = process.env.COLLABORATION_SERVICE_URL || "http://localhost:4003";
12+
13+
const verifyToken = async (req, res, next) => {
14+
const header = req.headers.authorization;
15+
if (!header || !header.startsWith("Bearer ")) {
16+
return res.status(401).json({ message: "Unauthorized" });
17+
}
18+
const token = header.split(" ")[1];
19+
try {
20+
const decodedToken = await admin.auth().verifyIdToken(token);
21+
req.user = decodedToken;
22+
next();
23+
} catch (error) {
24+
return res.status(401).json({ message: "Unauthorized" });
25+
}
26+
};
27+
28+
const onProxyReq = (proxyReq, req, res) => {
29+
if (req.user) {
30+
proxyReq.setHeader("x-user-id", req.user.uid);
31+
proxyReq.setHeader("x-user-email", req.user.email);
32+
}
33+
};
34+
35+
app.use(
36+
"/matching",
37+
verifyToken,
38+
createProxyMiddleware({
39+
target: MATCHING_SERVICE_URL,
40+
changeOrigin: true,
41+
onProxyReq,
42+
})
43+
);
44+
45+
app.use(
46+
"/questions",
47+
verifyToken,
48+
createProxyMiddleware({
49+
target: QUESTION_SERVICE_URL,
50+
changeOrigin: true,
51+
onProxyReq,
52+
})
53+
);
54+
55+
app.use(
56+
"/collaboration",
57+
verifyToken,
58+
createProxyMiddleware({
59+
target: COLLABORATION_SERVICE_URL,
60+
changeOrigin: true,
61+
onProxyReq,
62+
})
63+
);
64+
65+
app.get("/health", (req, res) => {
66+
res.send("User Service is healthy");
67+
});
68+
69+
const PORT = process.env.PORT || 4000;
70+
app.listen(PORT, () => {
71+
console.log(`User Service listening on port ${PORT}`);
72+
});

0 commit comments

Comments
 (0)