From 725c965c4e73fe787bb8735599a356ab91cda6eb Mon Sep 17 00:00:00 2001 From: Anatoly Kopyl Date: Tue, 23 Mar 2021 18:40:38 +0300 Subject: [PATCH] Checking for existing accounts when registering Packed up js into a file --- public/auth.html | 10 +--------- public/index.js | 7 +++++++ server.js | 17 ++++++++++++----- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/public/auth.html b/public/auth.html index ff00abf..2375851 100644 --- a/public/auth.html +++ b/public/auth.html @@ -25,14 +25,6 @@ - + \ No newline at end of file diff --git a/public/index.js b/public/index.js index e69de29..96a75cb 100644 --- a/public/index.js +++ b/public/index.js @@ -0,0 +1,7 @@ +grecaptcha.ready(function () { + grecaptcha.execute('6LcTXIsaAAAAAGWE4913KuaqU1tTT9uyqmvPADcn', { action: 'demo' }) + .then(function (token) { + document.getElementsByClassName('g-recaptcha-response')[0].value = token; + document.getElementsByClassName('g-recaptcha-response')[1].value = token; + }); +}); \ No newline at end of file diff --git a/server.js b/server.js index 9b5e718..a117876 100644 --- a/server.js +++ b/server.js @@ -49,12 +49,19 @@ app.post('/register', async (req, res) => { verifyCaptcha(req, res, async () => { const hashedPass = await bcrypt.hash(req.body.pass, 10) try { - await client.db('reg_example').collection('users').insertOne({ - login: req.body.login, - pass: hashedPass + const user = await client.db('reg_example').collection('users').findOne({ + login: req.body.login }) - req.session.loggedIn = true - res.status(201).redirect('/') + if (user) { + res.send('A user with this username already exists.') + } else { + await client.db('reg_example').collection('users').insertOne({ + login: req.body.login, + pass: hashedPass + }) + req.session.loggedIn = true + res.status(201).redirect('/') + } } catch (e) { console.log("Error: " + e) res.status(500).send()