Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

hcaptcha flutter

during one of the projects i was working on, i found out that there aren’t any “solid enough” captcha implementations in flutter.

after following [https://medium.com/@hcaptcha/implementing-hcaptcha-in-your-flutter-app-13ea6ddca71b](this official guide)
on https://www.hcaptcha.com/, i decided to make an “all-in-one” solution, that takes care
of the state and widgets needed to get hcaptcha working with your flutter project.

this repository is part of the hacktoberfest 2021 challenge! if it seems kind of empty, it’s because it’s intentionally left this way.
take a look at the issues tab for stuff that needs doing, and earn yourself a free t-shirt along the way!

features:

  • uses flutter bloc to manage the state.
  • provides a class for adding any of your data (eg form data) when solving the captcha.

captcha html page creation:

for this package to work, we assume that the app you’re developing client-server based, meaning that
there’s a backend to support it.

before you take care of the package installation and implementation, you’ll need to add a simple html
page to your server (don’t forget to add your secret key!!!):

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>captcha challenge</title>
    </head>

    <body>
        <div style="display: flex; justify-content: center;">
            <form method="post">
                <div class="h-captcha" data-sitekey="your_secret_goes_here" data-callback="captchacallback"></div>
                <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
            </form>
        </div>

        <script>
            function captchacallback(response) {
                if (typeof captcha!=="undefined") {
                    captcha.postmessage(response);
                }
            }
        </script>
    </body>
</html>

this page takes care of serving your captcha to your mobile device. the captcha thing inside the
<script> is where the magic happens. this is a javascript channel
and its job is to pass the data back to the flutter app, using the integrated webview
(which we’ll set up in the next step).

in this case, we’ll assume that the url pointing to this page is myapp.kgiannakis.me/captcha-challenge.

installation:

note: for now and until most of the scaffolding work of the project is complete, in terms of documentation and such, the project will only be published here and not on pub.dev.

  1. download this plugin to a directory of your choice.
  2. include it in your pubspec file:
dependencies:
  ...
  hcaptcha_flutter:
    path: ../hcaptcha_flutter
  1. create a hcaptchaprovider:
hcaptchaprovider(
  captchaurl: "https://myapp.kgiannakis.me/captcha-challenge",
  onprecaptcha: const someform(),
  oncaptcha: const captchascreen(),
  oncaptchacompleted: (result, cbloc, data) {
    // this is the part where you pass the result to the server for verification
    final username = data.getdata(key: "username");
    log(result);
    log(username);
  })

this provides the scaffolding work of the captcha process. it injects a captchabloc to the widget
tree along with a captchadatacontainer. think of the data container as a method to temporarily store
stuff when dealing with a captcha challenge.

  1. inside your form, add the captcha logic when submitting:
if (_formkey.currentstate!.validate()) {
  // add the username to the data container.
  var datacontainer = captchautils.getdatacontainer(context);
  datacontainer.adddata(
      key: "username", datain: _usernamecontroller.value.text);

  // this advances the user to the second step (the challenge)
  captchautils.gotocaptcha(context);
}
  1. finally, add the hcaptcha widget itself:
class captchascreen extends statelesswidget {
  const captchascreen({key? key}) : super(key: key);

  @override
  widget build(buildcontext context) {
    return constrainedbox(
      constraints: const boxconstraints(minheight: 450),
      child: hcaptcha(),
    );
  }
}
  1. when the challenge has been completed by the user, the callback in step 3 (oncaptchacompleted)
    will be called. you can then pass on the result to your backend for verification.

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.