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

Database/models And LOGIN SIGNUP routes #159

Merged
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
</a>
</td>
<td align="center">
<a href="https://github.com/itznayan">
<img src="https://avatars.githubusercontent.com/u/136584376?v=4" width="100;" alt="itznayan"/>
<a href="https://github.com/vishnuprasad2004">
<img src="https://avatars.githubusercontent.com/u/116942066?v=4" width="100;" alt="vishnuprasad2004"/>
<br />
<sub><b>Mahera Nayan</b></sub>
<sub><b>Vishnu Prasad Korada</b></sub>
</a>
</td>
<td align="center">
Expand Down Expand Up @@ -192,6 +192,8 @@ Special thanks to our amazing mentors who are guiding this project! 🙌
<sub><b>Ayush Yadav</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/mishradev1">
<img src="https://avatars.githubusercontent.com/u/118660840?v=4" width="100;" alt="mishradev1"/>
Expand Down
78 changes: 78 additions & 0 deletions backend/controller/admin.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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({
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"),
});
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved

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 });
}
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);
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) {
logger.error("Error creating admin:", {
message: error.message,
stack: error.stack,
});
res.status(500).json({ error: "Internal server error" });
}
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
}

async function loginAdmin(req, res) {

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 });
if (!admin) {
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 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" });
}
}
RamakrushnaBiswal marked this conversation as resolved.
Show resolved Hide resolved


module.exports = { createAdmin
, loginAdmin
};
75 changes: 75 additions & 0 deletions backend/controller/customer.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const bcrypt = require("bcrypt");
const { z } = require("zod");
const Customer = require("../models/customer.model");



// Define the schema
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 createCustomer(req, res) {
// Validate the request body
const validation = customerSchema.safeParse(req.body);

if (!validation.success) {
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({
name: req.body.name,
email: req.body.email,
password: hashedPassword,
});
await customer.save();
res.status(201).json({ message: "Customer created successfully" });
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
samar12-rad marked this conversation as resolved.
Show resolved Hide resolved
}
RamakrushnaBiswal marked this conversation as resolved.
Show resolved Hide resolved

async function loginCustomer(req, res) {
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 = customerLoginSchema.safeParse(req.body);
if (!validation.success) {
return res.status(400).json({ error: validation.error.errors });
}

try {
const customer = await Customer.findOne({ email: req.body.email });
if (!customer) {
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 email or password" });
}
res.json({ message: "Login successful" });
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
}
RamakrushnaBiswal marked this conversation as resolved.
Show resolved Hide resolved



module.exports = {
createCustomer,
loginCustomer
}
RamakrushnaBiswal marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions backend/models/admin.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

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,
},
{ timestamps: true }
);


const Admin = mongoose.model("Admin", adminSchema);

module.exports = Admin;
33 changes: 33 additions & 0 deletions backend/models/customer.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;



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,
},
{ timestamps: true }
);

const Customer = mongoose.model('Customer', customerSchema);

module.exports = Customer;
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "ISC",
"description": "",
"dependencies": {
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.0",
Expand Down
20 changes: 20 additions & 0 deletions backend/routes/adminRouter.js
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 22 additions & 0 deletions backend/routes/customerRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require("express");
const { loginCustomer, createCustomer } = require("../controller/customer.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", createCustomer);
router.post("/login", loginCustomer);

module.exports = router;
12 changes: 10 additions & 2 deletions backend/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -16,8 +19,6 @@ try {
};
}

router.use("/feedback", feedbackRouter);
router.use("/reservation", require("./reservationRouter"));

router.get("/", (req, res) => {
res.json({
Expand All @@ -31,4 +32,11 @@ router.get("/", (req, res) => {
});
});


router.use("/admin", require("./adminRouter"));
router.use("/feedback", feedbackRouter);
router.use("/user", require("./customerRouter"));
router.use("/reservation", require("./reservationRouter"));


module.exports = router;
Loading