Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nodemailer email script. #126

Open
wants to merge 2 commits into
base: restructure
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/app/components/Layout/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ class NavbarClass extends Component {
<DropdownItem tag={Link} to="/forgotPassword">
Forgot Password
</DropdownItem>
<DropdownItem divider />
<DropdownItem tag={Link} to="/signup">
Signup
</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
</Nav>
Expand Down
66 changes: 66 additions & 0 deletions client/app/components/Signup/Signup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { Component } from 'react'
import axios from "axios";
import { Link, Redirect } from 'react-router-dom';
import ReactLoading from '../common/Loading';
import { ToastContainer, ToastStore } from 'react-toasts';
import { Button, Label } from 'reactstrap';

export default class ForgotPassword extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: false,
isLoading: false
};
this.onSignup = this.onSignup.bind(this);
}

renderRedirect() {
if (this.state.redirect) {
return <Redirect push to='/' />
}
}

onSignup(event){
event.preventDefault();
const formElements = event.target.elements;
const firstName = formElements["firstName"].value;
const emailId = formElements["emailid"].value;
const password = formElements["password"].value;
const confirmPassword = formElements["confirmPassword"].value;
const signupData = {
firstName,
emailId,
password,
confirmPassword
}
//console.log(signupData);
}

render() {
return (
<div className="card bg-light">
{this.renderRedirect()}
<h4 className="card-header">Signup</h4>
<div className="card-body">
<form onSubmit={this.onSignup}>
<Label for="firstName">First Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Label>
<input className="mx-2 mb-2 p-1" type="text" name="firstName" placeholder="First Name" required/>
<br></br>
<Label for="emailId">Email ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Label>
<input className="mx-2 mb-2 p-1" type="text" name="emailid" placeholder="Email ID" required />
<br></br>
<Label for="password">Password&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Label>
<input className="mx-2 mb-2 p-1" type="password" name="password" placeholder="Password" required />
<br></br>
<Label for="confirmPassword">Retype Password</Label>
<input className="mx-2 mb-2 p-1" type="password" name="confirmPassword" placeholder="Retype Password" required />
<br></br>
<Button color="dark">Signup</Button>
</form>
</div>
<ToastContainer store={ToastStore} position={ToastContainer.POSITION.BOTTOM_LEFT} />
</div>
)
}
}
5 changes: 4 additions & 1 deletion client/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import downloadFile from './components/Pages/Assignments/downloadFile';
import zipFiles from './components/Pages/Assignments/zipFiles';
import updateHandle from './components/Pages/Profile/UpdateHandle';
import contribute from './components/Pages/Contribute';
import ReactLoading from './components/common/Loading'
import ReactLoading from './components/common/Loading';
import Signup from './components/Signup/Signup'
// import 'bootstrap/dist/css/bootstrap.min.css';


Expand Down Expand Up @@ -77,6 +78,8 @@ render((

<Route exact path="/forgotpassword" component={ForgotPassword} />

<Route exact path="/signup" component={Signup} />

<Route exact path="/reset/:token/:userID" component={ChangePassword} />

<Route component={NotFound} />
Expand Down
35 changes: 22 additions & 13 deletions server/routes/api/accountManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const UserSession = require('../../models/UserSession');
const jwt = require('jsonwebtoken');
var verifyUser = require('../../middleware/Token').verifyUser;
const fs = require('fs');
var nodemailer = require('nodemailer');
var path = require('path');
var privateKey = fs.readFileSync('server/sslcert/server.key', 'utf8'); //privatekey for jwt
const config = require('../../../config/config');
const mail=require("../../sendEmail/mainMail");

// TODO: Limit number of queries to these endpoints
// TODO: Async functionality
Expand Down Expand Up @@ -593,18 +593,27 @@ module.exports = (app) => {
}
console.log("JWT generated for forgot password.");
var link = config.host_url + 'reset/' + token + '/' + user._id.toString();
var writeData = user.basicInfo.email + "," + user.name.firstName + "," + link + "\n";
fs.appendFile("./server/sendEmail/emails.csv", writeData, function (err) {
if (err) {
return console.log(err);
}
console.log("Email scheduled");
});
return res.status(200).send({
success: true,
message: 'Email sent'
});

// var writeData = user.basicInfo.email + "," + user.name.firstName + "," + link + "\n";
// fs.appendFile("./server/sendEmail/emails.csv", writeData, function (err) {
// if (err) {
// return console.log(err);
// }
// console.log("Email scheduled");
// });
mail.sendPasswordResetMail(user.basicInfo.email, user.name.firstName, link)
.then( () => {
return res.status(200).send({
success: true,
message: 'Email sent'
});
})
.catch( (err) => {
return res.status(500).send({
success: false,
message: err.message
})
})

});
});
})
Expand Down
6 changes: 6 additions & 0 deletions server/sendEmail/mailCredentialsSample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
auth = {
user: '<your email id>',
pass: '<your gmail app password>'
}

module.exports=auth;
55 changes: 55 additions & 0 deletions server/sendEmail/mainMail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const nodemailer = require("nodemailer");
const auth = require("./mailCredentialsSample");
const fs = require("fs");

const transporter = nodemailer.createTransport({
service: 'gmail',
auth
});


const sendMail = (emailId, subject, text, html = null) => {
// If no template passed, only text shall be used by nodemailer.
const mailOptions = {
from: auth.user,
to: emailId,
subject,
text,
html
};
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
reject({success:false, message:error});
} else {
console.log('Email sent: ' + info.response);
resolve();
}
});
})
}

const sendPasswordResetMail = (userEmail, username, resetLink) => {
const subject = '[The Alcoding Club] Password Reset';
const text = `Hello ${username}, please reset password here: ${resetLink}`;
let mailTemplate = fs.readFileSync("server/sendEmail/emailTemplates/forgotPassword.txt","utf8");
mailTemplate = mailTemplate.replace("{{username}}", username);
mailTemplate = mailTemplate.replace("{{link}}", resetLink);

return new Promise((resolve, reject) => {
sendMail(userEmail, subject, text, mailTemplate)
.then(() =>{
resolve();
})
.catch((err) => {
reject(err);
})
})
}

module.exports = {
sendMail,
sendPasswordResetMail
}