From 0aa9c6957c30fee5b1bcaed3c439d63c360fdbce Mon Sep 17 00:00:00 2001 From: Anand Suthar Date: Fri, 31 May 2024 18:37:51 +0530 Subject: [PATCH] Added user-log-activity and minor fixes --- app/api/auth/login/route.ts | 2 +- app/api/auth/signup/route.ts | 2 +- app/api/auth/verifyotp/route.ts | 22 ++++++++++++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/api/auth/login/route.ts b/app/api/auth/login/route.ts index b58dd2a8..d599a1b5 100644 --- a/app/api/auth/login/route.ts +++ b/app/api/auth/login/route.ts @@ -1,5 +1,5 @@ import dbConfig from "@lib/db"; -import OtpTemplate from "@lib/emails/templates"; +import { OtpTemplate } from "@lib/emails/templates"; import { sendEmail } from "@lib/email"; import { render } from "@react-email/render"; import { generateSecureOTP } from "@utils/generateOtp"; diff --git a/app/api/auth/signup/route.ts b/app/api/auth/signup/route.ts index db693bce..d883bccf 100644 --- a/app/api/auth/signup/route.ts +++ b/app/api/auth/signup/route.ts @@ -1,5 +1,5 @@ import dbConfig from "@lib/db"; -import OtpTemplate from "@/lib/emails/templates"; +import { OtpTemplate } from "@/lib/emails/templates"; import { sendEmail } from "@lib/email"; import { render } from "@react-email/render"; import { generateSecureOTP } from "@utils/generateOtp"; diff --git a/app/api/auth/verifyotp/route.ts b/app/api/auth/verifyotp/route.ts index 4b668b72..b3535a14 100644 --- a/app/api/auth/verifyotp/route.ts +++ b/app/api/auth/verifyotp/route.ts @@ -1,5 +1,6 @@ import { setSession } from "@sessions/sessionUtils"; import dbConfig from "@lib/db"; +import logUserActivity from "@lib/logs"; type bodyType = { email: string; @@ -23,14 +24,14 @@ export async function POST(req: Request) { return Response.json({ error: "User role isn't valid." }); } - const result = await checkOTP(body); + const result = await checkOTP(body, req); return result; } catch (error) { console.error("Error during otp verification:", error); return Response.json({ error: "Internal Server Error" }); } } -async function checkOTP(body: bodyType) { +async function checkOTP(body: bodyType, req: Request) { const db = await dbConfig(); const collection = db.collection(body.role); @@ -38,12 +39,15 @@ async function checkOTP(body: bodyType) { const projection = { _id: 0, + username: 1, + firstname: 1, + lastname: 1, otp: 1, }; - const userOTP = await collection.findOne({ email }, { projection }); + const user = await collection.findOne({ email }, { projection }); - if (!userOTP || userOTP.otp !== body.otp) + if (!user || user.otp !== body.otp) return Response.json({ error: "OTP Verification Failed" }); await collection.updateOne({ email }, { $set: { otp: "" } }); @@ -51,5 +55,15 @@ async function checkOTP(body: bodyType) { // setting session for user (stores jwt token in cookies named session) await setSession(email, body.role); + const userlog = { + username: user.username, + name: `${user.firstname} ${user.lastname}`, + email, + role: body.role, + }; + + // storing user logs in db + await logUserActivity(userlog, req); + return Response.json({ message: "ok" }, { status: 200 }); }