-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- inject recaptcha within iframe - use postMessage to sync state between iframe and parent
- Loading branch information
1 parent
e61dbae
commit 0b75e61
Showing
4 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
Component, | ||
ContextType, | ||
} from 'react' | ||
import { | ||
ThemeFrameContext, | ||
} from '@/components/ThemeFrame' | ||
|
||
const CAPTCHA_URL = 'https://www.google.com/recaptcha/api.js' | ||
const PREFIX = `${Date.now()}|` | ||
const PARENT_ORIGIN = window.location.origin | ||
|
||
export interface CaptchaProps { | ||
siteKey: string | ||
size?: 'compact'|'normal', | ||
theme?: 'dark' | 'light', | ||
onSuccess?: () => void | ||
onError?: () => void | ||
onExpired?: () => void | ||
} | ||
|
||
export class Captcha extends Component<CaptchaProps> { | ||
static contextType = ThemeFrameContext | ||
|
||
declare context: ContextType<typeof ThemeFrameContext> | ||
|
||
_initialized: boolean = false | ||
|
||
componentDidMount() { | ||
if (!this._initialized) { | ||
this.initializeFrame() | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('message', this.onMessage) | ||
} | ||
|
||
onMessage = (event: MessageEvent) => { | ||
if (event?.data?.startsWith(`${PREFIX}`)) { | ||
const message = event.data.slice(PREFIX.length) | ||
const data = JSON.parse(message) | ||
switch (data.type) { | ||
case 'captcha-success': | ||
this.props.onSuccess?.() | ||
break | ||
|
||
case 'captcha-error': | ||
this.props.onError?.() | ||
break | ||
|
||
case 'captcha-expired': | ||
this.props.onExpired?.() | ||
break | ||
|
||
default: | ||
break | ||
} | ||
} | ||
} | ||
|
||
initializeFrame() { | ||
const doc = this.context | ||
if (doc === null || this._initialized) { | ||
return | ||
} | ||
|
||
const captchaScript = doc.createElement('script') | ||
captchaScript.src = CAPTCHA_URL | ||
doc.head.appendChild(captchaScript) | ||
|
||
const handlerScript = doc.createElement('script') | ||
handlerScript.innerHTML = ` | ||
window.successCallback = function (token) { | ||
window.parent.postMessage('${PREFIX}' + JSON.stringify({type: "captcha-success", payload: token}), '${PARENT_ORIGIN}') | ||
} | ||
window.expiredCallback = function () { | ||
window.parent.postMessage('${PREFIX}' + JSON.stringify({type: "captcha-expired", payload: null}), '${PARENT_ORIGIN}') | ||
} | ||
window.errorCallback = function () { | ||
window.parent.postMessage('${PREFIX}' + JSON.stringify({type: "captcha-expired", payload: null}), '${PARENT_ORIGIN}') | ||
} | ||
` | ||
doc.head.appendChild(handlerScript) | ||
|
||
window.addEventListener('message', this.onMessage, false) | ||
|
||
this._initialized = true | ||
} | ||
|
||
render() { | ||
this.initializeFrame() | ||
|
||
return ( | ||
<div | ||
className="g-recaptcha" | ||
data-sitekey={this.props.siteKey} | ||
data-theme={this.props.theme} | ||
data-size={this.props.size} | ||
data-callback="successCallback" | ||
data-expired-callback="expiredCallback" | ||
data-error-callback="errorCallback" | ||
/> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,6 @@ export { | |
export { | ||
B3FileUpload, | ||
} from './B3FileUpload' | ||
export { | ||
Captcha, | ||
} from './Captcha' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters