Skip to content
This repository was archived by the owner on Mar 26, 2023. It is now read-only.

Commit 9be5b08

Browse files
Merge pull request #91 from ForFansubs/dev
reCaptcha for login & register
2 parents a57c028 + 8386c66 commit 9be5b08

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SMTP_USERNAME=
1919
SMTP_PASSWORD=
2020
SMTP_HOST=
2121
SMTP_PORT=
22+
GOOGLE_RECAPTCHA_SECRET_KEY=
2223
PRERENDER_SERVICE_URL=
2324
USE_NEW_SEO_METHOD=
2425
REDIS_OPTIONS={}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ ecosystem.config.js
1818
npm-debug\.log
1919

2020
/config/sitemap.xml
21+
22+
package-lock.json

routes/api/user.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ const sendMail = require("../../methods/mailer").sendMail;
22
const SHA256 = require("crypto-js/sha256");
33
const express = require("express");
44
const router = express.Router();
5+
const axios = require("axios");
56
const gravatar = require("gravatar");
67
const bcrypt = require("bcryptjs");
8+
const qs = require("qs");
79
const jwt = require("jsonwebtoken");
810
const keys = require("../../config/keys");
911
const error_messages = require("../../config/error_messages");
@@ -81,6 +83,19 @@ router.post(
8183
d: "mm", // Default
8284
});
8385

86+
const recaptchaResponse = await axios.post(
87+
"https://www.google.com/recaptcha/api/siteverify",
88+
qs.stringify({
89+
secret: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
90+
response: req.body.recaptcha_response,
91+
remoteip: req.ip,
92+
})
93+
);
94+
95+
if (!recaptchaResponse.data.success) {
96+
return res.status(401).send("reCaptcha problem!");
97+
}
98+
8499
bcrypt.genSalt(10, (err, salt) => {
85100
bcrypt.hash(password, salt, async (err, p_hash) => {
86101
let user_result;
@@ -260,6 +275,19 @@ router.post(
260275
});
261276
}
262277

278+
const recaptchaResponse = await axios.post(
279+
"https://www.google.com/recaptcha/api/siteverify",
280+
qs.stringify({
281+
secret: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
282+
response: req.body.recaptcha_response,
283+
remoteip: req.ip,
284+
})
285+
);
286+
287+
if (!recaptchaResponse.data.success) {
288+
return res.status(401).send("reCaptcha problem!");
289+
}
290+
263291
// Check Password
264292
bcrypt.compare(password, user.password).then((isMatch) => {
265293
if (isMatch) {

validators/user.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const registerUserSchema = Joi.object({
55
email: Joi.string().email().required(),
66
password: Joi.string().required().min(8).max(100),
77
repeat_password: Joi.ref("password"),
8+
recaptcha_response: Joi.string(),
89
});
910

1011
const loginUserSchema = Joi.object({
1112
username: Joi.string().required(),
1213
password: Joi.string().required().min(8).max(100),
14+
recaptcha_response: Joi.string(),
1315
});
1416

1517
module.exports = { registerUserSchema, loginUserSchema };

0 commit comments

Comments
 (0)