Tuesday, 9 July 2019

Google Recaptcha v3 with VUE

https://github.com/AurityLab/recaptcha-v3/#usage-1

import { load } from 'recaptcha-v3'

load('<site key>').then((recaptcha) => {
  recaptcha.execute('<action>').then((token) => {
      console.log(token) // Will print the token
    })
})



PHP sample on backend communication
https://codeforgeek.com/google-recaptcha-v3-tutorial/


Laravel using use GuzzleHttp\Client;
public function send_captacha_verification(<your_client_generated_recpatcha_token>) {
// Guzzle client to send request to google
$client = new Client();
$response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
'secret' => '<your_secrect_key>'
'response' => <your_client_generated_recpatcha_token>,
'remoteip' => $_SERVER['REMOTE_ADDR']
]
]);
if ($response->getStatusCode() != 200) {
return FALSE;
}
// Response is JSON string
$content_response = $response->getBody()->getContents();
if ( empty($content_response) ) {
return FALSE;
}
$content_response_array = json_decode($content_response, TRUE);
if ( empty($content_response_array) ) {
return FALSE;
}
// success is a boolean, so if its empty meaning either value does not exist, or it is false
if ( empty($content_response_array['success']) OR ! isset($content_response_array['score'])) {
return FALSE;
}
// Check score
if ($content_response_array['score'] <= 0.5) {
return FALSE;
}
return TRUE;
}

No comments:

Post a Comment