|
|
|
@ -1,7 +1,7 @@
|
|
|
|
'use client'
|
|
|
|
'use client'
|
|
|
|
import { useRouter, useSearchParams } from 'next/navigation'
|
|
|
|
import { useRouter, useSearchParams } from 'next/navigation'
|
|
|
|
import type { FC } from 'react'
|
|
|
|
import type { FC } from 'react'
|
|
|
|
import React, { useEffect } from 'react'
|
|
|
|
import React, { useCallback, useEffect } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import cn from '@/utils/classnames'
|
|
|
|
import cn from '@/utils/classnames'
|
|
|
|
import Toast from '@/app/components/base/toast'
|
|
|
|
import Toast from '@/app/components/base/toast'
|
|
|
|
@ -9,6 +9,7 @@ import { fetchWebOAuth2SSOUrl, fetchWebOIDCSSOUrl, fetchWebSAMLSSOUrl } from '@/
|
|
|
|
import { setAccessToken } from '@/app/components/share/utils'
|
|
|
|
import { setAccessToken } from '@/app/components/share/utils'
|
|
|
|
import Button from '@/app/components/base/button'
|
|
|
|
import Button from '@/app/components/base/button'
|
|
|
|
import { useGlobalPublicStore } from '@/context/global-public-context'
|
|
|
|
import { useGlobalPublicStore } from '@/context/global-public-context'
|
|
|
|
|
|
|
|
import { SSOProtocol } from '@/types/feature'
|
|
|
|
|
|
|
|
|
|
|
|
const WebSSOForm: FC = () => {
|
|
|
|
const WebSSOForm: FC = () => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { t } = useTranslation()
|
|
|
|
@ -27,15 +28,15 @@ const WebSSOForm: FC = () => {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const getAppCodeFromRedirectUrl = () => {
|
|
|
|
const getAppCodeFromRedirectUrl = useCallback(() => {
|
|
|
|
const appCode = redirectUrl?.split('/').pop()
|
|
|
|
const appCode = redirectUrl?.split('/').pop()
|
|
|
|
if (!appCode)
|
|
|
|
if (!appCode)
|
|
|
|
return null
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
|
|
return appCode
|
|
|
|
return appCode
|
|
|
|
}
|
|
|
|
}, [redirectUrl])
|
|
|
|
|
|
|
|
|
|
|
|
const processTokenAndRedirect = async () => {
|
|
|
|
const processTokenAndRedirect = useCallback(async () => {
|
|
|
|
const appCode = getAppCodeFromRedirectUrl()
|
|
|
|
const appCode = getAppCodeFromRedirectUrl()
|
|
|
|
if (!appCode || !tokenFromUrl || !redirectUrl) {
|
|
|
|
if (!appCode || !tokenFromUrl || !redirectUrl) {
|
|
|
|
showErrorToast('redirect url or app code or token is invalid.')
|
|
|
|
showErrorToast('redirect url or app code or token is invalid.')
|
|
|
|
@ -44,7 +45,7 @@ const WebSSOForm: FC = () => {
|
|
|
|
|
|
|
|
|
|
|
|
await setAccessToken(appCode, tokenFromUrl)
|
|
|
|
await setAccessToken(appCode, tokenFromUrl)
|
|
|
|
router.push(redirectUrl)
|
|
|
|
router.push(redirectUrl)
|
|
|
|
}
|
|
|
|
}, [getAppCodeFromRedirectUrl, redirectUrl, router, tokenFromUrl])
|
|
|
|
|
|
|
|
|
|
|
|
const handleSSOLogin = async () => {
|
|
|
|
const handleSSOLogin = async () => {
|
|
|
|
const appCode = getAppCodeFromRedirectUrl()
|
|
|
|
const appCode = getAppCodeFromRedirectUrl()
|
|
|
|
@ -53,18 +54,18 @@ const WebSSOForm: FC = () => {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (systemFeatures.sso_enforced_for_web_protocol) {
|
|
|
|
switch (systemFeatures.webapp_auth.sso_config.protocol) {
|
|
|
|
case 'saml': {
|
|
|
|
case SSOProtocol.SAML: {
|
|
|
|
const samlRes = await fetchWebSAMLSSOUrl(appCode, redirectUrl)
|
|
|
|
const samlRes = await fetchWebSAMLSSOUrl(appCode, redirectUrl)
|
|
|
|
router.push(samlRes.url)
|
|
|
|
router.push(samlRes.url)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'oidc': {
|
|
|
|
case SSOProtocol.OIDC: {
|
|
|
|
const oidcRes = await fetchWebOIDCSSOUrl(appCode, redirectUrl)
|
|
|
|
const oidcRes = await fetchWebOIDCSSOUrl(appCode, redirectUrl)
|
|
|
|
router.push(oidcRes.url)
|
|
|
|
router.push(oidcRes.url)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'oauth2': {
|
|
|
|
case SSOProtocol.OAuth2: {
|
|
|
|
const oauth2Res = await fetchWebOAuth2SSOUrl(appCode, redirectUrl)
|
|
|
|
const oauth2Res = await fetchWebOAuth2SSOUrl(appCode, redirectUrl)
|
|
|
|
router.push(oauth2Res.url)
|
|
|
|
router.push(oauth2Res.url)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
@ -74,6 +75,14 @@ const WebSSOForm: FC = () => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const goWebApp = () => {
|
|
|
|
|
|
|
|
if (!redirectUrl) {
|
|
|
|
|
|
|
|
showErrorToast('redirect url is invalid.')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
router.push(redirectUrl)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
const init = async () => {
|
|
|
|
const init = async () => {
|
|
|
|
if (message) {
|
|
|
|
if (message) {
|
|
|
|
@ -88,8 +97,8 @@ const WebSSOForm: FC = () => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
init()
|
|
|
|
init()
|
|
|
|
}, [message, tokenFromUrl]) // Added dependencies to useEffect
|
|
|
|
}, [message, processTokenAndRedirect, tokenFromUrl])
|
|
|
|
|
|
|
|
if (systemFeatures.webapp_auth.enable) {
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div className="flex items-center justify-center h-full">
|
|
|
|
<div className="flex items-center justify-center h-full">
|
|
|
|
<div className={cn('flex flex-col items-center w-full grow justify-center', 'px-6', 'md:px-[108px]')}>
|
|
|
|
<div className={cn('flex flex-col items-center w-full grow justify-center', 'px-6', 'md:px-[108px]')}>
|
|
|
|
@ -97,6 +106,12 @@ const WebSSOForm: FC = () => {
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
return <div className="flex items-center justify-center h-full">
|
|
|
|
|
|
|
|
<p>Current App is not required for login, you can <span className='text-text-accent cursor-pointer' onClick={goWebApp}>click here</span> continue.</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default React.memo(WebSSOForm)
|
|
|
|
export default React.memo(WebSSOForm)
|
|
|
|
|