From ad37c224e769f9e4ede407a0ed9199d5330b0d2c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:36:08 +0000 Subject: [PATCH 1/6] docs(contributor): contrib-readme-action has updated readme --- README.md | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7d21c673..fb3af533 100644 --- a/README.md +++ b/README.md @@ -135,10 +135,17 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 - - AbhijitMotekar99 + + vishnuprasad2004
- Abhijit Motekar + Vishnu Prasad Korada +
+ + + + Navneetdadhich +
+ Navneet Dadhich
@@ -149,10 +156,10 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 - - Navneetdadhich + + AbhijitMotekar99
- Navneet Dadhich + Abhijit Motekar
@@ -168,6 +175,15 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
Dev Mishra + + + + + + jaidh01 +
+ Jai Dhingra +
@@ -176,8 +192,6 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Bashua Mutiat - - Sapna127 @@ -198,13 +212,6 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
Vaibhav-Kumar-K-R
- - - - vishnuprasad2004 -
- Vishnu Prasad Korada -
From 4df28ddc9af6ede7a3fcbb5f86d3677663dc0d5e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 02:13:50 +0000 Subject: [PATCH 2/6] docs(contributor): contrib-readme-action has updated readme --- README.md | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5624a029..c750ea37 100644 --- a/README.md +++ b/README.md @@ -126,19 +126,19 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 - - vishnuprasad2004 + + sajalbatra
- Vishnu Prasad Korada + Sajal Batra
- - sajalbatra + + vishnuprasad2004
- Sajal Batra + Vishnu Prasad Korada
@@ -213,6 +213,15 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Shiva Bajpai + + + stutxi +
+ Stuti +
+ + + Syed-Farazuddin @@ -220,8 +229,6 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Syed Faraz - - Vaibhav-Kumar-K-R From ffa8695cadcdb893ba7bbf6b63407b6099e8ae82 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 11:08:15 +0000 Subject: [PATCH 3/6] docs(contributor): contrib-readme-action has updated readme --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c750ea37..1b73c04a 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,13 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Navneet Dadhich + + + tanishirai +
+ Tanishi Rai +
+ Ayush215mb @@ -169,6 +176,8 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Ayush Yadav + + mishradev1 @@ -176,8 +185,6 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Dev Mishra - - jaidh01 @@ -213,6 +220,8 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Shiva Bajpai + + stutxi @@ -220,8 +229,6 @@ Special thanks to our amazing mentors who are guiding this project! 🙌 Stuti - - Syed-Farazuddin From 3db113bf3087f3431b129766152f2e71227da7e9 Mon Sep 17 00:00:00 2001 From: Samarth Vaidya Date: Tue, 8 Oct 2024 18:45:46 +0530 Subject: [PATCH 4/6] Added login/ sign up user model admin model --- backend/controller/admin.controller.js | 65 ++++++++++++++++++++++++ backend/controller/user.controller.js | 68 ++++++++++++++++++++++++++ backend/models/admin.model.js | 19 +++++++ backend/models/user.model.js | 20 ++++++++ backend/package.json | 1 + backend/routes/adminRouter.js | 20 ++++++++ backend/routes/index.js | 12 ++++- backend/routes/userRouter.js | 22 +++++++++ 8 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 backend/controller/admin.controller.js create mode 100644 backend/controller/user.controller.js create mode 100644 backend/models/admin.model.js create mode 100644 backend/models/user.model.js create mode 100644 backend/routes/adminRouter.js create mode 100644 backend/routes/userRouter.js diff --git a/backend/controller/admin.controller.js b/backend/controller/admin.controller.js new file mode 100644 index 00000000..34a61c4b --- /dev/null +++ b/backend/controller/admin.controller.js @@ -0,0 +1,65 @@ +const bcrypt = require("bcrypt"); +const { z } = require("zod"); +const Admin = require("../models/admin.model"); + +// Define the schema +const adminSchema = z.object({ + name: z.string().min(1, "Name is required"), + email: z.string().email("Invalid email address"), + password: z.string().min(6, "Password must be at least 6 characters long"), +}); + +async function createAdmin(req, res) { + // Validate the request body + const validation = adminSchema.safeParse(req.body); + + if (!validation.success) { + return res.status(400).json({ error: validation.error.errors }); + } + + try { + const hashedPassword = await bcrypt.hash(req.body.password, 10); + const admin = new Admin({ + name: req.body.name, + email: req.body.email, + password: hashedPassword, + }); + await admin.save(); + res.status(201).json({ message: "Admin created successfully" }); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +} + +async function loginAdmin(req, res) { + + const adminSchema = 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 = adminSchema.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 }); + if (!admin) { + return res.status(404).json({ error: "Admin not found" }); + } + const validPassword = await bcrypt.compare(req.body.password, admin.password); + if (!validPassword) { + return res.status(401).json({ error: "Invalid password" }); + } + res.json({ message: "Login successful" }); + } + catch (error) { + res.status(500).json({ error: "Internal server error" }); + } + } + + +module.exports = { createAdmin + , loginAdmin + }; diff --git a/backend/controller/user.controller.js b/backend/controller/user.controller.js new file mode 100644 index 00000000..ff3b7333 --- /dev/null +++ b/backend/controller/user.controller.js @@ -0,0 +1,68 @@ +const bcrypt = require("bcrypt"); +const { z } = require("zod"); +const User = require("../models/user.model"); + +// Define the schema +const userSchema = z.object({ + name: z.string().min(1, "Name is required"), + email: z.string().email("Invalid email address"), + password: z.string().min(6, "Password must be at least 6 characters long"), +}); + +async function createUser(req, res) { + // Validate the request body + const validation = userSchema.safeParse(req.body); + + if (!validation.success) { + return res.status(400).json({ error: validation.error.errors }); + } + + try { + const hashedPassword = await bcrypt.hash(req.body.password, 10); + const user = new User({ + name: req.body.name, + email: req.body.email, + password: hashedPassword, + }); + await user.save(); + res.status(201).json({ message: "User created successfully" }); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +} + +async function loginUser(req, res) { + const userSchema = 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 = userSchema.safeParse(req.body); + if (!validation.success) { + return res.status(400).json({ error: validation.error.errors }); + } + + try { + const user = await User.findOne({ email: req.body.email }); + if (!user) { + return res.status(404).json({ error: "User not found" }); + } + const validPassword = await bcrypt.compare( + req.body.password, + user.password + ); + if (!validPassword) { + return res.status(401).json({ error: "Invalid password" }); + } + res.json({ message: "Login successful" }); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +} + + + +module.exports = { + createUser, + loginUser + }; \ No newline at end of file diff --git a/backend/models/admin.model.js b/backend/models/admin.model.js new file mode 100644 index 00000000..3aa60b37 --- /dev/null +++ b/backend/models/admin.model.js @@ -0,0 +1,19 @@ + +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +const adminSchema = new Schema({ + name: String, + password: String, + email: String, + role: { + type: String, + default: "admin" + }, + bio: String, + profilePicture: String, +}); + +const Admin = mongoose.model("User", adminSchema); + +module.exports = Admin; \ No newline at end of file diff --git a/backend/models/user.model.js b/backend/models/user.model.js new file mode 100644 index 00000000..8fed01e2 --- /dev/null +++ b/backend/models/user.model.js @@ -0,0 +1,20 @@ +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; + + + +const userSchema = new Schema({ + name: String, + password: String, + email: String, + role: { + type: String, + default: "user" + }, + bio: String, + profilePicture: String, +}); + +const User = mongoose.models.User || mongoose.model('User', userSchema); + +module.exports = User; diff --git a/backend/package.json b/backend/package.json index cf18051e..adb636c8 100644 --- a/backend/package.json +++ b/backend/package.json @@ -12,6 +12,7 @@ "license": "ISC", "description": "", "dependencies": { + "bcrypt": "^5.1.1", "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.0", diff --git a/backend/routes/adminRouter.js b/backend/routes/adminRouter.js new file mode 100644 index 00000000..372ff3c3 --- /dev/null +++ b/backend/routes/adminRouter.js @@ -0,0 +1,20 @@ +const express = require("express"); +const { createAdmin, loginAdmin } = require("../controller/admin.controller"); +const router = express.Router(); +require("dotenv").config(); + + +router.get("/", (req, res) => { + res.json({ + message: "Welcome to the Admin API!", + version: "1.0.0", + endpoints: { + login: "/login", + register: "/register", + }, + documentation: "https://api-docs-url.com",}); + }); +router.post("/register", createAdmin); +router.post("/login", loginAdmin); + +module.exports = router; \ No newline at end of file diff --git a/backend/routes/index.js b/backend/routes/index.js index bbe44865..a82c30e5 100644 --- a/backend/routes/index.js +++ b/backend/routes/index.js @@ -5,6 +5,9 @@ const logger = require("../config/logger"); // Import your Winston logger const router = express.Router(); let feedbackRouter; + + + try { feedbackRouter = require("./feedbackRouter"); } catch (error) { @@ -16,8 +19,6 @@ try { }; } -router.use("/feedback", feedbackRouter); -router.use("/reservation", require("./reservationRouter")); router.get("/", (req, res) => { res.json({ @@ -31,4 +32,11 @@ router.get("/", (req, res) => { }); }); + +router.use("/admin", require("./adminRouter")); +router.use("/feedback", feedbackRouter); +router.use("/user", require("./userRouter")); +router.use("/reservation", require("./reservationRouter")); + + module.exports = router; diff --git a/backend/routes/userRouter.js b/backend/routes/userRouter.js new file mode 100644 index 00000000..89fab850 --- /dev/null +++ b/backend/routes/userRouter.js @@ -0,0 +1,22 @@ +const express = require("express"); +const { createUser, loginUser } = require("../controller/user.controller"); +const router = express.Router(); +require("dotenv").config(); + + + +router.get("/", (req, res) => { + res.json({ + message: "Welcome to the User API!", + version: "1.0.0", + endpoints: { + login: "/login", + register: "/register", + }, + documentation: "https://api-docs-url.com",}); +}); + +router.post("/register", createUser); +router.post("/login", loginUser); + +module.exports = router; From f979fabc8cde97bad8775307465ecf6384731618 Mon Sep 17 00:00:00 2001 From: Samarth Vaidya Date: Tue, 8 Oct 2024 19:10:21 +0530 Subject: [PATCH 5/6] Changed some code to fix deployement --- ...r.controller.js => customer.controller.js} | 34 ++++++++++--------- .../{user.model.js => customer.model.js} | 8 ++--- .../{userRouter.js => customerRouter.js} | 6 ++-- backend/routes/index.js | 2 +- 4 files changed, 26 insertions(+), 24 deletions(-) rename backend/controller/{user.controller.js => customer.controller.js} (68%) rename backend/models/{user.model.js => customer.model.js} (58%) rename backend/routes/{userRouter.js => customerRouter.js} (68%) diff --git a/backend/controller/user.controller.js b/backend/controller/customer.controller.js similarity index 68% rename from backend/controller/user.controller.js rename to backend/controller/customer.controller.js index ff3b7333..41112cc1 100644 --- a/backend/controller/user.controller.js +++ b/backend/controller/customer.controller.js @@ -1,17 +1,19 @@ const bcrypt = require("bcrypt"); const { z } = require("zod"); -const User = require("../models/user.model"); +const Customer = require("../models/customer.model"); + + // Define the schema -const userSchema = z.object({ +const customerSchema = z.object({ name: z.string().min(1, "Name is required"), email: z.string().email("Invalid email address"), password: z.string().min(6, "Password must be at least 6 characters long"), }); -async function createUser(req, res) { +async function createCustomer(req, res) { // Validate the request body - const validation = userSchema.safeParse(req.body); + const validation = customerSchema.safeParse(req.body); if (!validation.success) { return res.status(400).json({ error: validation.error.errors }); @@ -19,37 +21,37 @@ async function createUser(req, res) { try { const hashedPassword = await bcrypt.hash(req.body.password, 10); - const user = new User({ + const customer = new Customer({ name: req.body.name, email: req.body.email, password: hashedPassword, }); - await user.save(); - res.status(201).json({ message: "User created successfully" }); + await customer.save(); + res.status(201).json({ message: "Customer created successfully" }); } catch (error) { res.status(500).json({ error: "Internal server error" }); } } -async function loginUser(req, res) { - const userSchema = z.object({ +async function loginCustomer(req, res) { + const customerSchema = 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 = userSchema.safeParse(req.body); + const validation = customerSchema.safeParse(req.body); if (!validation.success) { return res.status(400).json({ error: validation.error.errors }); } try { - const user = await User.findOne({ email: req.body.email }); - if (!user) { + const customer = await Customer.findOne({ email: req.body.email }); + if (!customer) { return res.status(404).json({ error: "User not found" }); } const validPassword = await bcrypt.compare( req.body.password, - user.password + customer.password ); if (!validPassword) { return res.status(401).json({ error: "Invalid password" }); @@ -63,6 +65,6 @@ async function loginUser(req, res) { module.exports = { - createUser, - loginUser - }; \ No newline at end of file + createCustomer, + loginCustomer +} \ No newline at end of file diff --git a/backend/models/user.model.js b/backend/models/customer.model.js similarity index 58% rename from backend/models/user.model.js rename to backend/models/customer.model.js index 8fed01e2..fa163004 100644 --- a/backend/models/user.model.js +++ b/backend/models/customer.model.js @@ -3,18 +3,18 @@ const Schema = mongoose.Schema; -const userSchema = new Schema({ +const customerSchema = new Schema({ name: String, password: String, email: String, role: { type: String, - default: "user" + default: "customer" }, bio: String, profilePicture: String, }); -const User = mongoose.models.User || mongoose.model('User', userSchema); +const Customer = mongoose.model('Customer', customerSchema); -module.exports = User; +module.exports = Customer; diff --git a/backend/routes/userRouter.js b/backend/routes/customerRouter.js similarity index 68% rename from backend/routes/userRouter.js rename to backend/routes/customerRouter.js index 89fab850..7dccb16a 100644 --- a/backend/routes/userRouter.js +++ b/backend/routes/customerRouter.js @@ -1,5 +1,5 @@ const express = require("express"); -const { createUser, loginUser } = require("../controller/user.controller"); +const { loginCustomer, createCustomer } = require("../controller/customer.controller"); const router = express.Router(); require("dotenv").config(); @@ -16,7 +16,7 @@ router.get("/", (req, res) => { documentation: "https://api-docs-url.com",}); }); -router.post("/register", createUser); -router.post("/login", loginUser); +router.post("/register", createCustomer); +router.post("/login", loginCustomer); module.exports = router; diff --git a/backend/routes/index.js b/backend/routes/index.js index a82c30e5..6b72eab3 100644 --- a/backend/routes/index.js +++ b/backend/routes/index.js @@ -35,7 +35,7 @@ router.get("/", (req, res) => { router.use("/admin", require("./adminRouter")); router.use("/feedback", feedbackRouter); -router.use("/user", require("./userRouter")); +router.use("/user", require("./customerRouter")); router.use("/reservation", require("./reservationRouter")); From 26356771ba4475c7f94c39ebccd259436d2ab090 Mon Sep 17 00:00:00 2001 From: Samarth Vaidya Date: Tue, 8 Oct 2024 19:44:30 +0530 Subject: [PATCH 6/6] coderabbit changes --- backend/controller/admin.controller.js | 21 ++++++++++++--- backend/controller/customer.controller.js | 13 ++++++--- backend/models/admin.model.js | 26 ++++++++++-------- backend/models/customer.model.js | 33 ++++++++++++++++------- 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/backend/controller/admin.controller.js b/backend/controller/admin.controller.js index 34a61c4b..bd87bfe5 100644 --- a/backend/controller/admin.controller.js +++ b/backend/controller/admin.controller.js @@ -1,6 +1,7 @@ const bcrypt = require("bcrypt"); const { z } = require("zod"); const Admin = require("../models/admin.model"); +const logger = require("../config/logger"); // Define the schema const adminSchema = z.object({ @@ -16,6 +17,10 @@ async function createAdmin(req, res) { if (!validation.success) { return res.status(400).json({ error: validation.error.errors }); } + const existingAdmin = await Admin.findOne({ email: req.body.email }); + if (existingAdmin) { + return res.status(409).json({ error: "Email is already registered" }); + } try { const hashedPassword = await bcrypt.hash(req.body.password, 10); @@ -27,18 +32,22 @@ async function createAdmin(req, res) { await admin.save(); res.status(201).json({ message: "Admin created successfully" }); } catch (error) { + logger.error("Error creating admin:", { + message: error.message, + stack: error.stack, + }); res.status(500).json({ error: "Internal server error" }); } } async function loginAdmin(req, res) { - const adminSchema = z.object({ + 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 = adminSchema.safeParse(req.body); + const validation = adminLoginSchema.safeParse(req.body); if(!validation.success) { return res.status(400).json({ error: validation.error.errors }); } @@ -46,15 +55,19 @@ async function loginAdmin(req, res) { try { const admin = await Admin.findOne({ email: req.body.email }); if (!admin) { - return res.status(404).json({ error: "Admin not found" }); + return res.status(401).json({ error: "Invalid email or password" }); } const validPassword = await bcrypt.compare(req.body.password, admin.password); if (!validPassword) { - return res.status(401).json({ error: "Invalid password" }); + return res.status(401).json({ error: "Invalid email or password" }); } res.json({ message: "Login successful" }); } catch (error) { + logger.error("Error logging in admin:", { + message: error.message, + stack: error.stack, + }); res.status(500).json({ error: "Internal server error" }); } } diff --git a/backend/controller/customer.controller.js b/backend/controller/customer.controller.js index 41112cc1..7e3add4e 100644 --- a/backend/controller/customer.controller.js +++ b/backend/controller/customer.controller.js @@ -19,6 +19,11 @@ async function createCustomer(req, res) { return res.status(400).json({ error: validation.error.errors }); } + const existingCustomer = await Customer.findOne({ email: req.body.email }); + if (existingCustomer) { + return res.status(400).json({ error: "Email is already registered" }); + } + try { const hashedPassword = await bcrypt.hash(req.body.password, 10); const customer = new Customer({ @@ -34,12 +39,12 @@ async function createCustomer(req, res) { } async function loginCustomer(req, res) { - const customerSchema = z.object({ + const customerLoginSchema = 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 = customerSchema.safeParse(req.body); + const validation = customerLoginSchema.safeParse(req.body); if (!validation.success) { return res.status(400).json({ error: validation.error.errors }); } @@ -47,14 +52,14 @@ async function loginCustomer(req, res) { try { const customer = await Customer.findOne({ email: req.body.email }); if (!customer) { - return res.status(404).json({ error: "User not found" }); + return res.status(401).json({ error: "Invalid email or password" }); } const validPassword = await bcrypt.compare( req.body.password, customer.password ); if (!validPassword) { - return res.status(401).json({ error: "Invalid password" }); + return res.status(401).json({ error: "Invalid email or password" }); } res.json({ message: "Login successful" }); } catch (error) { diff --git a/backend/models/admin.model.js b/backend/models/admin.model.js index 3aa60b37..6c8aca28 100644 --- a/backend/models/admin.model.js +++ b/backend/models/admin.model.js @@ -2,18 +2,22 @@ const mongoose = require("mongoose"); const Schema = mongoose.Schema; -const adminSchema = new Schema({ - name: String, - password: String, - email: String, - role: { - type: String, - default: "admin" +const adminSchema = new Schema( + { + name: { type: String, required: true }, + password: String, + email: { type: String, required: true, unique: true }, + role: { + type: String, + default: "admin", + }, + bio: String, + profilePicture: String, }, - bio: String, - profilePicture: String, -}); + { timestamps: true } +); + -const Admin = mongoose.model("User", adminSchema); +const Admin = mongoose.model("Admin", adminSchema); module.exports = Admin; \ No newline at end of file diff --git a/backend/models/customer.model.js b/backend/models/customer.model.js index fa163004..6ec78620 100644 --- a/backend/models/customer.model.js +++ b/backend/models/customer.model.js @@ -3,17 +3,30 @@ const Schema = mongoose.Schema; -const customerSchema = new Schema({ - name: String, - password: String, - email: String, - role: { - type: String, - default: "customer" +const customerSchema = new Schema( + { + name: { type: String, required: true }, + password: String, + email: { + type: String, + required: true, + unique: true, + validate: { + validator: function (v) { + return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v); + }, + message: (props) => `${props.value} is not a valid email address!`, + }, + }, + role: { + type: String, + default: "customer", + }, + bio: String, + profilePicture: String, }, - bio: String, - profilePicture: String, -}); + { timestamps: true } +); const Customer = mongoose.model('Customer', customerSchema);