From 9a6ee1baa2b40624501522512e007d774bc5c5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Mon, 4 Jun 2018 16:21:36 +0200 Subject: [PATCH] Signing up for the API work again - Can use the same email more than once, the only difference is that it wont create a new key, only a new link to the key --- server/public/assets/js/token_apply.js | 2 + server/routing/client/api.js | 71 ++++++++++++++------------ server/routing/client/router.js | 36 ++++++------- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/server/public/assets/js/token_apply.js b/server/public/assets/js/token_apply.js index 4b21eda8..3412e2dd 100644 --- a/server/public/assets/js/token_apply.js +++ b/server/public/assets/js/token_apply.js @@ -24,9 +24,11 @@ window.addEventListener("DOMContentLoaded", function(e) { Helper.toggleClass(".submit", "disabled"); Helper.removeClass(".full-form-token", "hide"); var captcha_response = grecaptcha.getResponse(); + console.log(captcha_response); Helper.ajax({ type: "POST", url: "/api/apply", + headers: {"Content-Type": "application/json;charset=UTF-8"}, data: { origin: origin, email: email, diff --git a/server/routing/client/api.js b/server/routing/client/api.js index a29a6dcb..8b712273 100644 --- a/server/routing/client/api.js +++ b/server/routing/client/api.js @@ -11,6 +11,7 @@ var List = require(pathThumbnails + '/handlers/list.js'); var Functions = require(pathThumbnails + '/handlers/functions.js'); var Frontpage = require(pathThumbnails + '/handlers/frontpage.js'); var Search = require(pathThumbnails + '/handlers/search.js'); +var uniqid = require('uniqid'); var toShowChannel = { start: 1, @@ -1254,42 +1255,10 @@ try { token_db.collection("api_links").find({token: token}, function(e, d) { if(results_find.length == 0 || (d.length == 0 && results_find.length > 0 && !results_find[0].active)) { token_db.collection("api_token").insert({name: name, origin: origin, token: id, usage: 0, active: false, limit: 20}, function(err, docs){ - token_db.collection("api_links").insert({id: uniqid_link, token: id, createdAt: new Date()}, function(err, docs) { - let transporter = nodemailer.createTransport(mailconfig); - - transporter.verify(function(error, success) { - if (error) { - token_db.collection("api_links").remove({id: uniqid_link}, function(e,d) { - res.send("failed"); - return; - }) - } else { - var subject = 'ZOFF: API-key'; - var message = "Link to API-key: https://zoff.me/api/apply/" + uniqid_link + "\n\nThis link expires in 1 day."; - var msg = { - from: mailconfig.from, - to: name, - subject: subject, - text: message, - html: message, - } - transporter.sendMail(msg, (error, info) => { - if (error) { - res.status(400).send("failed"); - transporter.close(); - return; - } - res.status(200).send("success"); - transporter.close(); - return; - }); - } - }); - }) + createApiLink(req, res, uniqid_link, id, name); }); } else { - res.send("failed"); - return; + createApiLink(req, res, uniqid_link, token, name); } }); }) @@ -1299,6 +1268,40 @@ try { } }); + function createApiLink(req, res, uniqid_link, id, name) { + token_db.collection("api_links").insert({id: uniqid_link, token: id, createdAt: new Date()}, function(err, docs) { + let transporter = nodemailer.createTransport(mailconfig); + transporter.verify(function(error, success) { + if (error) { + token_db.collection("api_links").remove({id: uniqid_link}, function(e,d) { + res.send("failed"); + return; + }) + } else { + var subject = 'ZOFF: API-key'; + var message = "Hello,

Thanks for signing up for the API, here is your key: https://zoff.me/api/apply/" + uniqid_link + "

This link will expire in 1 day, so please write it down.

zoff-logo"; + var msg = { + from: mailconfig.from, + to: name, + subject: subject, + text: message, + html: message, + } + transporter.sendMail(msg, (error, info) => { + if (error) { + res.send("failed"); + transporter.close(); + return; + } + res.status(200).send("success"); + transporter.close(); + return; + }); + } + }); + }) + } + router.route('/api/mail').post(recaptcha.middleware.verify, function(req, res) { if(req.recaptcha.error == null) { let transporter = nodemailer.createTransport(mailconfig); diff --git a/server/routing/client/router.js b/server/routing/client/router.js index 67b04666..81bd6529 100644 --- a/server/routing/client/router.js +++ b/server/routing/client/router.js @@ -54,7 +54,23 @@ router.route('/').post(function(req, res, next){ root(req, res, next); }); -router.route('/api/apply/:id').get(function(req,res) { +router.route('/api/apply').get(function(req, res, next) { + var data = { + year: year, + javascript_file: "token.min.js", + captcha: res.recaptcha, + analytics: analytics, + activated: false, + id: "", + correct: false, + stylesheet: "style.css", + embed: false, + og_image: "https://zoff.me/assets/images/small-square.jpg", + } + res.render('layouts/client/token', data); +}); + +router.route('/api/apply/:id').get(function(req, res) { var id = req.params.id; token_db.collection('api_links').find({id: id}, function(err, result) { if(result.length == 1) { @@ -74,7 +90,7 @@ router.route('/api/apply/:id').get(function(req,res) { } res.render('layouts/client/token', data); }); - }) + }); } else { var data = { year: year, @@ -94,22 +110,6 @@ router.route('/api/apply/:id').get(function(req,res) { }); -router.route('/api/apply').get(function(req, res, next) { - var data = { - year: year, - javascript_file: "token.min.js", - captcha: res.recaptcha, - analytics: analytics, - activated: false, - id: "", - correct: false, - stylesheet: "style.css", - embed: false, - og_image: "https://zoff.me/assets/images/small-square.jpg", - } - res.render('layouts/client/token', data); -}); - function root(req, res, next) { try{ var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0];