Spaces:
Sleeping
Sleeping
File size: 8,355 Bytes
97f53b4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const exphbs = require('express-handlebars');
var nodemailer = require('nodemailer');
var User = require('../Database/models/user');
const { successResponse, failedResponse } = require("../utils/responseModel");
var otp = Math.random();
otp = otp * 10000;
otp = parseInt(otp);
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]', // here use your real email
pass: 'vmllbwfiehtqaeep' // put your password correctly (not in this question please)
}
});
router.post('/', async (req, res, next) => {
try {
var email = req.body.email;
console.log(email);
var newOtpValue = Math.floor(1000 + Math.random() * 9000);
const data = await User.findOne({ email: email });
if (data) {
console.log(data);
//find the user using unique id for update token.
User.updateOne({
unique_id
: data.unique_id
}, {
$set: {
token: newOtpValue
}
}).exec()
.then(result => {
//Generate Main formate how you send.
var mailOptions = {
priority: "high",
from: '[email protected]',
to: email,
subject: `OTP for frogot password`,
html: `<div style="font-family: Helvetica,Arial,sans-serif;min-width:1000px;overflow:auto;line-height:2">
<div style="margin:50px auto;width:70%;padding:20px 0">
<div style="border-bottom:1px solid #eee">
<a href="" style="font-size:1.4em;color: #00466a;text-decoration:none;font-weight:600">Hayat Store</a>
</div>
<p style="font-size:1.1em">Hi ${data.username},</p>
<p>Thank you for choosing the Hayat. Use the following OTP to complete your forgot password procedures. OTP is valid for 2 minutes</p>
<h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;">${newOtpValue}</h2>
<p style="font-size:0.9em;">Regards,<br />Hayat Store</p>
<hr style="border:none;border-top:1px solid #eee" />
<div style="float:right;padding:8px 0;color:#aaa;font-size:0.8em;line-height:1;font-weight:300">
<p>Hayat Store Inc</p>
<p>Tamil Nadu,</p>
<p>Nagapattinum dt,</p>
<p>Enangudi.</p>
</div>
</div>
</div>` // html body
};
console.log(mailOptions);
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.status(201).send(failedResponse(error));
console.log('Error found' + error);
} else {
//Success message
res.status(200).send(successResponse("OTP sent to your email!"));
console.log('');
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
} else {
res.send(failedResponse("Invalid user!"))
}
} catch (error) {
res.status(500).send(failedResponse(error));
}
});
//Verify the otp
router.post('/verify', async (req, res, next) => {
try {
var email = req.body.email;
console.log(req.body.otp)
const data = await User.findOne({ email: email });
if (data) {
if (data.token == req.body.otp) {
User.updateOne({
unique_id
: data.unique_id
}, {
$set: {
token: ""
}
}).exec()
.then(result => {
res.send(successResponse("Verified successfully!"))
});
otp = 0;
}
else {
res.send(failedResponse("Invalid OTP!"));
}
} else {
res.send(failedResponse("Data not found!"));
}
} catch (err) {
res.send(failedResponse(err));
}
});
//Resend the otp
router.post('/resend', async function (req, res) {
try {
var email = req.body.email;
console.log(email);
var newOtpValue = Math.floor(1000 + Math.random() * 9000);
const data = await User.findOne({ email: email });
if (data) {
console.log(data);
//find the user using unique id for update token.
User.updateOne({
unique_id
: data.unique_id
}, {
$set: {
token: newOtpValue
}
}).exec()
.then(result => {
//Generate Main formate how you send.
var mailOptions = {
priority: "high",
from: '[email protected]',
to: '[email protected] ',
subject: `OTP for frogot password`,
html: `<div style="font-family: Helvetica,Arial,sans-serif;min-width:1000px;overflow:auto;line-height:2">
<div style="margin:50px auto;width:70%;padding:20px 0">
<div style="border-bottom:1px solid #eee">
<a href="" style="font-size:1.4em;color: #00466a;text-decoration:none;font-weight:600">Hayat Store</a>
</div>
<p style="font-size:1.1em">Hi ${data.username},</p>
<p>Thank you for choosing the Hayat. Use the following OTP to complete your forgot password procedures. OTP is valid for 1 minutes</p>
<h2 style="background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;">${newOtpValue}</h2>
<p style="font-size:0.9em;">Regards,<br />Hayat Store</p>
<hr style="border:none;border-top:1px solid #eee" />
<div style="float:right;padding:8px 0;color:#aaa;font-size:0.8em;line-height:1;font-weight:300">
<p>Hayat Store Inc</p>
<p>Tamil Nadu,</p>
<p>Nagapattinum dt,</p>
<p>Enangudi.</p>
</div>
</div>
</div>` // html body
};
console.log(mailOptions);
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.status(400).send(failedResponse(error));
console.log('Error found' + error);
} else {
//Success message
res.send(successResponse("OTP sent to your email!"));
console.log('OTP sent to your email!');
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
} else {
res.status(400).send(failedResponse("Invalid user!"))
}
} catch (error) {
res.status(400).send(failedResponse(err));
}
});
module.exports = router; |