B4J Tutorial [Server] Login System & Filters Tutorial

Discussion in 'B4J Tutorials' started by Erel, Mar 19, 2014.

  1. moster67

    moster67 Expert Licensed User

    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:
    Code:
    <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:
    Code:
    <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:
    Code:
    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:
    Code:
    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: Dec 18, 2017
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice