Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User management rita #87

Merged
merged 8 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm --prefix users/authservice ci
- run: npm --prefix users/userservice ci

- run: npm --prefix gatewayservice ci
- run: npm --prefix webapp ci
- run: npm --prefix users/authservice test -- --coverage
- run: npm --prefix users/userservice test -- --coverage

- run: npm --prefix gatewayservice test -- --coverage
- run: npm --prefix webapp test -- --coverage
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
File renamed without changes.
4 changes: 2 additions & 2 deletions users/authservice/Dockerfile → users/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/authservice
WORKDIR /usr/src/users

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
Expand All @@ -17,4 +17,4 @@ COPY . .
EXPOSE 8002

# Define the command to run your app
CMD ["node", "auth-service.js"]
CMD ["node", "index.js"]
2 changes: 1 addition & 1 deletion users/authservice/auth-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const userSchema = new mongoose.Schema({
createdAt: Date,
});

const User = mongoose.model('User', userSchema);
const User = mongoose.model('authUser', userSchema);

module.exports = User
39 changes: 6 additions & 33 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
const express = require('express');
const mongoose = require('mongoose');
const router = express.Router();
const bcrypt = require('bcrypt');
const authUser = require('./auth-model')
const jwt = require('jsonwebtoken');
const User = require('./auth-model')
const cors = require('cors');

const app = express();
const port = 8002;


app.use(cors());
// Middleware to parse JSON in request body
app.use(express.json());

// Connect to MongoDB
// Connect to MongoDB - testing
const mongoUri = 'mongodb+srv://prueba:prueba@cluster0.kjzbhst.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0';


// Connect to the database
mongoose.connect(mongoUri).then(
console.log('Succesfully connected to MongoDB')
);

// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
Expand All @@ -31,9 +14,9 @@ function validateRequiredFields(req, requiredFields) {
}
}
}

// Route for user login
app.post('/login', async (req, res) => {
router.post('/login', async (req, res) => {
try {

// Check if required fields are present in the request body
Expand Down Expand Up @@ -63,7 +46,7 @@ app.post('/login', async (req, res) => {
if (user && await bcrypt.compare(password, user.password)) {
// Generate a JWT token
const token = jwt.sign({ username: user.username, userEmail: user.email, questions_answered: user.questions_answered, correctly_answered_questions: user.correctly_answered_questions }, 'your-secret-key', { expiresIn: '1h' });
// Respond with the token and user information
// Respond with the token that has all the user information
res.json({ token: token });
} else {
res.status(401).json({ error: 'Invalid credentials' });
Expand All @@ -73,14 +56,4 @@ app.post('/login', async (req, res) => {
}
});

// Start the server
const server = app.listen(port, () => {
console.log(`Auth Service listening at http://localhost:${port}`);
});

server.on('close', () => {
// Close the Mongoose connection
mongoose.connection.close();
});

module.exports = server
module.exports = router
Loading