386 lines
14 KiB
JavaScript
386 lines
14 KiB
JavaScript
const bcrypt = require("bcryptjs");
|
|
const User = require("../models/User");
|
|
const Student = require("../models/Student");
|
|
const jwt = require("jsonwebtoken");
|
|
const JWT_secret = "Cantileverlabs";
|
|
const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS", null, [
|
|
"ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX",
|
|
]);
|
|
const nodemailer = require("nodemailer");
|
|
const smtpTransport = require("nodemailer-smtp-transport");
|
|
|
|
// -------------------------------------------- mail transporter -----------------------------------------
|
|
|
|
var transport = nodemailer.createTransport(
|
|
smtpTransport({
|
|
host: `${process.env.HOST}`, //`${process.env.HOST}`
|
|
port: 465,
|
|
auth: {
|
|
user: `${process.env.EMAIL}`, //`${process.env.EMAIL}`
|
|
pass: `${process.env.PASS}`, //`${process.env.PASS}`
|
|
},
|
|
})
|
|
);
|
|
|
|
// -------------------------------------------- mail transporter -----------------------------------------
|
|
|
|
module.exports.Protected = async (req, res, next) => {
|
|
res.send("Hello User");
|
|
};
|
|
module.exports.postSignup = async (req, res, next) => {
|
|
try {
|
|
//we need firstName , lastName , email , password as input
|
|
let firstName = req.body.firstName || " ";
|
|
let lastName = req.body.lastName || " ";
|
|
const { sending_company_email, email, password, subject, _html } = req.body;
|
|
let user = await User.findOne({ email: email });
|
|
if (user) {
|
|
res.json({
|
|
message: "User already exist",
|
|
type: "error",
|
|
});
|
|
} else {
|
|
const email_otp = Math.floor(100000 + Math.random() * 900000);
|
|
console.log("otp", email_otp);
|
|
const hashedPass = await bcrypt.hash(password, 12);
|
|
user = new User({
|
|
firstName: firstName,
|
|
lastName: lastName,
|
|
email: email,
|
|
password: hashedPass,
|
|
isAdmin: false,
|
|
email_otp,
|
|
});
|
|
user = await user.save();
|
|
await Student.deleteOne({ user: user._id });
|
|
let student = new Student({
|
|
user: user._id,
|
|
});
|
|
student = await student.save();
|
|
user.student = student._id;
|
|
await user.save();
|
|
const message = {
|
|
from: `${sending_company_email}`, // Sender address
|
|
to: `${email}`, // List of recipients
|
|
subject: `${subject}`, // Subject line
|
|
html: `${_html}`, // design html for email message.
|
|
};
|
|
transport.sendMail(message, function (err, info) {
|
|
if (err) {
|
|
console.log(err);
|
|
} else {
|
|
console.log(info);
|
|
}
|
|
});
|
|
res.json({
|
|
message: "OTP has sent to the Email",
|
|
type: "success",
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
};
|
|
|
|
module.exports.verfiyemail = async (req, res, next) => {
|
|
const { email, otp } = req.body;
|
|
try {
|
|
let user = await User.findOne({ email: email });
|
|
if (user) {
|
|
const isMatched = await (user.email_otp == otp ? true : false);
|
|
if (isMatched) {
|
|
if (!user.isVerified) {
|
|
user.isVerified = true;
|
|
await user.save();
|
|
res.json({
|
|
message: "User Verified, Please Login",
|
|
});
|
|
} else {
|
|
res.json({
|
|
message: "User Already Verified, Please Login",
|
|
});
|
|
}
|
|
} else {
|
|
res.json({
|
|
message: "OTP Doesn't Matched!",
|
|
type: "error",
|
|
});
|
|
}
|
|
} else {
|
|
res.json({
|
|
message: "No user with this email exists",
|
|
type: "error",
|
|
});
|
|
}
|
|
} catch {
|
|
(err) => {
|
|
console.log(err);
|
|
};
|
|
}
|
|
};
|
|
|
|
module.exports.postSignin = async (req, res, next) => {
|
|
try {
|
|
//we need email and password as input
|
|
let email = req.body.email;
|
|
let password = req.body.password;
|
|
let user = await User.findOne({ email: email });
|
|
if (user) {
|
|
const isMatched = await bcrypt.compare(password, user.password);
|
|
if (isMatched) {
|
|
const token = jwt.sign({ _id: user._id }, JWT_secret);
|
|
res.json({
|
|
token: token,
|
|
});
|
|
} else {
|
|
res.json({
|
|
message: "email and password doesn't match",
|
|
type: "error",
|
|
});
|
|
}
|
|
} else {
|
|
res.json({
|
|
message: "No user with this email exists",
|
|
type: "error",
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
};
|
|
|
|
// Phone verification Starts.
|
|
// -----------------------------------------------------------------------------------------------
|
|
|
|
module.exports.sendOTP = (req, res, next) => {
|
|
//uNNYosMopvvCW9RTR1tRWJmYC test
|
|
//llVKD53ve6QRpbCKOHzWBADaS live
|
|
const { phoneNumber } = req.body;
|
|
try {
|
|
if (!phoneNumber) {
|
|
res.status(422).json({ message: "Please Add All Required Fields" });
|
|
return;
|
|
} else {
|
|
messagebird.verify.create(
|
|
phoneNumber,
|
|
{
|
|
template: "Your verification code is %token",
|
|
},
|
|
function (err, response) {
|
|
if (err) {
|
|
console.log(err);
|
|
res.status(422).json({ message: err.errors[0].description });
|
|
} else {
|
|
console.log(response);
|
|
res.json({ id: response.id });
|
|
}
|
|
}
|
|
);
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
};
|
|
|
|
module.exports.getOTP = (req, res, next) => {
|
|
try {
|
|
const { id, otp } = req.body;
|
|
messagebird.verify.verify(id, otp, function (err, response) {
|
|
if (err) {
|
|
console.log({ error: err.errors[0].description, id: id });
|
|
res.json({ error: err.errors[0].description, id: id });
|
|
} else {
|
|
console.log(response);
|
|
res.json({ message: "Code Verified" });
|
|
}
|
|
});
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
};
|
|
// Phone verification End.
|
|
// -----------------------------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------------------------
|
|
// Forgot password Starts
|
|
|
|
module.exports.forgotpassword = async (req, res, next) => {
|
|
const { email, link, sending_company_email, subject } = req.body;
|
|
//link = https://cantileverlabs.herokuapp.com/resetpassword/:id/:token
|
|
try {
|
|
await User.findOne({ email }).then((user) => {
|
|
if (!user) {
|
|
res.status(404).json({ error: "User not found with this Email" });
|
|
return;
|
|
} else {
|
|
const payload = {
|
|
email: user.email,
|
|
_id: user._id,
|
|
};
|
|
const secret = JWT_secret + user.password;
|
|
const token = jwt.sign(payload, secret, { expiresIn: "10m" });
|
|
User.findByIdAndUpdate(user._id, {
|
|
$set: { passwordResetToken: token },
|
|
})
|
|
.then((data) => {
|
|
const reset_link = `${link}/${user._id}/${token}`;
|
|
const message = {
|
|
from: `${sending_company_email}`, // Sender address
|
|
to: `${user.email}`, // List of recipients
|
|
subject: `${subject}`, // Subject line
|
|
html: `
|
|
<!doctype html>
|
|
<html lang="en-US">
|
|
|
|
<head>
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
|
|
<title>Reset Password Email Template</title>
|
|
<meta name="description" content="Reset Password Email Template.">
|
|
<style type="text/css">
|
|
a:hover {text-decoration: underline !important;}
|
|
</style>
|
|
</head>
|
|
|
|
<body marginheight="0" topmargin="0" marginwidth="0" style="margin: 0px; background-color: #f2f3f8;" leftmargin="0">
|
|
<!--100% body table-->
|
|
<table cellspacing="0" border="0" cellpadding="0" width="100%" bgcolor="#f2f3f8"
|
|
style="@import url(https://fonts.googleapis.com/css?family=Rubik:300,400,500,700|Open+Sans:300,400,600,700); font-family: 'Open Sans', sans-serif;">
|
|
<tr>
|
|
<td>
|
|
<table style="background-color: #f2f3f8; max-width:670px; margin:0 auto;" width="100%" border="0"
|
|
align="center" cellpadding="0" cellspacing="0">
|
|
<tr>
|
|
<td style="height:80px;"> </td>
|
|
</tr>
|
|
<tr>
|
|
<td style="text-align:center;">
|
|
<a href="https://cantileverlabs.com" title="logo" target="_blank">
|
|
<img width="80" height="80"
|
|
src="https://media-exp1.licdn.com/dms/image/C510BAQEgcV3sgE1PIA/company-logo_200_200/0/1552289011007?e=2159024400&v=beta&t=FO8loLVwC5qoHmYkk-gR-mv7vC36LPG17yZkxOFl6Go" title="logo" alt="logo">
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="height:20px;"> </td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0"
|
|
style="max-width:670px;background:#fff; border-radius:3px; text-align:center;-webkit-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);-moz-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);box-shadow:0 6px 18px 0 rgba(0,0,0,.06);">
|
|
<tr>
|
|
<td style="height:40px;"> </td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding:0 35px;">
|
|
<h1 style="color:#1e1e2d; font-weight:500; margin:0;font-size:32px;font-family:'Rubik',sans-serif;">You have
|
|
requested to reset your password</h1>
|
|
<span
|
|
style="display:inline-block; vertical-align:middle; margin:29px 0 26px; border-bottom:1px solid #cecece; width:100px;"></span>
|
|
<p style="color:#455056; font-size:15px;line-height:24px; margin:0;">
|
|
We cannot simply send you your old password. A unique link to reset your
|
|
password has been generated for you. To reset your password, click the
|
|
following link and follow the instructions.
|
|
</p>
|
|
<a href=${reset_link}
|
|
style="background:#F9F871;text-decoration:none !important; font-weight:500; margin-top:35px; color:#111;text-transform:uppercase; font-size:14px;padding:10px 24px;display:inline-block;border-radius:50px;">Reset
|
|
Password</a>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="height:40px;"> </td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<tr>
|
|
<td style="height:20px;"> </td>
|
|
</tr>
|
|
<tr>
|
|
<td style="text-align:center;">
|
|
<div class="reset-logo2-J ">
|
|
<img src="./images/Icon material-copyright.png" alt="">
|
|
<span class="light-copyright-J">copyright <span class="bold-copyright-J">2018 Cantilever Labs</span></span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="height:80px;"> </td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<!--/100% body table-->
|
|
</body>
|
|
|
|
</html>
|
|
`, // design html for email message.
|
|
};
|
|
transport.sendMail(message, function (err, info) {
|
|
if (err) {
|
|
console.log(err);
|
|
} else {
|
|
console.log(info);
|
|
}
|
|
});
|
|
res.status(200).json({
|
|
message: "Link is Active for 10 mins",
|
|
reset_link,
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
}
|
|
});
|
|
} catch {
|
|
(error) => {
|
|
console.log("Error from forgot pass", error);
|
|
};
|
|
}
|
|
};
|
|
module.exports.resetpassword = async (req, res, next) => {
|
|
const { _id, token } = req.params;
|
|
const { password } = req.body;
|
|
try {
|
|
let user = await User.findById({ _id });
|
|
if (!user) {
|
|
res.json({ error: "User not Found or WrongId" });
|
|
return;
|
|
} else {
|
|
const secret = JWT_secret + user.password;
|
|
const user_token = user.passwordResetToken;
|
|
const payload = jwt.verify(token, secret);
|
|
const hashedPass = await bcrypt.hash(password, 12);
|
|
if (token == user_token) {
|
|
user.password = hashedPass;
|
|
await user
|
|
.save()
|
|
.then((ok) => {
|
|
res.json({ message: "Password Updated!" });
|
|
})
|
|
.catch((err) => {
|
|
console.log("Error in save", err);
|
|
});
|
|
} else {
|
|
res.status(422).json({ error: "Either Token not found or Expired!" });
|
|
return;
|
|
}
|
|
}
|
|
} catch {
|
|
(err) => {
|
|
console.log("error from try catch resetpass", err);
|
|
};
|
|
}
|
|
};
|
|
|
|
// Forgot password Ends
|
|
// -----------------------------------------------------------------------------------------------
|
|
|
|
module.exports.checkProtected = (req, res, next) => {
|
|
console.log(req.user);
|
|
res.json({
|
|
message: "Protected",
|
|
user: req.user,
|
|
});
|
|
};
|