11import { Button } from "@/components/ui/button" ;
22import { useKcClsx } from "@keycloakify/login-ui/useKcClsx" ;
3- import { useEffect , useState , type FormEvent } from "react" ;
3+ import { useState } from "react" ;
44import { assert } from "tsafe/assert" ;
55import { useKcContext } from "../../KcContext" ;
66import { UserProfileFormFields } from "../../components/UserProfileFormFields" ;
@@ -16,64 +16,12 @@ export function Form() {
1616 const [ isFormSubmittable , setIsFormSubmittable ] = useState ( false ) ;
1717 const [ areTermsAccepted , setAreTermsAccepted ] = useState ( false ) ;
1818
19- const recaptchaRequired = kcContext . recaptchaRequired ;
20- const recaptchaSiteKey = kcContext . recaptchaSiteKey ;
21- const recaptchaAction = kcContext . recaptchaAction ;
22-
23- // reCAPTCHA v3 is invisible: load the API with the site key on mount, then on
24- // submit fetch a token for the action and POST it in the `g-recaptcha-response`
25- // field (the param Keycloak's recaptcha authenticator reads). No widget.
26- useEffect ( ( ) => {
27- if ( ! recaptchaRequired || recaptchaSiteKey === undefined ) {
28- return ;
29- }
30- const src = `https://www.google.com/recaptcha/api.js?render=${ recaptchaSiteKey } ` ;
31- if ( document . querySelector ( `script[src="${ src } "]` ) !== null ) {
32- return ;
33- }
34- const script = document . createElement ( "script" ) ;
35- script . src = src ;
36- script . async = true ;
37- document . head . appendChild ( script ) ;
38- } , [ recaptchaRequired , recaptchaSiteKey ] ) ;
39-
40- const onSubmit = ( event : FormEvent < HTMLFormElement > ) => {
41- if ( ! recaptchaRequired || recaptchaSiteKey === undefined ) {
42- return ; // no captcha -> let the form submit normally
43- }
44- event . preventDefault ( ) ;
45- const form = event . currentTarget ;
46- const grecaptcha = ( window as any ) . grecaptcha ;
47- if ( grecaptcha === undefined ) {
48- form . submit ( ) ;
49- return ;
50- }
51- grecaptcha . ready ( ( ) => {
52- grecaptcha
53- . execute ( recaptchaSiteKey , { action : recaptchaAction ?? "register" } )
54- . then ( ( token : string ) => {
55- let field = form . querySelector < HTMLInputElement > (
56- 'input[name="g-recaptcha-response"]'
57- ) ;
58- if ( field === null ) {
59- field = document . createElement ( "input" ) ;
60- field . type = "hidden" ;
61- field . name = "g-recaptcha-response" ;
62- form . appendChild ( field ) ;
63- }
64- field . value = token ;
65- form . submit ( ) ;
66- } ) ;
67- } ) ;
68- } ;
69-
7019 return (
7120 < form
7221 id = "kc-register-form"
7322 action = { kcContext . url . registrationAction }
7423 className = "space-y-4"
7524 method = "post"
76- onSubmit = { onSubmit }
7725 >
7826 < UserProfileFormFields
7927 onIsFormSubmittableValueChange = { setIsFormSubmittable }
0 commit comments