0

I am developing locally - laravel sail / docker environment - and access my site with http://localhost. Trying to test hCaptcha. Cannot do so locally. It has been suggested to change my /etc/hosts file:

# cat hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.21.0.7  2d23274c42bb
# 

changing the 'localhost' in the hosts file to 'test.anydomain.com', and then I can test the captcha services.

If I do this, will i be able to access my local test site by using the url 'test.anydomain.com', that is with some arbitrary web site domain name? Is there a better way to accomplish this? Can you just change the APP_URL in the Laravel .env file? Or would the container be able to pick up on the env('APP_URL') variable value?

When I try to edit the /etc/hosts file, I find that the docker container does not have a text editor like Vim or Nano installed. I suppose I could use [sed] command to change the text from local host to a domain name, but I don't want to mess up my container. (I don't get the ins and outs of docker containers yet).

when trying to change the text using sed, I get the following:

sed -i 's/127.0.0.1 localhost/127.0.0.1 test.rec4life.com/g' /etc/hosts
sed: cannot rename /etc/sedIABEfa: Device or resource busy

On a somewhat related note, I have tried to use Beyond Code's expose.dev service where I could share my local machine via the internet. After initiating the expose service and changing the APP_URL to the shared with expose url. I could share the home page, but if you clicked on a link within the home page, you were sent back to plain old local host.

Not sure if the two (hCaptcha and shared with expose) are related problems.

Hoping the answer is out there.

  • I know this is for clarifying issues. But I just wanted to add that I went with mews as the captcha option. pretty simple to implement. So if you choose to answer, thank you so much. would be interested in knowing. but for now, I am going with mews. https://www.positronx.io/laravel-captcha-tutorial-example/ – Robert Bryan Davis Sep 03 '22 at 16:06
  • good gracious. I have tried to integrate mews, hcaptcha, google captcha, with laravel & livewire, and am failing on all three efforts. the google captcha is not even showing up, only the erms and privacy icon in bottom left corner. the mews captcha is changing after every field is updated, whether lazy, or deounced and when i try to save the data, the validation always failes even though the text i type looks correctly, and i cannot get the hcaptcha to function locally, that is on local host, as i have been unable to change the host file, i think because of user rights (that is seen above) – Robert Bryan Davis Sep 04 '22 at 15:40
  • do any of you 17 viewers thus far have any thoughts? Or am i just a hopelessly ignorant programmer! I am going to get in a kayak right now to get away from this! – Robert Bryan Davis Sep 04 '22 at 15:41

1 Answers1

0

I've done this with Google ReCAPTCHA and it works seemlessly. We are using Interia and ReactJS but it should work similar so that you can use your Sail development environment to test it and then deploy it to your production server.

1. Install google recaptcha using Compose into your projects vendor directory.

2. In your reCAPTCHA domains setting, ensure you specify:
   localhost
   127.0.0.1
   DockerIP (e.g. 172.18.0.1)
   your.domain.com

3. In your app, make sure you pass the site keys properly 
   ideally set in the .env file to the google javascript call here is an example:

   app.blade.php
   <script src="https://www.google.com/recaptcha/api.js?render=sitekey" defer></script>

   grecaptcha.ready(function() {
   grecaptcha.execute('xxxxxxxxxsitekeyxxxxx', {action: 'myaction'}).then(function(token) {.........
   
 4. In your controller, verify like this:

        $recaptcha = new \ReCaptcha\ReCaptcha('secretkey');
        $resp = $recaptcha->setExpectedAction('summaryquery')
                          ->setScoreThreshold(0.5)
                          ->verify($data['g-recaptcha-response'],  $this->getIp());
        //error_log(print_r($resp,true));
        if ($resp->isSuccess()) {
            // Verified!
            .........
Rob Mascaro
  • 791
  • 7
  • 16