11import { ArrowRight } from 'lucide-react'
2+ import { useEffect , useLayoutEffect , useRef } from 'react'
23import { Button } from '@helpwave/hightide'
34import type { KcContext } from '../KcContext'
45import { useI18n } from '../i18n'
56import Template from 'keycloakify/login/Template'
67import { PageLayout } from '../components/PageLayout'
7- import { useEffect } from 'react'
88import { useTranslation } from '../../i18n/useTranslation'
99import { getPageTitleKey } from '../utils/pageTitles'
1010
11+ const FORM_ID = 'kc-saml-post-form'
12+
1113type SamlPostFormProps = {
1214 kcContext : Extract < KcContext , { pageId : 'saml-post-form.ftl' } > ,
1315} ;
1416
17+ function submitForm ( ) : boolean {
18+ const form = document . getElementById ( FORM_ID ) as HTMLFormElement | null
19+ if ( form ) {
20+ form . submit ( )
21+ return true
22+ }
23+ return false
24+ }
25+
1526export default function SamlPostForm ( { kcContext } : SamlPostFormProps ) {
1627 const { i18n } = useI18n ( { kcContext } )
1728 const t = useTranslation ( )
29+ const submittedRef = useRef ( false )
1830
19- useEffect ( ( ) => {
20- const form = document . getElementById ( 'kc-saml-post-form' ) as HTMLFormElement
21- if ( form && kcContext . url . loginAction ) {
22- form . submit ( )
31+ const actionUrl = kcContext . samlPost ?. url ?? kcContext . url . loginAction
32+
33+ useLayoutEffect ( ( ) => {
34+ if ( submittedRef . current || ! actionUrl ) return
35+ if ( submitForm ( ) ) {
36+ submittedRef . current = true
2337 }
24- } , [ kcContext . url . loginAction ] )
38+ } , [ actionUrl ] )
39+
40+ useEffect ( ( ) => {
41+ if ( submittedRef . current || ! actionUrl ) return
42+ const id = setTimeout ( ( ) => {
43+ if ( submittedRef . current ) return
44+ if ( submitForm ( ) ) {
45+ submittedRef . current = true
46+ }
47+ } , 100 )
48+ return ( ) => clearTimeout ( id )
49+ } , [ actionUrl ] )
2550
2651 return (
2752 < Template
@@ -37,8 +62,8 @@ export default function SamlPostForm({ kcContext }: SamlPostFormProps) {
3762 < p > { t ( 'samlPostFormMessage' ) } </ p >
3863
3964 < form
40- id = "kc-saml-post-form"
41- action = { kcContext . samlPost ?. url ?? kcContext . url . loginAction }
65+ id = { FORM_ID }
66+ action = { actionUrl }
4267 method = "POST"
4368 style = { { display : 'none' } }
4469 >
@@ -54,11 +79,16 @@ export default function SamlPostForm({ kcContext }: SamlPostFormProps) {
5479 />
5580 )
5681 ) }
57- < Button type = "submit" color = "primary" >
58- < ArrowRight className = "w-4 h-4" />
59- { t ( 'doContinue' ) }
60- </ Button >
6182 </ form >
83+
84+ < Button
85+ type = "button"
86+ color = "primary"
87+ onClick = { ( ) => submitForm ( ) }
88+ >
89+ < ArrowRight className = "w-4 h-4" />
90+ { t ( 'doContinue' ) }
91+ </ Button >
6292 </ div >
6393 </ PageLayout >
6494 </ Template >
0 commit comments