Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/17arindam/PlayCafe into goo…
Browse files Browse the repository at this point in the history
…gle_translate
  • Loading branch information
17arindam committed Oct 10, 2024
2 parents 8f10d6d + a796767 commit d383829
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 44 deletions.
39 changes: 39 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

FROM node:18 AS frontend-build

WORKDIR /app/frontend
COPY frontend/package*.json ./

RUN npm install

COPY frontend/ ./

FROM node:18 AS backend-build

WORKDIR /app/backend
COPY backend/package*.json ./

RUN npm install

COPY backend/ ./

FROM node:18

WORKDIR /app

COPY --from=backend-build /app/backend ./backend
COPY --from=frontend-build /app/frontend ./frontend

COPY frontend/package*.json ./frontend/
COPY backend/package*.json ./backend/
RUN npm install --prefix frontend && npm install --prefix backend


COPY start.sh ./

RUN chmod +x start.sh

EXPOSE 5173 3000


CMD ["sh", "start.sh"]
43 changes: 37 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,22 @@ Make sure you follow our contributing guidlines:- [here](https://github.com/Ram
npm run dev
4. Open your browser at `http://localhost:3000` to see the project running! 🌟

Set-up using Dockerfile:-

1. **Build Docker Image**:
```bash
docker build -t playcafe .
2. **Run Docker Image**
```bash
docker run -p 5173:5173 -p 3000:3000 playcafe
3. Open your browser at `http://localhost:5173` to see the project running! 🌟

Set-up using docker-compose :-

1. **Build Docker Image and Run the Application**:
```bash
docker compose up --build
## 🤝 Contributing
We love contributions! 💙 Whether you're a participant in **GSSoC** or an open-source enthusiast, we welcome your input. Here's how you can contribute:
Expand Down Expand Up @@ -118,22 +133,29 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
<sub><b>Tejas Benibagde</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/haseebzaki-07">
<img src="https://avatars.githubusercontent.com/u/147314463?v=4" width="100;" alt="haseebzaki-07"/>
<br />
<sub><b>Haseeb Zaki</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Suhas-Koheda">
<img src="https://avatars.githubusercontent.com/u/72063139?v=4" width="100;" alt="Suhas-Koheda"/>
<br />
<sub><b>Suhas Koheda</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/sajalbatra">
<img src="https://avatars.githubusercontent.com/u/125984550?v=4" width="100;" alt="sajalbatra"/>
<br />
<sub><b>Sajal Batra</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/vishnuprasad2004">
<img src="https://avatars.githubusercontent.com/u/116942066?v=4" width="100;" alt="vishnuprasad2004"/>
Expand Down Expand Up @@ -169,15 +191,15 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
<sub><b>Tanishi Rai</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Shiva-Bajpai">
<img src="https://avatars.githubusercontent.com/u/141490705?v=4" width="100;" alt="Shiva-Bajpai"/>
<br />
<sub><b>Shiva Bajpai</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Sawan-Kushwah">
<img src="https://avatars.githubusercontent.com/u/138680328?v=4" width="100;" alt="Sawan-Kushwah"/>
Expand Down Expand Up @@ -213,15 +235,15 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
<sub><b>Jai Dhingra</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Jay-1409">
<img src="https://avatars.githubusercontent.com/u/166749819?v=4" width="100;" alt="Jay-1409"/>
<br />
<sub><b>Jay shah</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Mohitranag18">
<img src="https://avatars.githubusercontent.com/u/152625405?v=4" width="100;" alt="Mohitranag18"/>
Expand All @@ -236,6 +258,13 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
<sub><b>Bashua Mutiat</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/NilanchalaPanda">
<img src="https://avatars.githubusercontent.com/u/110488337?v=4" width="100;" alt="NilanchalaPanda"/>
<br />
<sub><b>Nilanchal</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Sapna127">
<img src="https://avatars.githubusercontent.com/u/91309280?v=4" width="100;" alt="Sapna127"/>
Expand All @@ -250,6 +279,8 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
<sub><b>Stuti </b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Syed-Farazuddin">
<img src="https://avatars.githubusercontent.com/u/119295880?v=4" width="100;" alt="Syed-Farazuddin"/>
Expand Down
5 changes: 5 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
npm-debug.log
.git
.gitignore
README.md
3 changes: 2 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MONGO_URI=enter_your_mongo_uri
EMAIL_USER=your_gmail
EMAIL_PASS=your_16_digit_pass
EMAIL_PASS=your_16_digit_pass
JWT_SECRET=secret
11 changes: 11 additions & 0 deletions backend/config/secret.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const JWT_SECRET = process.env.JWT_SECRET;
const MONGO_URI = process.env.MONGO_URI;
const PORT = process.env.PORT;
const CORS_ORIGIN = process.env.CORS_ORIGIN;

module.exports = {
JWT_SECRET,
MONGO_URI,
PORT,
CORS_ORIGIN,
};
30 changes: 26 additions & 4 deletions backend/controller/admin.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const bcrypt = require("bcrypt");
const { z } = require("zod");
const Admin = require("../models/admin.model");
const logger = require("../config/logger");
const jwt = require("jsonwebtoken");

// Define the schema
const adminSchema = z.object({
Expand All @@ -18,8 +19,10 @@ async function createAdmin(req, res) {
return res.status(400).json({ error: validation.error.errors });
}
const existingAdmin = await Admin.findOne({ email: req.body.email });
const existingAdmin = await Admin.findOne({ email: req.body.email });
if (existingAdmin) {
return res.status(409).json({ error: "Email is already registered" });
return res.status(409).json({ error: "Email is already registered" });
}

try {
Expand All @@ -34,7 +37,6 @@ async function createAdmin(req, res) {
} catch (error) {
logger.error("Error creating admin:", {
message: error.message,
stack: error.stack,
});
res.status(500).json({ error: "Internal server error" });
}
Expand All @@ -50,6 +52,15 @@ async function loginAdmin(req, res) {
if (!validation.success) {
return res.status(400).json({ error: validation.error.errors });
}
const adminLoginSchema = z.object({
email: z.string().email("Invalid email address"),
password: z.string().min(6, "Password must be at least 6 characters long"),
});
// Validate the request body
const validation = adminLoginSchema.safeParse(req.body);
if (!validation.success) {
return res.status(400).json({ error: validation.error.errors });
}

try {
const admin = await Admin.findOne({ email: req.body.email });
Expand All @@ -58,16 +69,27 @@ async function loginAdmin(req, res) {
}
const validPassword = await bcrypt.compare(
req.body.password,
admin.password,
admin.password
);
if (!validPassword) {
return res.status(401).json({ error: "Invalid email or password" });
}
res.json({ message: "Login successful" });
const token = jwt.sign(
{ id: admin._id, role: "admin" },
process.env.JWT_SECRET,
{
expiresIn: "1h",
}
);
res.json({
message: "Login successful",
token,
role: "admin",
admin: { id: admin._id, name: admin.name, email: admin.email },
});
} catch (error) {
logger.error("Error logging in admin:", {
message: error.message,
stack: error.stack,
});
res.status(500).json({ error: "Internal server error" });
}
Expand Down
17 changes: 16 additions & 1 deletion backend/controller/customer.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const bcrypt = require("bcrypt");
const { z } = require("zod");
const Customer = require("../models/customer.model");
const jwt = require("jsonwebtoken");

// Define the schema
const customerSchema = z.object({
Expand Down Expand Up @@ -59,7 +60,21 @@ async function loginCustomer(req, res) {
if (!validPassword) {
return res.status(401).json({ error: "Invalid email or password" });
}
res.json({ message: "Login successful" });
const token = jwt.sign(
{ id: customer._id },
process.env.JWT_SECRET,
{ expiresIn: "1h" } // Expires in 1 hour
);
res.json({
message: "Login successful",
token,
role: "customer",
user: {
id: customer._id,
name: customer.name,
email: customer.email,
},
});
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
Expand Down
25 changes: 25 additions & 0 deletions backend/middlewares/authAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const jwt = require("jsonwebtoken");
const logger = require("../config/logger");

const authenticateAdmin = (req, res, next) => {
const token = req.header("Authorization")?.split(" ")[1]; // Expecting "Bearer <token>"

if (token) {
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
return res.sendStatus(403); // Forbidden
}
if (decoded.role !== "admin") {
return res.sendStatus(403); // Forbidden
}

req.user = decoded;
logger.info(`Admin authenticated: ${JSON.stringify(decoded.id)}`);
next();
});
} else {
res.sendStatus(401); // Unauthorized
}
};

module.exports = authenticateAdmin;
25 changes: 25 additions & 0 deletions backend/middlewares/authCustomer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const jwt = require("jsonwebtoken");
const logger = require("../config/logger");
const config = require("../config/secret");

const authenticateCustomer = (req, res, next) => {
const token = req.header("Authorization")?.split(" ")[1]; // Expecting "Bearer <token>"

if (token) {
jwt.verify(token, config.JWT_SECRET, (err, user) => {
if (err) {
if (err.name === "TokenExpiredError") {
return res.status(401).json({ message: "Token expired" });
}
return res.status(403).json({ message: "Invalid token" });
}
req.user = user;
logger.info(`Customer authenticated: ${JSON.stringify(user.username)}`);
next();
});
} else {
res.sendStatus(401); // Unauthorized
}
};

module.exports = authenticateCustomer;
3 changes: 2 additions & 1 deletion backend/routes/adminRouter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const express = require("express");
const { createAdmin, loginAdmin } = require("../controller/admin.controller");
const authenticateAdmin = require("../middlewares/authAdmin");
const router = express.Router();
require("dotenv").config();

router.get("/", (req, res) => {
router.get("/", authenticateAdmin, (req, res) => {
res.json({
message: "Welcome to the Admin API!",
version: "1.0.0",
Expand Down
3 changes: 2 additions & 1 deletion backend/routes/customerRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const {
createCustomer,
resetPassword,
} = require("../controller/customer.controller");
const authenticateCustomer = require("../middlewares/authCustomer");
const router = express.Router();
require("dotenv").config();

router.get("/", (req, res) => {
router.get("/", authenticateCustomer, (req, res) => {
res.json({
message: "Welcome to the User API!",
version: "1.0.0",
Expand Down
Loading

0 comments on commit d383829

Please sign in to comment.