Validate recaptcha for registration
This commit is contained in:
parent
5194830e0d
commit
cfc413f782
3 changed files with 22 additions and 4 deletions
|
@ -3,3 +3,4 @@ mysql-python
|
|||
Flask-SQLAlchemy
|
||||
SQLAlchemy
|
||||
gunicorn
|
||||
requests
|
|
@ -4,6 +4,7 @@ from flask import current_app as app
|
|||
from models import db, Users
|
||||
from utils import api_wrapper
|
||||
|
||||
import requests
|
||||
import utils
|
||||
|
||||
blueprint = Blueprint("user", __name__)
|
||||
|
@ -11,6 +12,9 @@ blueprint = Blueprint("user", __name__)
|
|||
@blueprint.route("/register", methods=["POST"])
|
||||
@api_wrapper
|
||||
def user_register():
|
||||
if not validate_captcha(request.form):
|
||||
return { "success": 0, "message": "Please do the captcha." }
|
||||
|
||||
name = request.form["name"]
|
||||
username = request.form["username"]
|
||||
password = request.form["password"]
|
||||
|
@ -59,3 +63,13 @@ def add_user(name, username, email, password):
|
|||
user = Users(name, username, email, password)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
def validate_captcha(form):
|
||||
if "captcha_response" not in form:
|
||||
return False
|
||||
captcha_response = form["captcha_response"]
|
||||
data = {"secret": "6Lc4xhMTAAAAACFaG2NyuKoMdZQtSa_1LI76BCEu", "response": captcha_response}
|
||||
response = requests.post("https://www.google.com/recaptcha/api/siteverify", data=data)
|
||||
if response.json()["success"]:
|
||||
return True
|
||||
return False
|
||||
|
|
|
@ -1,19 +1,22 @@
|
|||
$("#registration-form").on("submit", function(e) {
|
||||
e.preventDefault();
|
||||
register($("#name").val(), $("#username").val(), $("#password").val(), $("#password_confirm").val(), $("#email").val());
|
||||
register($("#name").val(), $("#username").val(), $("#password").val(), $("#password_confirm").val(), $("#email").val(), $("#g-recaptcha-response").val());
|
||||
});
|
||||
|
||||
function register(name, username, password, password_confirm, email) {
|
||||
function register(name, username, password, password_confirm, email, captcha_response) {
|
||||
$.post("/api/user/register", {
|
||||
name: name,
|
||||
username: username,
|
||||
password: password,
|
||||
password_confirm: password_confirm,
|
||||
email: email
|
||||
email: email,
|
||||
captcha_response: captcha_response
|
||||
}, function(data) {
|
||||
$("#status").text(data.message);
|
||||
if (data.success == 1) {
|
||||
// wait then redirect or whatever
|
||||
} else {
|
||||
grecaptcha.reset();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue