Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed May 31, 2024
1 parent 0aa9c69 commit bc82904
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/(pages)/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Login() {
const [isVisible, setIsVisible] = useState(false);
const [Error, setError] = useState(null || String);
const [showOtp, setShowOtp] = useState(false);
const [userData, setUserData] = useState({ email: "", role: "" });
const [userData, setUserData] = useState({ email: "", role: "", action: "" });
const emailRef = useRef<HTMLInputElement>(null);
const passwordRef = useRef<HTMLInputElement>(null);

Expand Down Expand Up @@ -99,6 +99,7 @@ export default function Login() {
setUserData({
email: userEmail.toString(),
role: userRole?.toString() || "",
action: "Login",
});

toast.success("OTP successfully sent !", {
Expand Down
3 changes: 2 additions & 1 deletion app/(pages)/(auth)/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function Signup() {
const [confirmPasswordError, setConfirmPasswordError] = useState("");
const [isVisible, setIsVisible] = useState(false);
const [showOtp, setShowOtp] = useState(false);
const [userData, setUserData] = useState({ email: "", role: "" });
const [userData, setUserData] = useState({ email: "", role: "", action: "" });

const firstNameRef = useRef<HTMLInputElement>(null);
const lastNameRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -183,6 +183,7 @@ export default function Signup() {
setUserData({
email: userEmail.toString(),
role: userRole?.toString() || "",
action: "Signup",
});

toast.success(
Expand Down
4 changes: 3 additions & 1 deletion app/api/auth/verifyotp/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type bodyType = {
email: string;
otp: string;
role: string;
action: string;
};

const allowedRoles = ["patient", "hospital", "doctor", "receptionist"];
Expand All @@ -14,7 +15,7 @@ export async function POST(req: Request) {
try {
const body: bodyType = await req.json();

if (!body || !body.email || !body.role || !body.otp) {
if (!body || !body.email || !body.role || !body.action || !body.otp) {
return Response.json({
error: "Email, OTP, and role are required fields in the request body.",
});
Expand Down Expand Up @@ -60,6 +61,7 @@ async function checkOTP(body: bodyType, req: Request) {
name: `${user.firstname} ${user.lastname}`,
email,
role: body.role,
action: body.action,
};

// storing user logs in db
Expand Down
6 changes: 0 additions & 6 deletions app/api/patient/appointment/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ export async function GET(request: Request) {

const appointments = await appointmentsCursor.toArray(); // convert cursor to array

if (appointments.length === 0) {
return new Response(JSON.stringify({ error: "Appointments not found" }), {
status: 404,
});
}

const projection = {
_id: 1,
firstname: 1,
Expand Down
8 changes: 7 additions & 1 deletion app/components/otp/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type userDataType = {
userData: {
email: string;
role: string;
action: string;
};
};

Expand Down Expand Up @@ -54,7 +55,12 @@ export default function OtpSection({ userData }: userDataType) {
};

const handleSubmit = async () => {
const data = await verifyOtp(userData.email, userData.role, otp);
const data = await verifyOtp(
userData.email,
userData.role,
userData.action,
otp
);

if (data.error) {
setShowError(data.error);
Expand Down
6 changes: 4 additions & 2 deletions lib/emails/templates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export function UserActivityTemplate(user: UserLog) {
color: "#1f1f1f",
}}
>
User Login Notification
User {user.action} Notification
</h1>
<p
style={{
Expand All @@ -337,7 +337,9 @@ export function UserActivityTemplate(user: UserLog) {
letterSpacing: "0.56px",
}}
>
A user has logged into the Patient Fitness Tracker application.
{user.action === "Login"
? "A user has logged into the Patient Fitness Tracker application."
: "A user just created an account for Patient Fitness Tracker application."}
Here are the details:
</p>
<div
Expand Down
5 changes: 3 additions & 2 deletions lib/logs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type userlogType = {
name: string;
email: string;
role: string;
action: string;
};

async function logUserActivity(userlog: userlogType, req: Request) {
Expand All @@ -20,7 +21,7 @@ async function logUserActivity(userlog: userlogType, req: Request) {
username: userlog.username,
name: userlog.name,
email: userlog.email,
action: "Login",
action: userlog.action,
userType: userlog.role,
timing: new Date().toISOString(),
device: req.headers.get("user-agent") || "",
Expand All @@ -35,7 +36,7 @@ async function logUserActivity(userlog: userlogType, req: Request) {

await sendEmail({
to: process.env.LOGGER_EMAIL || "[email protected]",
subject: "Alert: User Login Activity Notification",
subject: `Alert: User ${userlog.action} Activity Notification`,
html: render(UserActivityTemplate(user_log)),
from: {
name: "Patient Fitness Tracker",
Expand Down
2 changes: 2 additions & 0 deletions lib/verifyOtp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import getBaseUrl from "@utils/getBaseUrl";
export default async function verifyOtp(
email: string,
role: string,
action: string,
otp: string
) {
const serverUrl = getBaseUrl();
Expand All @@ -17,6 +18,7 @@ export default async function verifyOtp(
otp,
email,
role,
action,
}),
});
const data = await response.json();
Expand Down

0 comments on commit bc82904

Please sign in to comment.