Skip to content

Instantly share code, notes, and snippets.

@sbosell
Created September 29, 2020 12:28
Show Gist options
  • Save sbosell/a59b514c4cdd9f50ecef0b770e083ca0 to your computer and use it in GitHub Desktop.
Save sbosell/a59b514c4cdd9f50ecef0b770e083ca0 to your computer and use it in GitHub Desktop.
recapthca3 and flutter web
@JS()
library grecaptcha;
import 'package:js/js.dart';
import 'package:js/js_util.dart';
@JS()
@anonymous
class Options {
external String get action;
// Must have an unnamed factory constructor with named arguments.
external factory Options({String action});
}
@JS('grecaptcha.ready')
external captchaReady(Function callback);
@JS("grecaptcha.execute")
external captchaExecute(String key, Options opts);
dynamic doValidation(String key) async {
var promise = captchaExecute(key, Options(action: 'submit'));
var qs = await promiseToFuture(promise);
return qs;
}
RaisedButton(
child: Text('Submit'),
onPressed: () {
doValidation(yourCaptchaKey).then((data) {
var token = data.toString();
if (validateTokenOnServer(token)) {
}
});
})
private bool VerifyCaptchaResponse(string response)
{
var json = "https://www.google.com/recaptcha/api/siteverify"
.AddQueryParam("secret",yoursecret)
.AddQueryParam("response", response).GetJsonFromUrl();
var data =JSON.parse(json).ToObjectDictionary();
return data["success"].ConvertTo<bool>() && data["score"].ConvertTo<float>() > .3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment