Skip to content

Commit

Permalink
authentication with otp
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektiwari47 committed Apr 18, 2024
1 parent 55d32a0 commit f95969e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 30 deletions.
18 changes: 12 additions & 6 deletions apps/client/src/components/visitor/Authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const Login = () => {
<div >
<div>
<h2 className='text-center text-[20px] font-medium pb-5'>Login</h2>
<p className='text-[#808080]'>User Name</p>
<input className='input1' type='text' value={username} onChange={(e) => setUsername(e.target.value)} />
<p className='text-[#808080]'>Email Id</p>
<input className='input1' type='email' value={username} onChange={(e) => setUsername(e.target.value)} />
<p className='text-[#808080]'>Password</p>
<input className='input1' type='password' value={password} onChange={(e) => setPassword(e.target.value)} />

Expand Down Expand Up @@ -151,17 +151,23 @@ const Login = () => {
<div>
<div className='authCard'>
<h2 className='text-center text-[20px] font-medium pb-5'>Signup</h2>
<p className='text-[#808080]'>User Name</p>
<input className='input1' type='text' value={username} onChange={(e) => setUsername(e.target.value)} />
<p className='text-[#808080]'>Email Id</p>
<input className='input1' type='email' value={username} onChange={(e) => setUsername(e.target.value)} />
<p className='text-[#808080]'>Password</p>
<input className='input1' type='password' value={password} onChange={(e) => setPassword(e.target.value)} />
<p className='text-[#808080]'>Full Name</p>
<input className='input1' type='text' value={fullName} onChange={(e) => setFullName(e.target.value)} />
<p className='text-[#808080]'>Hostel Name</p>
<input className='input1' type='text' value={hostelName} onChange={(e) => setHostelName(e.target.value)} />
<select className='input1' value={hostelName} onChange={(e) => setHostelName(e.target.value)}>
<option value="">Select Hostel</option>
<option value="CV Raman">CV Raman</option>
<option value="Charak">Charak</option>
<option value="Vishvakrama">Vishvakrama</option>

</select>
<p className='text-[#808080]'>Hostel Room</p>
<input className='input1' type='text' value={hostelRoom} onChange={(e) => setHostelRoom(e.target.value)} />
<button className='btn-primary sm:w-2/3 md:w-2/3 lg:w-2/3 mx-auto mt-[5vh]' onClick={handleSignup}>OTP</button>
<button className='btn-primary sm:w-2/3 md:w-2/3 lg:w-2/3 mx-auto mt-[5vh]' onClick={handleSignup}>SignUp</button>
</div>
</div>
);
Expand Down
35 changes: 26 additions & 9 deletions apps/server/dist/helper/buyMail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buyProduct = void 0;
exports.soldMail = void 0;
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
Expand All @@ -20,19 +20,36 @@ const transporter = nodemailer.createTransport({
pass: "qoii dhsi hkzq yxte",
},
});
// async..await is not allowed in global scope, must use a wrapper
function buyProduct(userMail) {
function soldMail(userMail, sellerMail, sellPrice, productName) {
return __awaiter(this, void 0, void 0, function* () {
// send mail with defined transport object
// Define the recipients
const recipients = sellerMail ? `${userMail}, ${sellerMail}` : userMail;
// Define the HTML content for the receipt
const htmlContent = `
<div>
<h1>Bid Receipt</h1>
<p>Item was sold!!</p>
<p><strong>Product Name:</strong> ${productName}</p>
<p><strong>Sell Price:</strong> INR${sellPrice.toFixed(2)}</p>
<p><strong>Buyer Mail:</strong> INR${userMail}</p>
<p><strong>Seller Mail:</strong> INR${sellerMail}</p>
<hr>
<p>
This is to inform you that biding ${productName} has been completed at a price of INR ${sellPrice.toFixed(2)}. to the user ${userMail}
</p>
<p>This is a confirmation of the sale transaction. Please keep it for your records.</p>
</div>
`;
// Send mail with defined transport object
const info = yield transporter.sendMail({
from: '"BidHub" <[email protected]>',
to: `${userMail}`,
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>", // html body
to: recipients,
subject: "Product Sold ✔",
text: `Biding ${productName} has been completed at a price of INR ${sellPrice.toFixed(2)}. to the user ${userMail} `,
html: htmlContent, // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <[email protected]>
});
}
exports.buyProduct = buyProduct;
exports.soldMail = soldMail;
3 changes: 1 addition & 2 deletions apps/server/dist/helper/otpmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ function sendOTPMail(userMail, otp) {
<div class="email-content">
<p>Hello,</p>
<p>Your One-Time Password (OTP) for verification is:</p>
<div class="otp-code">${otp}</div>
<div class="otp-code">${otp}</div>
</div>
</div>
</body>
Expand Down
4 changes: 2 additions & 2 deletions apps/server/dist/routes/activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ router.put('/buy/:productId', index_1.authenticateJwt, (req, res) => __awaiter(v
yield user.save();
yield seller.save();
console.log(user.username);
if (user.username != null) {
(0, buyMail_1.buyProduct)(user.username).catch((e) => {
if (user.username != null && seller.username != null && product.name != null) {
(0, buyMail_1.soldMail)(user.username, seller.username, product.sellPrice, product.name).catch((e) => {
console.log("Mail was not sent");
});
}
Expand Down
39 changes: 32 additions & 7 deletions apps/server/helper/buyMail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,42 @@ const transporter = nodemailer.createTransport({

});

// async..await is not allowed in global scope, must use a wrapper
export async function buyProduct(userMail:string | string[]) {
// send mail with defined transport object
export async function soldMail(
userMail: string,
sellerMail: string | undefined,
sellPrice: number,
productName: string
): Promise<void> {
// Define the recipients
const recipients = sellerMail ? `${userMail}, ${sellerMail}` : userMail;

// Define the HTML content for the receipt
const htmlContent = `
<div>
<h1>Bid Receipt</h1>
<p>Item was sold!!</p>
<p><strong>Product Name:</strong> ${productName}</p>
<p><strong>Sell Price:</strong> INR${sellPrice.toFixed(2)}</p>
<p><strong>Buyer Mail:</strong> ${userMail}</p>
<p><strong>Seller Mail:</strong> ${sellerMail}</p>
<hr>
<p>
This is to inform you that biding ${productName} has been completed at a price of INR ${sellPrice.toFixed(2)}. to the user ${userMail}
</p>
<p>This is a confirmation of the sale transaction. Please keep it for your records.</p>
</div>
`;

// Send mail with defined transport object
const info = await transporter.sendMail({
from: '"BidHub" <[email protected]>', // sender address
to: `${userMail}`, // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
to: recipients, // list of receivers
subject: "Product Sold ✔", // Subject line
text: `Biding ${productName} has been completed at a price of INR ${sellPrice.toFixed(2)}. to the user ${userMail} `, // plain text body
html: htmlContent, // html body
});


console.log("Message sent: %s", info.messageId);
// Message sent: <[email protected]>
}
Expand Down
10 changes: 6 additions & 4 deletions apps/server/routes/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { authenticateJwt } from "../middleware/index";
import {SECRET} from '../constants/index';
import { Product,Query,User } from "../db";
import mongoose from 'mongoose';
import { buyProduct } from '../helper/buyMail';
import { soldMail } from '../helper/buyMail';
import { moneyRefunded } from '../helper/moneyRefunded';
const router = express.Router();

Expand Down Expand Up @@ -202,10 +202,12 @@ router.put('/buy/:productId', authenticateJwt, async (req, res) => {
await user.save();
await seller.save();
console.log(user.username)
if(user.username!=null){
buyProduct(user.username).catch((e)=>{
if(user.username!=null && seller.username!=null && product.name!=null){
soldMail(user.username,seller.username,product.sellPrice,product.name).catch((e)=>{
console.log("Mail was not sent")
})}
});

}

// if(allUserIds.length!=0){
// moneyRefunded(allUserIds);
Expand Down

0 comments on commit f95969e

Please sign in to comment.