Skip to content

Commit

Permalink
Merge questions branch with develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
uo288347 committed Mar 7, 2024
2 parents 1e9322e + c90d9a6 commit 57dbd2c
Show file tree
Hide file tree
Showing 40 changed files with 703 additions and 170 deletions.
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
coverage
docs/build

# Dependencies lock files
package-lock.json

# Node.js dependencies
node_modules

# IDE/Editor files
.idea/
.vscode/

# Environment variables
.env
79 changes: 79 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,85 @@ app.get('/questions', async (req, res) => {
}
});

app.post('/user/edit', async (req, res) => {
try {
// Forward the add user request to the user service
const userResponse = await axios.post(userServiceUrl + '/user/edit', req.body);
res.json(userResponse.data);
} catch (error) {
if (error.response && error.response.status) {
res.status(error.response.status).json({ error: error.response.data.error });
} else if (error.message) {
res.status(500).json({ error: error.message });
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});

app.get('/group/list', async (req, res) => {
try {
const userResponse = await axios.get(userServiceUrl + '/group/api/list');
res.json(userResponse.data);
} catch (error) {
if (error.response && error.response.status) {
res.status(error.response.status).json({ error: error.response.data.error });
} else if (error.message) {
res.status(500).json({ error: error.message });
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});


app.post('/group/add', async (req, res) => {
try {
const userResponse = await axios.post(userServiceUrl + '/group/add', req.body);
res.json(userResponse.data);
} catch (error) {
if (error.response && error.response.status) {
res.status(error.response.status).json({ error: error.response.data.error });
} else if (error.message) {
res.status(500).json({ error: error.message });
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});

app.get('/group/:name', async (req, res) => {
try {
const { name } = req.params;
const userResponse = await axios.get(`${userServiceUrl}/group/${name}`);
res.json(userResponse.data);
} catch (error) {
if (error.response && error.response.status) {
res.status(error.response.status).json({ error: error.response.data.error });
} else if (error.message) {
res.status(500).json({ error: error.message });
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});

app.post('/group/:name/join', async (req, res) => {
try {
const { name } = req.params;
const userResponse = await axios.post(`${userServiceUrl}/group/${name}/join`, req.body);
res.json(userResponse.data);
} catch (error) {
if (error.response && error.response.status) {
res.status(error.response.status).json({ error: error.response.data.error });
} else if (error.message) {
res.status(500).json({ error: error.message });
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});

// Start the gateway service
const server = app.listen(port, () => {
console.log(`Gateway Service listening at http://localhost:${port}`);
Expand Down
3 changes: 3 additions & 0 deletions users/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Imports (express syntax)
const express = require('express');


// Routes:
const authRoutes = require('./routes/auth-routes.js');
const userRoutes = require('./routes/user-routes.js');
const groupRoutes = require('./routes/group-routes.js');

// App definition and
const app = express();
Expand All @@ -15,6 +17,7 @@ app.use(express.json());
// Routes middlewares to be used
app.use('/user', userRoutes);
app.use('/login', authRoutes);
app.use('/group', groupRoutes);

// Start the service
const server = app.listen(port, () => {
Expand Down
8 changes: 8 additions & 0 deletions users/models/user-model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Sequelize, DataTypes } = require('sequelize');


// Database connection configuration
const sequelize = new Sequelize({
host: 'mariadb',
Expand All @@ -15,6 +16,7 @@ const User = sequelize.define('User', {
username: {
type: DataTypes.STRING,
primaryKey: true,
notEmpty: true,
},
password: {
type: DataTypes.STRING,
Expand All @@ -23,10 +25,12 @@ const User = sequelize.define('User', {
name: {
type: DataTypes.STRING,
allowNull: false,
notEmpty: true,
},
surname: {
type: DataTypes.STRING,
allowNull: false,
notEmpty: true,
},
createdAt: {
type: DataTypes.DATE,
Expand Down Expand Up @@ -63,10 +67,14 @@ const Group = sequelize.define('Group', {
type: DataTypes.STRING,
primaryKey: true
},
creator: {
type: DataTypes.STRING,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
}
// When the session is introduced, the creator user and more stuff will be added
})

const UserGroup = sequelize.define('UserGroup', {
Expand Down
80 changes: 80 additions & 0 deletions users/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions users/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
"dependencies": {
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"sequelize": "^6.6.5",
"express-session": "^1.18.0",
"jose": "5.2.2",
"jsonwebtoken": "^9.0.2",
"mariadb": "^2.5.1"
"mariadb": "^2.5.1",
"sequelize": "^6.6.5"
},
"devDependencies": {
"jest": "^29.7.0",
"supertest": "^6.3.4"
}
}
}
29 changes: 25 additions & 4 deletions users/routes/auth-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const { User } = require('../models/user-model');


require('dotenv').config();

router.post('/', async (req, res) => {
try {

const { username, password } = req.body;

// Check if required fields are present in the request body
Expand All @@ -20,10 +23,28 @@ router.post('/', async (req, res) => {

// Check if the user exists and verify the password
if (user && user.username === username && await bcrypt.compare(password, user.password)) {
// Generate a JWT token
const token = jwt.sign({ userId: user.id }, 'your-secret-key', { expiresIn: '1h' });

// Token payload
const payload = {
userId: username
};

//CHANGE THIS TO ENVIRONMENT VARS (NOT PUBLIC)
const secretKey = 'eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTcwOTQ2OTkzMywiaWF0IjoxNzA5NDY5OTMzfQ.pQ8H6FKeZyEHPnGs4Ah3-n-QXJ5E8YM_u1AfZHI7Ip0';

const options = {
expiresIn: '1h'
};

//Token sign and creation
const token = jwt.sign(payload, secretKey, options);

//This should save token in user's browser, it doesn't seem to do anything
res.cookie("token", token); // maxAge (millis) = 1 hour

// Respond with the token and user information
return res.json({ token, username, createdAt: user.createdAt });
return res.status(200).json({ token, username, createdAt: user.createdAt });

} else {
return res.status(401).json({ error: 'Invalid credentials' });
}
Expand Down
Loading

0 comments on commit 57dbd2c

Please sign in to comment.