userSSOForm.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use client'
  2. import { useRouter, useSearchParams } from 'next/navigation'
  3. import type { FC } from 'react'
  4. import { useEffect, useState } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import cn from '@/utils/classnames'
  7. import Toast from '@/app/components/base/toast'
  8. import { getUserOAuth2SSOUrl, getUserOIDCSSOUrl, getUserSAMLSSOUrl } from '@/service/sso'
  9. import Button from '@/app/components/base/button'
  10. import useRefreshToken from '@/hooks/use-refresh-token'
  11. type UserSSOFormProps = {
  12. protocol: string
  13. }
  14. const UserSSOForm: FC<UserSSOFormProps> = ({
  15. protocol,
  16. }) => {
  17. const { getNewAccessToken } = useRefreshToken()
  18. const searchParams = useSearchParams()
  19. const consoleToken = searchParams.get('access_token')
  20. const refreshToken = searchParams.get('refresh_token')
  21. const message = searchParams.get('message')
  22. const router = useRouter()
  23. const { t } = useTranslation()
  24. const [isLoading, setIsLoading] = useState(false)
  25. useEffect(() => {
  26. if (refreshToken && consoleToken) {
  27. localStorage.setItem('console_token', consoleToken)
  28. localStorage.setItem('refresh_token', refreshToken)
  29. getNewAccessToken()
  30. router.replace('/apps')
  31. }
  32. if (message) {
  33. Toast.notify({
  34. type: 'error',
  35. message,
  36. })
  37. }
  38. }, [consoleToken, refreshToken, message, router])
  39. const handleSSOLogin = () => {
  40. setIsLoading(true)
  41. if (protocol === 'saml') {
  42. getUserSAMLSSOUrl().then((res) => {
  43. router.push(res.url)
  44. }).finally(() => {
  45. setIsLoading(false)
  46. })
  47. }
  48. else if (protocol === 'oidc') {
  49. getUserOIDCSSOUrl().then((res) => {
  50. document.cookie = `user-oidc-state=${res.state}`
  51. router.push(res.url)
  52. }).finally(() => {
  53. setIsLoading(false)
  54. })
  55. }
  56. else if (protocol === 'oauth2') {
  57. getUserOAuth2SSOUrl().then((res) => {
  58. document.cookie = `user-oauth2-state=${res.state}`
  59. router.push(res.url)
  60. }).finally(() => {
  61. setIsLoading(false)
  62. })
  63. }
  64. else {
  65. Toast.notify({
  66. type: 'error',
  67. message: 'invalid SSO protocol',
  68. })
  69. setIsLoading(false)
  70. }
  71. }
  72. return (
  73. <div className={
  74. cn(
  75. 'flex flex-col items-center w-full grow justify-center',
  76. 'px-6',
  77. 'md:px-[108px]',
  78. )
  79. }>
  80. <div className='flex flex-col md:w-[400px]'>
  81. <div className="w-full mx-auto">
  82. <h2 className="text-[32px] font-bold text-gray-900">{t('login.pageTitle')}</h2>
  83. </div>
  84. <div className="w-full mx-auto mt-10">
  85. <Button
  86. tabIndex={0}
  87. variant='primary'
  88. onClick={() => { handleSSOLogin() }}
  89. disabled={isLoading}
  90. className="w-full"
  91. >{t('login.sso')}
  92. </Button>
  93. </div>
  94. </div>
  95. </div>
  96. )
  97. }
  98. export default UserSSOForm