enterpriseSSOForm.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use client'
  2. import cn from 'classnames'
  3. import { useRouter, useSearchParams } from 'next/navigation'
  4. import type { FC } from 'react'
  5. import { useEffect, useState } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import Toast from '@/app/components/base/toast'
  8. import { getOIDCSSOUrl, getSAMLSSOUrl } from '@/service/enterprise'
  9. import Button from '@/app/components/base/button'
  10. type EnterpriseSSOFormProps = {
  11. protocol: string
  12. }
  13. const EnterpriseSSOForm: FC<EnterpriseSSOFormProps> = ({
  14. protocol,
  15. }) => {
  16. const searchParams = useSearchParams()
  17. const consoleToken = searchParams.get('console_token')
  18. const message = searchParams.get('message')
  19. const router = useRouter()
  20. const { t } = useTranslation()
  21. const [isLoading, setIsLoading] = useState(false)
  22. useEffect(() => {
  23. if (consoleToken) {
  24. localStorage.setItem('console_token', consoleToken)
  25. router.replace('/apps')
  26. }
  27. if (message) {
  28. Toast.notify({
  29. type: 'error',
  30. message,
  31. })
  32. }
  33. }, [])
  34. const handleSSOLogin = () => {
  35. setIsLoading(true)
  36. if (protocol === 'saml') {
  37. getSAMLSSOUrl().then((res) => {
  38. router.push(res.url)
  39. }).finally(() => {
  40. setIsLoading(false)
  41. })
  42. }
  43. else {
  44. getOIDCSSOUrl().then((res) => {
  45. document.cookie = `oidc-state=${res.state}`
  46. router.push(res.url)
  47. }).finally(() => {
  48. setIsLoading(false)
  49. })
  50. }
  51. }
  52. return (
  53. <div className={
  54. cn(
  55. 'flex flex-col items-center w-full grow justify-center',
  56. 'px-6',
  57. 'md:px-[108px]',
  58. )
  59. }>
  60. <div className='flex flex-col md:w-[400px]'>
  61. <div className="w-full mx-auto">
  62. <h2 className="text-[32px] font-bold text-gray-900">{t('login.pageTitle')}</h2>
  63. </div>
  64. <div className="w-full mx-auto mt-10">
  65. <Button
  66. tabIndex={0}
  67. type='primary'
  68. onClick={() => { handleSSOLogin() }}
  69. disabled={isLoading}
  70. className="w-full !fone-medium !text-sm"
  71. >{t('login.sso')}
  72. </Button>
  73. </div>
  74. </div>
  75. </div>
  76. )
  77. }
  78. export default EnterpriseSSOForm