diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 88b84c8..b9e6e40 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -2,6 +2,7 @@ const express = require('express'); const axios = require('axios'); const cors = require('cors'); const promBundle = require('express-prom-bundle'); +const cookieParser = require('cookie-parser'); const app = express(); const port = 8000; @@ -9,8 +10,24 @@ const port = 8000; const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002'; const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001'; +function parseCookies(response) { + const cookies = {}; + const cookieHeader = response.headers['set-cookie'] + + if (cookieHeader) { + const cookieStrings = Array.isArray(cookieHeader) ? cookieHeader : [cookieHeader]; + for (const cookieString of cookieStrings) { + const [name, value] = cookieString.split(';')[0].split('='); + cookies[name.trim()] = value.trim(); + } + } + + return cookies; +} + app.use(cors()); app.use(express.json()); +app.use(cookieParser()); //Prometheus configuration const metricsMiddleware = promBundle({includeMethod: true}); @@ -25,6 +42,9 @@ app.post('/login', async (req, res) => { try { // Forward the login request to the authentication service const authResponse = await axios.post(authServiceUrl+'/login', req.body); + const cookies = parseCookies(authResponse) + const token = cookies.token + res.cookie('token', token) res.json(authResponse.data); } catch (error) { res.status(error.response.status).json({ error: error.response.data.error }); diff --git a/gatewayservice/package-lock.json b/gatewayservice/package-lock.json index fc5f2d6..5ef04ef 100644 --- a/gatewayservice/package-lock.json +++ b/gatewayservice/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "axios": "^1.6.5", + "cookie-parser": "^1.4.6", "cors": "^2.8.5", "express": "^4.18.2", "express-prom-bundle": "^7.0.0" @@ -1787,6 +1788,26 @@ "node": ">= 0.6" } }, + "node_modules/cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "dependencies": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/cookie-parser/node_modules/cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", diff --git a/gatewayservice/package.json b/gatewayservice/package.json index f712722..313b29f 100644 --- a/gatewayservice/package.json +++ b/gatewayservice/package.json @@ -19,6 +19,7 @@ "homepage": "https://github.com/arquisoft/wiq_en1a#readme", "dependencies": { "axios": "^1.6.5", + "cookie-parser": "^1.4.6", "cors": "^2.8.5", "express": "^4.18.2", "express-prom-bundle": "^7.0.0" diff --git a/users/authservice/auth-service.js b/users/authservice/auth-service.js index 9764f08..6641650 100644 --- a/users/authservice/auth-service.js +++ b/users/authservice/auth-service.js @@ -2,13 +2,16 @@ const express = require('express'); const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); -const User = require('./auth-model') +const User = require('./auth-model'); +const cookieParser = require('cookie-parser') const app = express(); const port = 8002; // Middleware to parse JSON in request body app.use(express.json()); +// Middleware to do anything related with cookies +app.use(cookieParser()) // Connect to MongoDB const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb'; @@ -36,8 +39,9 @@ app.post('/login', async (req, res) => { // Check if the user exists and verify the password if (user && await bcrypt.compare(password, user.password)) { - // Generate a JWT token + // Generate a JWT token and save it in a cookie const token = jwt.sign({ userId: user._id }, 'your-secret-key', { expiresIn: '1h' }); + res.cookie('token', token); // Respond with the token and user information res.json({ token: token, username: username, createdAt: user.createdAt }); } else { diff --git a/users/authservice/package-lock.json b/users/authservice/package-lock.json index e0ceb0b..978a817 100644 --- a/users/authservice/package-lock.json +++ b/users/authservice/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "bcrypt": "^5.1.1", "body-parser": "^1.20.2", + "cookie-parser": "^1.4.6", "express": "^4.18.2", "jsonwebtoken": "^9.0.2", "mongoose": "^8.0.4" @@ -1898,6 +1899,26 @@ "node": ">= 0.6" } }, + "node_modules/cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "dependencies": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/cookie-parser/node_modules/cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", diff --git a/users/authservice/package.json b/users/authservice/package.json index 23f1c02..e9d7694 100644 --- a/users/authservice/package.json +++ b/users/authservice/package.json @@ -20,6 +20,7 @@ "dependencies": { "bcrypt": "^5.1.1", "body-parser": "^1.20.2", + "cookie-parser": "^1.4.6", "express": "^4.18.2", "jsonwebtoken": "^9.0.2", "mongoose": "^8.0.4" diff --git a/users/userservice/user-model.js b/users/userservice/user-model.js index 71d81b5..328601b 100644 --- a/users/userservice/user-model.js +++ b/users/userservice/user-model.js @@ -13,6 +13,13 @@ const userSchema = new mongoose.Schema({ type: Date, default: Date.now, }, + points: { + type: Number, + default: function() { + // Generate a random integer between 0 and 100 + return Math.floor(Math.random() * 101); + } + } }); const User = mongoose.model('User', userSchema); diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index be95842..bfb581f 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -26,6 +26,35 @@ function validateRequiredFields(req, requiredFields) { } } +// Function to get the user's ranking data +async function getRankingFor(loggedUser) { + const users = await User.find().sort({points: -1}) + const ranking = users.indexOf( (user) => user._id == loggedUser._id) + + return { ranking: ranking, points: loggedUser.points, user: loggedUser.username } +} + +app.get('/rankings', async (req, res) => { + try { + const { token } = req.cookies + const decoded = jwt.verify(token, 'your-secret-key') + const userId = decoded.userId + const loggedUser = await User.findById(userId) + const userRanking = getRankingFor(loggedUser) + const usersRanking = (await User.find().sort({points: -1})).map( (user, index) => { + return { + ranking: index+1, + points: user.points, + user: user.username } + }) + + res.json(userRanking, usersRanking) + + } catch (error) { + res.status(400).json({ error: error.message }); + } +}) + app.post('/adduser', async (req, res) => { try { // Check if required fields are present in the request body