Skip to content

Commit 3c7f821

Browse files
committed
fix saml post form redirect
1 parent 923fc71 commit 3c7f821

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "id.helpwave.de",
3-
"version": "0.1.10",
3+
"version": "0.1.11",
44
"repository": {
55
"type": "git",
66
"url": "git://github.com/helpwave/id.helpwave.de.git"

src/login/pages/SamlPostForm.tsx

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,52 @@
11
import { ArrowRight } from 'lucide-react'
2+
import { useEffect, useLayoutEffect, useRef } from 'react'
23
import { Button } from '@helpwave/hightide'
34
import type { KcContext } from '../KcContext'
45
import { useI18n } from '../i18n'
56
import Template from 'keycloakify/login/Template'
67
import { PageLayout } from '../components/PageLayout'
7-
import { useEffect } from 'react'
88
import { useTranslation } from '../../i18n/useTranslation'
99
import { getPageTitleKey } from '../utils/pageTitles'
1010

11+
const FORM_ID = 'kc-saml-post-form'
12+
1113
type 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+
1526
export 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

Comments
 (0)