Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions backend/controllers/passController.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
const User = require("../model/user.model")
const Pass = require("../model/pass.model")
const { ErrorHandler } = require("../middleware/errorMiddleware")

//@desc Generate a pass by user
//@route POST /users/generatePass
//@route POST /passes/generatePass
const generatePass = async (req, res, next) => {
try {
const { name, email, phone, duration, start } = req.body
const { name, phone, duration, start } = req.body
let email = req.body.email?.toLowerCase()
const emailRegex = /^[^\s+@]+@[^\s@]+\.[^\s@]{2,}$/i
const phoneRegex = /^\+?\d{1,3}[- ]?\d{3}[- ]?\d{3}[- ]?\d{4}$/
if (!name?.length) throw new ErrorHandler(400, "Please enter a valid name")
if (!email?.length)
throw new ErrorHandler(400, "Please enter a valid email address")
else if (!emailRegex.test(email)) {
throw new ErrorHandler(400, "Invalid email! Please try again")
}
if (!phone?.length)
throw new ErrorHandler(400, "Please enter a valid phone number")
else if (!phoneRegex.test(phone)) {
throw new ErrorHandler(
400,
"Invalid Phone Number! Please try with country code"
)
}
if (!duration?.length)
throw new ErrorHandler(400, "Please enter a valid duration")
if (!start?.length)
throw new ErrorHandler(400, "Please enter a valid start time")

const newPass = await Pass.create({
name,
phone,
email,
duration,
start: start+":00.000Z",
start: start + ":00.000Z",
generatedUserId: req.user.id,
userName: req.user.name,
})
Expand All @@ -29,7 +49,7 @@ const generatePass = async (req, res, next) => {
}

//@desc View all passes generated by a user
//@route Get /users/viewPasses
//@route Get /passes/viewPasses
const viewPasses = async (req, res, next) => {
try {
const passes = await Pass.find({ generatedUserId: req.user.id })
Expand All @@ -45,7 +65,7 @@ const viewPasses = async (req, res, next) => {
}

//@desc Verify pass by guard
//@route PUT /users/verifyPass
//@route PUT /passes/verifyPass/:id
const verifyPass = async (req, res, next) => {
try {
const passVerified = await Pass.findOneAndUpdate(
Expand Down
21 changes: 21 additions & 0 deletions backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,30 @@ const registerUser = async (req, res, next) => {
try {
const { name, password, confirmpwd, phone } = req.body
let email = req.body.email?.toLowerCase()
const emailRegex = /^[^\s+@]+@[^\s@]+\.[^\s@]{2,}$/i
const phoneRegex = /^\+?\d{1,3}[- ]?\d{3}[- ]?\d{3}[- ]?\d{4}$/
if (!email?.length)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are using the same checks statement in signup as well as singin, we can move those lines to some utility file and use the function to check email and other similar stuff. You can also try to read something around it.

throw new ErrorHandler(400, "Please enter a valid email address")
else if (!emailRegex.test(email)) {
throw new ErrorHandler(400, "Invalid email! Please try again")
}
if (!name?.length) throw new ErrorHandler(400, "Please enter a valid name")
if (!password?.length)
throw new ErrorHandler(400, "Please enter a valid password")
if (!confirmpwd?.length)
throw new ErrorHandler(400, "Please enter a valid confirm password")
if (password != confirmpwd) {
throw new ErrorHandler(400, "Passwords do not match")
}
if (!phone?.length)
throw new ErrorHandler(400, "Please enter a valid phone number")
else if (!phoneRegex.test(phone)) {
throw new ErrorHandler(
400,
"Invalid Phone Number! Please try with country code"
)
}

const userExists = await User.findOne({ email })

if (userExists) {
Expand Down Expand Up @@ -49,6 +68,8 @@ const loginUser = async (req, res, next) => {
let email = req.body.email?.toLowerCase()
if (!email?.length)
throw new ErrorHandler(400, "Please enter a valid email address")
if (!password?.length)
throw new ErrorHandler(400, "Please enter a valid password")

const user = await User.findOne({ email })

Expand Down