Skip to content

Commit

Permalink
Removed token cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
adriiglz committed Mar 8, 2024
1 parent c09c98c commit bec70f1
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 25 deletions.
20 changes: 0 additions & 20 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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;
Expand All @@ -11,24 +10,8 @@ const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002';
const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';
const questionServiceUrl = process.env.QUESTION_SERVICE_URL || 'http://localhost:8010';

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});
Expand All @@ -43,9 +26,6 @@ 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 });
Expand Down
6 changes: 1 addition & 5 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
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';
Expand Down Expand Up @@ -39,9 +36,8 @@ 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 and save it in a cookie
// Generate a JWT token
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 {
Expand Down

0 comments on commit bec70f1

Please sign in to comment.