Git Product home page Git Product logo

yesod-recaptcha2's Introduction

yesod-recaptcha2

Hackage yesod-recaptcha2 on Stackage LTS test

It support new Google reCAPTCHA(v2, v3) for yesod-form instead yesod-recaptcha beacuse original yesod-recaptcha is dead.

And it support reCAPTCHA new version(v2, v3).

Example

Setup

import Yesod.ReCaptcha2
instance YesodReCaptcha App where
  reCaptchaSiteKey = pure "foo"
  reCaptchaSecretKey = pure "bar"
  reCaptchaLanguage = pure Nothing

Append to applicative form

buildForm :: Form MyForm
buildForm = renderDivs $ MyForm <$>
  areq textField "foo" Nothing <*
  reCaptcha

yesod-recaptcha2's People

Contributors

alebon avatar dependabot[bot] avatar gtgteq avatar ncaq avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

alebon alexkazik

yesod-recaptcha2's Issues

yesod-recaptcha2-0.2.3 build failure with yesod-1.6

As seen on the Stackage build server:

Building library for yesod-recaptcha2-0.2.3..

[1 of 1] Compiling Yesod.ReCaptcha2 ( src/Yesod/ReCaptcha2.hs, dist/build/Yesod/ReCaptcha2.o )

src/Yesod/ReCaptcha2.hs:37:15: error:
    • Variable not in scope:
        liftHandlerT
          :: HandlerFor site0 (FormResult ())
             -> transformers-0.5.2.0:Control.Monad.Trans.RWS.Lazy.RWST
                  (Maybe (Env, FileEnv), site, [Lang])
                  Enctype
                  Ints
                  (HandlerT site IO)
                  (FormResult ())
    • Perhaps you meant ‘liftHandler’ (imported from ClassyPrelude.Yesod)
   |
37 |     result <- liftHandlerT formResult
   |               ^^^^^^^^^^^^

Invisible ReCaptcha

Here is a first draft to add Invisible ReCaptcha:

New methods:

mReCaptchaInvisible :: YesodReCaptcha site => MForm (HandlerFor site) (FormResult ())
mReCaptchaInvisible = fst <$> mReCaptcha

reCaptchaInvisible :: YesodReCaptcha site => AForm (HandlerFor site) ()
reCaptchaInvisible = formToAForm ((,[]) <$> mReCaptchaInvisible)

reCaptchaInvisibleButton :: YesodReCaptcha site => Maybe Text -> HandlerFor site (Text, WidgetFor site (), [(Text, Text)])
reCaptchaInvisibleButton mIdent = do
  mReCaptchaLanguage <- reCaptchaLanguage
  siteKey <- reCaptchaSiteKey
  ident <- maybe newIdent return mIdent
  let
    scriptName = "reCaptchaOnSubmit_" <> ident
  let
    widget = do
      case mReCaptchaLanguage of
        Nothing ->
          addScriptRemote "https://www.google.com/recaptcha/api.js"
        Just hl ->
          addScriptRemote $ "https://www.google.com/recaptcha/api.js?hl=" <> hl
      [whamlet|<script>function #{scriptName} (token) { document.getElementById("#{ident}").submit(); } |]
  return
    ( ident
    , widget
    , [("data-sitekey", siteKey), ("data-callback", scriptName)]
    )

How to use:
where you render the form:
(formIdent, reCaptchaWidget, reCaptchaButtonAttributes) <- reCaptchaInvisibleButton Nothing
You can replace Nothing with Just "id" if the form has already an id, otherwise a new one will be generated.
In the form:

  1. the form itself needs a id: <form ##{formIdent} method=post action=@{route} enctype=#{enctype}>
  2. the button requires a special class and attributes, minimal: <button .g-recaptcha *{reCaptchaButtonAttributes}>
  3. add the widget ^{reCaptchaWidget} (somewhere)

This is the easiest way I could figure out.

The AForm is untested! (the MForm works like a charm.)

Feel free to add it if you like.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.