Skip to content

Commit

Permalink
Merge pull request #63 from Arquisoft/Auth
Browse files Browse the repository at this point in the history
Authentication + Docker
  • Loading branch information
ChristianFN2 authored Mar 10, 2024
2 parents aca9f57 + 039b6f3 commit 0fa26df
Show file tree
Hide file tree
Showing 18 changed files with 587 additions and 83 deletions.
25 changes: 21 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
permissions:
contents: read
packages: write
needs: [e2e-tests]
#needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
Expand All @@ -59,13 +59,30 @@ jobs:
registry: ghcr.io
workdir: webapp
buildargs: API_URI
docker-push-questionservice:
name: Push question service Docker Image to GitHub Packages
runs-on: ARM64
permissions:
contents: read
packages: write
# needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: arquisoft/wiq_en1a/usequestionservicerservice
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
workdir: questionservice
docker-push-authservice:
name: Push auth service Docker Image to GitHub Packages
runs-on: ARM64
permissions:
contents: read
packages: write
needs: [e2e-tests]
#needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
Expand All @@ -82,7 +99,7 @@ jobs:
permissions:
contents: read
packages: write
needs: [e2e-tests]
#needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
Expand All @@ -99,7 +116,7 @@ jobs:
permissions:
contents: read
packages: write
needs: [e2e-tests]
#needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
Expand Down
16 changes: 15 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ services:
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb
# platform: linux/arm64

questionservice:
container_name: questionservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_en1a/questionservice:latest
profiles: ["dev", "prod"]
build: ./questionservice
depends_on:
- mongodb
ports:
- "8010:8010"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb
# platform: linux/arm64
gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_en1a/gatewayservice:latest
Expand All @@ -58,6 +71,7 @@ services:
environment:
AUTH_SERVICE_URL: http://authservice:8002
USER_SERVICE_URL: http://userservice:8001
QUESTION_SERVICE_URL: http://questionservice:8010
# platform: linux/arm64

webapp:
Expand Down
2 changes: 2 additions & 0 deletions questionservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
20 changes: 20 additions & 0 deletions questionservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/questionservice

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the app source code to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 8010

# Define the command to run your app
CMD ["node", "question-service.js"]
1 change: 1 addition & 0 deletions users/authservice/auth-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
username: String,
email: String,
password: String,
createdAt: Date,
});
Expand Down
2 changes: 1 addition & 1 deletion users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ app.post('/login', async (req, res) => {
// Generate a JWT token
const token = jwt.sign({ userId: user._id }, 'your-secret-key', { expiresIn: '1h' });
// Respond with the token and user information
res.status(200).json({ token: token });
res.status(200).json({ token: token, username:user.username, email: user.email, createdAt: user.createdAt});
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
Expand Down
5 changes: 5 additions & 0 deletions users/userservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
Expand Down
23 changes: 21 additions & 2 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,35 @@ app.get('/rankings', async (req, res) => {
}
})


app.post("/addpoints", async (req, res) => {
try {
validateRequiredFields(req, ['username']);
const user = await User.findOne({
username: req.body.username
});
if (!user) {
throw new Error('User not found');
}
user.points += 1;
await user.save();
res.status(200).json(user);
} 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
validateRequiredFields(req, ['username', 'password']);
validateRequiredFields(req, ['username','email', 'password']);

// Encrypt the password before saving it
const hashedPassword = await bcrypt.hash(req.body.password, 10);

const newUser = new User({
username: req.body.username,
email: req.body.email,
password: hashedPassword,
});

Expand Down
Loading

0 comments on commit 0fa26df

Please sign in to comment.