wwf
2025-05-20 938c3e5a587ce950a94964ea509b9e7f8834dfae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use client'
import { useRouter, useSearchParams } from 'next/navigation'
import type { FC } from 'react'
import React, { useEffect } from 'react'
import cn from '@/utils/classnames'
import Toast from '@/app/components/base/toast'
import { fetchSystemFeatures, fetchWebOAuth2SSOUrl, fetchWebOIDCSSOUrl, fetchWebSAMLSSOUrl } from '@/service/share'
import { setAccessToken } from '@/app/components/share/utils'
import Loading from '@/app/components/base/loading'
 
const WebSSOForm: FC = () => {
  const searchParams = useSearchParams()
  const router = useRouter()
 
  const redirectUrl = searchParams.get('redirect_url')
  const tokenFromUrl = searchParams.get('web_sso_token')
  const message = searchParams.get('message')
 
  const showErrorToast = (message: string) => {
    Toast.notify({
      type: 'error',
      message,
    })
  }
 
  const getAppCodeFromRedirectUrl = () => {
    const appCode = redirectUrl?.split('/').pop()
    if (!appCode)
      return null
 
    return appCode
  }
 
  const processTokenAndRedirect = async () => {
    const appCode = getAppCodeFromRedirectUrl()
    if (!appCode || !tokenFromUrl || !redirectUrl) {
      showErrorToast('redirect url or app code or token is invalid.')
      return
    }
 
    await setAccessToken(appCode, tokenFromUrl)
    router.push(redirectUrl)
  }
 
  const handleSSOLogin = async (protocol: string) => {
    const appCode = getAppCodeFromRedirectUrl()
    if (!appCode || !redirectUrl) {
      showErrorToast('redirect url or app code is invalid.')
      return
    }
 
    switch (protocol) {
      case 'saml': {
        const samlRes = await fetchWebSAMLSSOUrl(appCode, redirectUrl)
        router.push(samlRes.url)
        break
      }
      case 'oidc': {
        const oidcRes = await fetchWebOIDCSSOUrl(appCode, redirectUrl)
        router.push(oidcRes.url)
        break
      }
      case 'oauth2': {
        const oauth2Res = await fetchWebOAuth2SSOUrl(appCode, redirectUrl)
        router.push(oauth2Res.url)
        break
      }
      default:
        showErrorToast('SSO protocol is not supported.')
    }
  }
 
  useEffect(() => {
    const init = async () => {
      const res = await fetchSystemFeatures()
      const protocol = res.sso_enforced_for_web_protocol
 
      if (message) {
        showErrorToast(message)
        return
      }
 
      if (!tokenFromUrl) {
        await handleSSOLogin(protocol)
        return
      }
 
      await processTokenAndRedirect()
    }
 
    init()
  }, [message, tokenFromUrl]) // Added dependencies to useEffect
 
  return (
    <div className="flex h-full items-center justify-center">
      <div className={cn('flex w-full grow flex-col items-center justify-center', 'px-6', 'md:px-[108px]')}>
        <Loading type='area' />
      </div>
    </div>
  )
}
 
export default React.memo(WebSSOForm)