-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
464 additions
and
157 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
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 |
---|---|---|
@@ -1,31 +1,35 @@ | ||
import dbConfig from "@lib/db"; | ||
import WelcomeTemplate from "@/emails/otpmail"; | ||
import OtpTemplate from "@lib/emails/templates"; | ||
import { sendEmail } from "@lib/email"; | ||
import { render } from "@react-email/render"; | ||
import { generateSecureOTP } from "@utils/generateOtp"; | ||
import bcrypt from "bcrypt"; | ||
|
||
type LoginBody = { | ||
email: string; | ||
password: string; | ||
role: string; | ||
}; | ||
|
||
const allowedRoles = ["patient", "hospital", "doctor", "receptionist"]; | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
const body: LoginBody = await req.json(); | ||
switch (body.role) { | ||
case "patient": | ||
return setOTP(body); | ||
case "hospital": | ||
return setOTP(body); | ||
case "doctor": | ||
return setOTP(body); | ||
case "receptionist": | ||
return setOTP(body); | ||
|
||
default: | ||
return Response.json({ error: "Invalid user" }); | ||
if (!body || !body.email || !body.password || !body.role) { | ||
return Response.json({ | ||
error: | ||
"Invalid request body. Please provide email, password, and role.", | ||
}); | ||
} | ||
|
||
if (!allowedRoles.includes(body.role)) { | ||
return Response.json({ error: "User role isn't valid." }); | ||
} | ||
|
||
const result = await setOTP(body); | ||
return result; | ||
} catch (error) { | ||
console.error("Error during login:", error); | ||
return Response.json({ error: "Internal Server Error" }); | ||
|
@@ -37,9 +41,21 @@ async function setOTP(loginBody: LoginBody) { | |
|
||
const collection = db.collection(loginBody.role); | ||
const email = loginBody.email; | ||
const user = await collection.findOne({ email }); | ||
const projection = { | ||
_id: 0, | ||
email: 1, | ||
firstname: 1, | ||
lastname: 1, | ||
password: 1, | ||
}; | ||
const user = await collection.findOne( | ||
{ email }, | ||
{ | ||
projection, | ||
} | ||
); | ||
|
||
if (!user || user.password !== loginBody.password) { | ||
if (!user || !(await bcrypt.compare(loginBody.password, user.password))) { | ||
return Response.json( | ||
{ error: "Invalid email or password" }, | ||
{ status: 401 } | ||
|
@@ -59,7 +75,7 @@ async function setOTP(loginBody: LoginBody) { | |
const mailsent = await sendEmail({ | ||
to: send.to, | ||
subject: send.subject, | ||
html: render(WelcomeTemplate(send.name, send.otp)), | ||
html: render(OtpTemplate(send.name, send.otp)), | ||
from: { | ||
name: "Patient Fitness Tracker", | ||
address: "[email protected]", | ||
|
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import dbConfig from "@lib/db"; | ||
import WelcomeTemplate from "@/emails/otpmail"; | ||
import OtpTemplate from "@/lib/emails/templates"; | ||
import { sendEmail } from "@lib/email"; | ||
import { render } from "@react-email/render"; | ||
import { generateSecureOTP } from "@utils/generateOtp"; | ||
|
@@ -9,6 +9,7 @@ import { | |
patientadditionalDetails, | ||
receptionistadditionalDetails, | ||
} from "@constants/index"; | ||
import bcrypt from "bcrypt"; | ||
|
||
type SignupBody = { | ||
firstname: string; | ||
|
@@ -19,6 +20,8 @@ type SignupBody = { | |
role: string; | ||
}; | ||
|
||
const allowedRoles = ["patient", "hospital", "doctor", "receptionist"]; | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
const body: SignupBody = await req.json(); | ||
|
@@ -30,21 +33,12 @@ export async function POST(req: Request) { | |
); | ||
} | ||
|
||
switch (body.role) { | ||
case "patient": | ||
return createAccount(body); | ||
case "hospital": | ||
return createAccount(body); | ||
case "doctor": | ||
return createAccount(body); | ||
case "receptionist": | ||
return createAccount(body); | ||
|
||
default: | ||
return Response.json({ | ||
error: "Error creating account. Invalid user role!", | ||
}); | ||
if (!allowedRoles.includes(body.role)) { | ||
return Response.json({ error: "User role isn't valid." }); | ||
} | ||
|
||
const result = await createAccount(body); | ||
return result; | ||
} catch (error) { | ||
console.error("Error during signup:", error); | ||
return Response.json({ error: "Internal Server Error" }); | ||
|
@@ -57,6 +51,7 @@ async function createAccount(signupBody: SignupBody) { | |
const collection = db.collection(signupBody.role); | ||
const email = signupBody.email; | ||
const username = signupBody.username; | ||
|
||
const existingUser = await collection.findOne({ | ||
$or: [{ email }, { username }], | ||
}); | ||
|
@@ -91,7 +86,13 @@ async function createAccount(signupBody: SignupBody) { | |
break; | ||
} | ||
|
||
const user = { ...signupBody, ...additionalDetails }; | ||
const hashedPassword = await hashPassword(signupBody.password); | ||
|
||
const user = { | ||
...signupBody, | ||
...additionalDetails, | ||
password: hashedPassword, | ||
}; | ||
|
||
await collection.insertOne(user); | ||
|
||
|
@@ -108,7 +109,7 @@ async function createAccount(signupBody: SignupBody) { | |
const mailsent = await sendEmail({ | ||
to: send.to, | ||
subject: send.subject, | ||
html: render(WelcomeTemplate(send.name, send.otp)), | ||
html: render(OtpTemplate(send.name, send.otp)), | ||
from: { | ||
name: "Patient Fitness Tracker", | ||
address: "[email protected]", | ||
|
@@ -138,3 +139,9 @@ function checkMissingElements(body: SignupBody) { | |
} | ||
return false; | ||
} | ||
|
||
async function hashPassword(password: string) { | ||
const saltRounds = parseInt(process.env.BCRYPT_SALT_ROUNDS || "10"); // Read salt rounds from environment variable or default to "10" | ||
const hashedPassword = await bcrypt.hash(password, saltRounds); | ||
return hashedPassword; | ||
} |
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
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
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
Oops, something went wrong.