'use client' import cn from 'classnames' import { useRouter, useSearchParams } from 'next/navigation' import type { FC } from 'react' import React, { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Toast from '@/app/components/base/toast' import Button from '@/app/components/base/button' import { fetchSystemFeatures, fetchWebOAuth2SSOUrl, fetchWebOIDCSSOUrl, fetchWebSAMLSSOUrl } from '@/service/share' import LogoSite from '@/app/components/base/logo/logo-site' import { setAccessToken } from '@/app/components/share/utils' const WebSSOForm: FC = () => { const searchParams = useSearchParams() const redirectUrl = searchParams.get('redirect_url') const tokenFromUrl = searchParams.get('web_sso_token') const message = searchParams.get('message') const router = useRouter() const { t } = useTranslation() const [isLoading, setIsLoading] = useState(false) const [protocal, setProtocal] = useState('') useEffect(() => { const fetchFeaturesAndSetToken = async () => { await fetchSystemFeatures().then((res) => { setProtocal(res.sso_enforced_for_web_protocol) }) // Callback from SSO, process token and redirect if (tokenFromUrl && redirectUrl) { const appCode = redirectUrl.split('/').pop() if (!appCode) { Toast.notify({ type: 'error', message: 'redirect url is invalid. App code is not found.', }) return } await setAccessToken(appCode, tokenFromUrl) router.push(redirectUrl) } } fetchFeaturesAndSetToken() if (message) { Toast.notify({ type: 'error', message, }) } }, []) const handleSSOLogin = () => { setIsLoading(true) if (!redirectUrl) { Toast.notify({ type: 'error', message: 'redirect url is not found.', }) setIsLoading(false) return } const appCode = redirectUrl.split('/').pop() if (!appCode) { Toast.notify({ type: 'error', message: 'redirect url is invalid. App code is not found.', }) return } if (protocal === 'saml') { fetchWebSAMLSSOUrl(appCode, redirectUrl).then((res) => { router.push(res.url) }).finally(() => { setIsLoading(false) }) } else if (protocal === 'oidc') { fetchWebOIDCSSOUrl(appCode, redirectUrl).then((res) => { router.push(res.url) }).finally(() => { setIsLoading(false) }) } else if (protocal === 'oauth2') { fetchWebOAuth2SSOUrl(appCode, redirectUrl).then((res) => { router.push(res.url) }).finally(() => { setIsLoading(false) }) } else { Toast.notify({ type: 'error', message: 'sso protocal is not supported.', }) setIsLoading(false) } } return (

{t('login.pageTitle')}

) } export default React.memo(WebSSOForm)