diff --git a/constants.js b/constants.js index 959b340..772536e 100644 --- a/constants.js +++ b/constants.js @@ -2,5 +2,5 @@ exports.token = process.env.SECRET exports.slack = process.env.SLACK_TOKEN exports.webhookURL = process.env.INVITE_CHANNEL_WEBHOOK exports.glitch = process.env.GLITCH_SECRET -exports.selfEmail = 'IIITV Coding Club ' +exports.selfEmail = 'codingclub@iiitvadodara.ac.in' exports.githubApi = 'https://api.github.com' diff --git a/package-lock.json b/package-lock.json index 6684039..c32c88b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "express": "^4.16.3", "gh-account-exists": "^0.1.0", "jquery": "^3.5.0", + "nodemailer": "^6.9.1", "path": "^0.12.7", "uikit": "^3.1.4", "url-crypt": "*" @@ -2191,6 +2192,14 @@ "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "dev": true }, + "node_modules/nodemailer": { + "version": "6.9.1", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.1.tgz", + "integrity": "sha512-qHw7dOiU5UKNnQpXktdgQ1d3OFgRAekuvbJLcdG5dnEo/GtcTHRYM7+UfJARdOFU9WUQO8OiIamgWPmiSFHYAA==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/normalize-package-data": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", @@ -5105,6 +5114,11 @@ "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "dev": true }, + "nodemailer": { + "version": "6.9.1", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.1.tgz", + "integrity": "sha512-qHw7dOiU5UKNnQpXktdgQ1d3OFgRAekuvbJLcdG5dnEo/GtcTHRYM7+UfJARdOFU9WUQO8OiIamgWPmiSFHYAA==" + }, "normalize-package-data": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", diff --git a/package.json b/package.json index 5604a4c..91fc70f 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "express": "^4.16.3", "gh-account-exists": "^0.1.0", "jquery": "^3.5.0", + "nodemailer": "^6.9.1", "path": "^0.12.7", "uikit": "^3.1.4", "url-crypt": "*" diff --git a/public/client.js b/public/client.js index f63327b..75e8d29 100644 --- a/public/client.js +++ b/public/client.js @@ -1,175 +1,175 @@ -$ ('#email').keyup (function () { - const email = $ ('#email'); - if (email.val ()) { - if (!validateEmail (email.val ())) { - email.css ({ +$('#email').keyup(function () { + const email = $('#email') + if (email.val()) { + if (!validateEmail(email.val())) { + email.css({ color: '#f0506e', - 'border-color': '#f0506e', - }); + 'border-color': '#f0506e' + }) } else { - email.css ({ + email.css({ color: '#32d296', - 'border-color': '#32d296', - }); + 'border-color': '#32d296' + }) } } -}); +}) var validateEmail = function (emailValue) { - var regex = /^\w+([-]?\w+)*@\w+([-]?\w+)*(\.\w{2,3})+$/; - return regex.test (emailValue); -}; + var regex = /^\w+([-]?\w+)*@\w+([-]?\w+)*(\.\w{2,3})+$/ + return regex.test(emailValue) +} -$ ('#username').keyup (function () { - const username = $ ('#username'); - if (username.val ()) { - const profile = `https://api.aashutosh.dev/gh/${username.val ()}`; - fetch (profile) - .then (response => response.json ()) - .then (data => { +$('#username').keyup(function () { + const username = $('#username') + if (username.val()) { + const profile = `https://api.aashutosh.dev/gh/${username.val()}` + fetch(profile) + .then(response => response.json()) + .then(data => { if (data.error) { - username.css ({ + username.css({ color: '#f0506e', - 'border-color': '#f0506e', - }); + 'border-color': '#f0506e' + }) } else { - username.css ({ + username.css({ color: '#32d296', - 'border-color': '#32d296', - }); + 'border-color': '#32d296' + }) } }) - .catch (e => { - console.log (e); - }); + .catch(e => { + console.log(e) + }) } else { - username.css ({ + username.css({ color: '#32d296', - 'border-color': '#32d296', - }); + 'border-color': '#32d296' + }) } -}); +}) window.getStatus = function (url) { - const request = new XMLHttpRequest (); + const request = new XMLHttpRequest() request.onreadystatechange = function () { if (request.readyState === 2) { - return request.status; + return request.status // this contains the status code } - }; - request.open ('GET', url, true); - request.send (); -}; + } + request.open('GET', url, true) + request.send() +} $.put = function (url, data, callback, type) { - if ($.isFunction (data)) { - type = type || callback; - callback = data; - data = {}; + if ($.isFunction(data)) { + type = type || callback + callback = data + data = {} } - return $.ajax ({ + return $.ajax({ url: url, type: 'PUT', success: callback, data: data, - contentType: type, - }); -}; + contentType: type + }) +} -$ (function () { - $ ('form').submit (function (event) { - event.preventDefault (); +$(function () { + $('form').submit(function (event) { + event.preventDefault() - const username = $ ('#username').val (); - const email = $ ('#email').val (); - fetch (`https://api.github.com/users/${username}`) - .then (res => res.json ()) - .then (out => { + const username = $('#username').val() + const email = $('#email').val() + fetch(`https://api.github.com/users/${username}`) + .then(res => res.json()) + .then(out => { if (out.message && out.message === 'Not Found') { // eslint-disable-next-line UIkit.notification ({ - message: "Username not found.", + message: 'Username not found.', status: 'danger', pos: 'top-center', - timeout: 1000, - }); + timeout: 1000 + }) } else { - fetch (`/sendmail/${username}/${email}`) - .then (res => { + fetch(`/sendmail/${username}/${email}`) + .then(res => { if (res.status === 200) { // eslint-disable-next-line UIkit.notification ({ - message: "A verification E-mail has been sent.", + message: 'A verification E-mail has been sent.', status: 'success', pos: 'top-center', - timeout: 2000, - }); + timeout: 2000 + }) } }) - .catch (e => { - console.error (e); + .catch(e => { + console.error(e) // eslint-disable-next-line UIkit.notification ({ - message: "An error occured. Please try again later.", + message: 'An error occured. Please try again later.', status: 'danger', pos: 'top-center', - timeout: 1000, - }); - }); + timeout: 1000 + }) + }) } }) - .catch (err => { - console.error (err); + .catch(err => { + console.error(err) // eslint-disable-next-line UIkit.notification ({ - message: "An error occured. Please try again later.", + message: 'An error occured. Please try again later.', status: 'danger', pos: 'top-center', - timeout: 1000, - }); - }); - }); -}); + timeout: 1000 + }) + }) + }) +}) window.showToast = async function () { - const username = $ ('#username').val (); - const email = $ ('#email').val (); + const username = $('#username').val() + const email = $('#email').val() if (username === '' && email === '') { // eslint-disable-next-line UIkit.notification ({ - message: " Email and Username are required fields", + message: ' Email and Username are required fields', status: 'danger', pos: 'top-center', - timeout: 2000, - }); + timeout: 2000 + }) } else if (email === '' || username === '') { - var empty = 'Email'; + var empty = 'Email' if (username.length === 0) { - empty = 'Username'; + empty = 'Username' } // eslint-disable-next-line UIkit.notification ({ message: ` ${empty} is required field.`, status: 'danger', pos: 'top-center', - timeout: 1500, - }); + timeout: 1500 + }) } else { // eslint-disable-next-line fetch (`https://api.github.com/users/${username}`) - .then (res => res.json ()) - .then (out => { + .then(res => res.json()) + .then(out => { if (!(out.message && out.message === 'Not Found')) { // eslint-disable-next-line UIkit.notification ({ - message: " Thank You, Check your inbox for the invite.", + message: ' Thank You, Check your inbox for the invite.', status: 'success', pos: 'bottom-center', - timeout: 2000, - }); + timeout: 2000 + }) } - }); + }) } -}; +} diff --git a/server.js b/server.js index 40c0092..585f480 100644 --- a/server.js +++ b/server.js @@ -10,18 +10,23 @@ const createMail = require('./createmail') const urlcrypt = require('url-crypt')( '~{ry*I)44==yU/]9<7DPk!Hj"R#:-/Z7(hTBnlRS=4CXF' ) -const sgMail = require('@sendgrid/mail') +const nodemailer = require('nodemailer') const { glitch, - slack, - webhookURL, token, selfEmail, githubApi } = require('./constants') +const transporter = nodemailer.createTransport({ + service: 'gmail', + auth: { + user: selfEmail, + pass: process.env.gmail_key + } +}) + app.use(bodyParser.json()) -sgMail.setApiKey(process.env.SG_TOKEN) // Auto-update Glitch with GitHub app.post('/git', (req, res) => { @@ -65,7 +70,7 @@ app.get('/', (_req, res) => { // Send the mail to the given email app.get('/sendmail/:username/:id', (req) => { - const { username, id } = req.params; + const { username, id } = req.params const base64 = urlcrypt.cryptObj({ email: id, @@ -74,23 +79,21 @@ app.get('/sendmail/:username/:id', (req) => { const verificationurl = `https://${req.get('host')}/verify/${base64}` - const msg = { + const mailOptions = { from: selfEmail, - bcc: selfEmail, to: id, subject: 'Invitation to join IIITV OSS Team', html: createMail.createMail(username, verificationurl) } - sgMail - .send(msg) - .then(() => { - console.log('Email sent') - }) - .catch((error) => { - console.error("Error", error); - }) -}); + transporter.sendMail(mailOptions) + .then((info) => { + console.log('Email Sent: ' + info.response) + }) + .catch((err) => { + console.error(err) + }) +}) // Verify the email id through the link, and add as member app.get('/verify/:base64', (request, response) => {