mirror of
https://github.com/anatolykopyl/registration.git
synced 2026-03-26 04:45:25 +00:00
Checking for existing accounts when registering
Packed up js into a file
This commit is contained in:
@@ -25,14 +25,6 @@
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
<script src="https://www.google.com/recaptcha/api.js?render=6LcTXIsaAAAAAGWE4913KuaqU1tTT9uyqmvPADcn"></script>
|
<script src="https://www.google.com/recaptcha/api.js?render=6LcTXIsaAAAAAGWE4913KuaqU1tTT9uyqmvPADcn"></script>
|
||||||
<script>
|
<script src="index.js"></script>
|
||||||
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;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -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;
|
||||||
|
});
|
||||||
|
});
|
||||||
17
server.js
17
server.js
@@ -49,12 +49,19 @@ app.post('/register', async (req, res) => {
|
|||||||
verifyCaptcha(req, res, async () => {
|
verifyCaptcha(req, res, async () => {
|
||||||
const hashedPass = await bcrypt.hash(req.body.pass, 10)
|
const hashedPass = await bcrypt.hash(req.body.pass, 10)
|
||||||
try {
|
try {
|
||||||
await client.db('reg_example').collection('users').insertOne({
|
const user = await client.db('reg_example').collection('users').findOne({
|
||||||
login: req.body.login,
|
login: req.body.login
|
||||||
pass: hashedPass
|
|
||||||
})
|
})
|
||||||
req.session.loggedIn = true
|
if (user) {
|
||||||
res.status(201).redirect('/')
|
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) {
|
} catch (e) {
|
||||||
console.log("Error: " + e)
|
console.log("Error: " + e)
|
||||||
res.status(500).send()
|
res.status(500).send()
|
||||||
|
|||||||
Reference in New Issue
Block a user