-
Notifications
You must be signed in to change notification settings - Fork 4
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 #106 from Somnath-Chattaraj/test1111
test1111
- Loading branch information
Showing
11 changed files
with
372 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
19 changes: 19 additions & 0 deletions
19
backend/prisma/migrations/20241010094547_dev/migration.sql
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,19 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "pic" TEXT DEFAULT 'https://i1.sndcdn.com/artworks-000338788569-fxot50-t500x500.jpg'; | ||
|
||
-- CreateTable | ||
CREATE TABLE "Otp" ( | ||
"id" SERIAL NOT NULL, | ||
"otp" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"expiresAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "Otp_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Otp_userId_idx" ON "Otp"("userId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Otp" ADD CONSTRAINT "Otp_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("user_id") ON DELETE RESTRICT ON UPDATE CASCADE; |
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,92 @@ | ||
import asyncHandler from "express-async-handler"; | ||
import otp from "otp-generator"; | ||
import prisma from "../lib/prisma"; | ||
import sendMail from "../mail/sendMail"; | ||
import bcrypt from "bcrypt"; | ||
|
||
export const otpGenerator = asyncHandler(async (req: any, res: any) => { | ||
const {email} = req.body; | ||
|
||
const otpCode = otp.generate(6, { upperCaseAlphabets: false, specialChars: false }); | ||
try { | ||
const user = await prisma.user.findFirst({ | ||
where: { | ||
email, | ||
}, | ||
}); | ||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
const response = await prisma.otp.create({ | ||
data: { | ||
otp: otpCode, | ||
user: { | ||
connect: { | ||
email, | ||
}, | ||
}, | ||
expiresAt: new Date(Date.now() + 10 * 60 * 1000), | ||
}, | ||
}); | ||
const htmlContent = `<h1>Your OTP is ${otpCode}</h1>`; | ||
await sendMail(htmlContent, email); | ||
res.status(200).json({ message: "OTP generated successfully and email sent" }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Error in generating OTP" }); | ||
} | ||
}); | ||
|
||
|
||
export const verifyOtp = asyncHandler(async (req: any, res: any) => { | ||
const { otp, email } = req.body; | ||
|
||
try { | ||
const otpData = await prisma.otp.findFirst({ | ||
where: { | ||
otp: otp, | ||
|
||
user: { | ||
email, | ||
}, | ||
expiresAt: { | ||
gte: new Date(), | ||
}, | ||
}, | ||
}); | ||
|
||
if (!otpData) { | ||
return res.status(400).json({ message: "Invalid OTP" }); | ||
} | ||
|
||
await prisma.otp.delete({ | ||
where: { | ||
id: otpData.id, | ||
}, | ||
}); | ||
|
||
res.status(200).json({ message: "OTP verified successfully" }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Error in verifying OTP" }); | ||
} | ||
}); | ||
|
||
export const changePassword = asyncHandler(async (req: any, res: any) => { | ||
const {email, password} = req.body; | ||
const hashedPassword = await bcrypt.hash(password, 8); | ||
try { | ||
const response = await prisma.user.update({ | ||
where: { | ||
email, | ||
}, | ||
data: { | ||
password: hashedPassword, | ||
}, | ||
}); | ||
res.status(200).json({ message: "Password changed successfully" }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Error in changing password" }); | ||
} | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import express from "express"; | ||
import { calAvgRating } from "../controllers/ratingController"; | ||
import { changePassword, otpGenerator, verifyOtp } from "../controllers/otpController"; | ||
|
||
const Otprouter = express.Router(); | ||
|
||
Otprouter.post('/' ,otpGenerator); | ||
Otprouter.get('/verify',verifyOtp); | ||
Otprouter.post('/change', changePassword); | ||
export default Otprouter; |
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.