Where the mind is without fear..
<html> <head> </head> <body> <div id='reg' class='inner' style='display: none;'> <div class='first'> <input name='name' id='name' type='text' size='35' placeholder='Your name'/> <input name='email' id='emailid' type='text' size='35' placeholder='Email Id'/> <input name='pin' id='pin' type='text' size='15' placeholder='PIN Code'/> <input name='pass' id='passid' type='password' size='25' placeholder='Password'/> <input name='conf' id='confrm' type='password' size='25' placeholder='Confirm Password'/> </div> <div class='first'> <button name='register' id='register' onclick='registerme()' type='button'>Register</button> <button name='back2' id='back2' onclick='doBackLog()' type='button'>Back</button> </div> </div> </body> </html>
async function registerme(){ var email = document.getElementById('emailid').value; var name = document.getElementById('name').value; var pin = document.getElementById('pin').value; var pass = document.getElementById('passid').value; var conf = document.getElementById('confrm').value; if(pass.localeCompare(conf) !== 0){ alert('Password mismatch'); return; } var addMe = {}; addMe[‘todo’] = 'newreg'; addMe['email'] = email; addMe['name'] = name; addMe['zip'] = pin; addMe['pass'] = pass; let formData = new FormData(); formData.append('mydata', JSON.stringify(addMe)); let res = await fetch('/registerMe', {method: 'POST', body: formData}); var data = await res.json(); data = JSON.stringify(data); alert(data); }
app.post('/registerMe', (req, res)=>{ var form = new formidable.IncomingForm(); form.parse(req,async (err, fields, files)=>{ var parsed = JSON.parse(fields.mydata); var email = parsed.email; var name = parsed.name; var zip = parsed.zip; var pass = parsed.pass; if(parsed.todo === 'newreg') sendLink(req,res, email, name, zip, pass); }); });
function sendLink(req,resp, to,name, zip, pass){ var user = {}; host = req.headers.host; //create random 16 character token var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var vtoken = ''; for(var i = 8; i > 0; --i){ vtoken += chars[Math.round(Math.random()*(chars.length - 1))] } var link = 'https://'+ host + '/verify?id='+vtoken; var userdata = { token: vtoken, host: host, to: to, name: name, zip: zip, pass: pass }; user[vtoken] = userdata; if(fs.existsSync(__dirname+'/abc/abc.json')){ var obj = fs.readFileSync(__dirname+'/abc/abc.json'); var json = JSON.parse(obj); var keys = Object.keys(json); json[vtoken] = userdata; fs.writeFileSync(__dirname+'/abc/abc.json',JSON.stringify(json), 'utf-8', (err)=>{ if(err){ return logger.debug("Error occured while saving json Object in sendLink"); } }); }else{ fs.writeFileSync('./abc/abc.json', JSON.stringify(user), 'utf-8', (err)=>{ if(err){ return logger.debug("Error occured while saving json Object"); } } ); } var mailOptions = { from: 'xxxxxx@xxxxxx.com', to: to, //req.query.to, subject: 'Please Confirm your Email Account', html: 'Hello,<br>Welcome to jeePhysicsAspirants<br>Please click on the link to Activate your Account.<br><a href='+link+'>Click here to verify</a>' } var isSend = false; var smtpTransport = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 465, secure: true, auth: { user: 'xxxxxx@xxxxxx.com', pass: xxxxxxxxxxxxxxx } }); smtpTransport.sendMail(mailOptions, function(error, response){ if(error){ return logger.debug(error);; } }); resp.end(JSON.stringify('Message sent, an email link has been sent to you to Activate your account')); }
app.get('/verify', (req, resp)=>{ var object = url.parse(req.url, true); var file = JSON.parse(fs.readFileSync(__dirname+'/test/test.json')); var reqid = object.query.id; if( typeof file[reqid] === 'undefined'){ resp.end(JSON.stringify('<h1>Bad Command</h1>')); return; } if(('https://'+req.headers.host) === ('https://'+file[reqid].host)){ if(reqid === file[reqid].token){ // here we can do our tasks related to successful verification resp.end(JSON.stringify('<h1>Email '+usrObj[reqid].to+'has been successfully verified.</h1>')); }else{ resp.end(JSON.stringify('<h1>Bad Request</h1>')); } }else{ resp.end(JSON.stringify('<h1>Request is from unknown source.</h1>')); } });