B4J Tutorial [Server] Login System & Filters Tutorial

moster67

Expert
Licensed User
can you change the code and add the new ReCaptcha
Thought I would add an example of using the reCAPTCHA V.2 here since it is related to the sample-app posted by Erel. The old reCAPTCHA did not work for me.

Below procedure for implementing reCAPTCHA V.2 worked for me:

upload_2017-12-18_22-32-56.png


1) first sign up for an api-key for your site (selecting reCAPTCHA V2)
http://www.google.com/recaptcha/admin

2) Modify the index.html file in the login_example directory by removing the old lines referring to reCAPTCHA V.1. Then paste this snippet before the closing </head> tag on your HTML template:
B4X:
<script src='https://www.google.com/recaptcha/api.js'></script>
In the same file, paste this snippet at the end of the <form> where you want the reCAPTCHA widget to appear:
B4X:
<div class="g-recaptcha" data-sitekey="Your Site api-key"></div>
3) In the settings.txt file, add your secret api-key: keyRecaptchaPrivateKey=secret api-key

In the B4J ServerExample project, open the RegisterHelper Class module and modify the Handle and JobDone subs as follows:

Handle:
B4X:
Dim sb As StringBuilder
            sb.Initialize
            sb.Append("secret=").Append(Main.settings.Get("RecaptchaPrivateKey"))
            sb.Append("&response=").Append(req.GetParameter("g-recaptcha-response"))
            j.PostString("https://www.google.com/recaptcha/api/siteverify", sb.ToString)
            StartMessageLoop
JobDone:
B4X:
Sub JobDone(j As HttpJob)
    If j.success Then
        Dim lines() As String = Regex.Split(CRLF, j.GetString)
        If lines.Length > 0 Then
            If lines(1).Contains("true") Then
                success = True
            Else
                success = False
                If lines.Length > 1 Then
                    errorMessage = "Some kind of error"
                End If
            End If
        End If
    Else
        success = False
        errorMessage = j.errorMessage
    End If
    j.Release
    StopMessageLoop
End Sub
That's it. It should now work with reCAPTCHA V2.
I didn't bother to change anything else which could take advantage of new features of B4X. I leave that to you.
 
Last edited:
Top