-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from samar12-rad/database/models
Feat: Database/models And LOGIN SIGNUP routes
- Loading branch information
Showing
9 changed files
with
267 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
}); | ||
|
||
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" }); | ||
} | ||
} | ||
|
||
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" }); | ||
} | ||
} | ||
|
||
|
||
module.exports = { createAdmin | ||
, loginAdmin | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
} | ||
|
||
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" }); | ||
} | ||
} | ||
|
||
|
||
|
||
module.exports = { | ||
createCustomer, | ||
loginCustomer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters