user6172032
user6172032

Reputation:

How to verify google captcha

Client side code

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
   var verified = function() {
       document.getElementById("loginform").submit();
   };
</script>    

<form action="www.example.com/" method="POST" id="loginform" onsubmit=" return validation()">
   <input  id="email" maxlength="80" name="email" size="20" type="text"  placeholder="Enter Your Email" style="margin-bottom: 30px;"/><br>
   <div id="captchadiv">
        <div class="g-recaptcha" data-sitekey="site key" data-callback="verified"></div>
   </div>
   <button type="submit" value="Submit" id="reg_submit" style=" display:block;margin: 0 auto;"><img src="/favicon.png" style="width: 20px;float: left;" />Sign in</button>                  
 </form>

Server Side code

reCAPTCHA=require('recaptcha2')

        recaptcha=new reCAPTCHA({
            siteKey:'site key',
            secretKey:'secretKey'
        })

I am working on node js.I am using google recaptcha2 and when i see lots of example and all example verify recaptcha using form submit. They define in action but my action method use in other navigation so i can use get, post request. I don't have any idea for how to use get, post request for recaptcha.I want to verify recaptcha on server side using get,post request.

I need help on back-end verification work. Thanks advance!

Upvotes: 5

Views: 6977

Answers (1)

A.K.
A.K.

Reputation: 2322

Please try this code

Client side code not more secure so please use both side code

Client side

function validateform(){
    var captcha_response = grecaptcha.getResponse();
        if(captcha_response.length == 0 || grecaptcha != undefined )
        {
            // Captcha is not Passed
            return '  Please verify you are not a robot.';
        }else{
            $.get('/captchaTest',{'response':captcha_response},function(response){
                if(response == undefined && response.responseCode == undefined && response.responseDesc == undefined  && response.responseCode !== 0 && response.responseDesc !== 'Sucess' ){
                    return ' You are a robot.';
                }
                grecaptcha.reset();
            });
        }
}

Server side

app.get('/captchaTest',function(req,res){
    var requestQuery = req.query;
    if( requestQuery != undefined && requestQuery != '' && requestQuery != null && requestQuery.response != undefined && requestQuery.response != '' && requestQuery.response != null ){
        var response = requestQuery.response;
            var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret="+ secret_key +"&response=" +response;
            // Hitting GET request to the URL, Google will respond with success or error scenario.
            request(verificationUrl,function(error,response,body) {
            body = JSON.parse(body);
            // Success will be true or false depending upon captcha validation.
                if(body.success !== undefined && !body.success) {
                    res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
                }else{
                    res.send({"responseCode" : 0,"responseDesc" : "Sucess"});
                }
            });
    }else{
        res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
    }
});

Upvotes: 4

Related Questions