Skip to content

Commit

Permalink
chore: Use reacaptcha package
Browse files Browse the repository at this point in the history
  • Loading branch information
kevee committed Sep 11, 2020
1 parent c4bfa3f commit 3e5acda
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
10 changes: 10 additions & 0 deletions gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ exports.onRenderBody = ({ setPostBodyComponents, setBodyAttributes }) => {
setBodyAttributes({
className: 'no-js',
})

setPostBodyComponents([
<script
type="text/javascript"
key="recaptcha-script"
async
defer
src="https://www.google.com/recaptcha/api.js"
/>,
])
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"react-dom": "^16.12.0",
"react-expand-animated": "^1.0.2",
"react-helmet": "^5.2.1",
"react-recaptcha": "^2.3.10",
"react-typography": "^0.16.19",
"slugify": "^1.4.0",
"smartypants": "^0.1.1"
Expand Down
30 changes: 9 additions & 21 deletions src/components/common/recaptcha.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useRef } from 'react'
import React from 'react'
import { useStaticQuery, graphql } from 'gatsby'
import Helmet from 'react-helmet'
import ReCaptcha from 'react-recaptcha'

const Recaptcha = () => {
const data = useStaticQuery(graphql`
Expand All @@ -13,27 +13,15 @@ const Recaptcha = () => {
}
`)

const captchaRef = useRef(false)

useEffect(() => {
if (typeof window !== 'undefined')
window.recaptchaOnload = () => {
window.grecaptcha.render(captchaRef.current, {
siteKey: data.site.siteMetadata.recaptchaKey,
})
}
}, [])

return (
<>
<Helmet>
<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=recaptchaOnload" />
</Helmet>
<div
className="g-recaptcha"
ref={captchaRef}
data-sitekey={data.site.siteMetadata.recaptchaKey}
/>
{typeof window !== 'undefined' && (
<ReCaptcha
sitekey={data.site.siteMetadata.recaptchaKey}
render="explicit"
elementID={`captcha-${Math.round(Math.random() * 1000)}`}
/>
)}
</>
)
}
Expand Down

0 comments on commit 3e5acda

Please sign in to comment.