Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

sso-auth.tsx 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use client'
  2. import { useRouter, useSearchParams } from 'next/navigation'
  3. import type { FC } from 'react'
  4. import { useCallback } from 'react'
  5. import { useState } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { Lock01 } from '@/app/components/base/icons/src/vender/solid/security'
  8. import Toast from '@/app/components/base/toast'
  9. import Button from '@/app/components/base/button'
  10. import { SSOProtocol } from '@/types/feature'
  11. import { fetchMembersOAuth2SSOUrl, fetchMembersOIDCSSOUrl, fetchMembersSAMLSSOUrl } from '@/service/share'
  12. type SSOAuthProps = {
  13. protocol: SSOProtocol | ''
  14. }
  15. const SSOAuth: FC<SSOAuthProps> = ({
  16. protocol,
  17. }) => {
  18. const router = useRouter()
  19. const { t } = useTranslation()
  20. const searchParams = useSearchParams()
  21. const redirectUrl = searchParams.get('redirect_url')
  22. const getAppCodeFromRedirectUrl = useCallback(() => {
  23. if (!redirectUrl)
  24. return null
  25. const url = new URL(`${window.location.origin}${decodeURIComponent(redirectUrl)}`)
  26. const appCode = url.pathname.split('/').pop()
  27. if (!appCode)
  28. return null
  29. return appCode
  30. }, [redirectUrl])
  31. const [isLoading, setIsLoading] = useState(false)
  32. const handleSSOLogin = () => {
  33. const appCode = getAppCodeFromRedirectUrl()
  34. if (!redirectUrl || !appCode) {
  35. Toast.notify({
  36. type: 'error',
  37. message: 'invalid redirect URL or app code',
  38. })
  39. return
  40. }
  41. setIsLoading(true)
  42. if (protocol === SSOProtocol.SAML) {
  43. fetchMembersSAMLSSOUrl(appCode, redirectUrl).then((res) => {
  44. router.push(res.url)
  45. }).finally(() => {
  46. setIsLoading(false)
  47. })
  48. }
  49. else if (protocol === SSOProtocol.OIDC) {
  50. fetchMembersOIDCSSOUrl(appCode, redirectUrl).then((res) => {
  51. router.push(res.url)
  52. }).finally(() => {
  53. setIsLoading(false)
  54. })
  55. }
  56. else if (protocol === SSOProtocol.OAuth2) {
  57. fetchMembersOAuth2SSOUrl(appCode, redirectUrl).then((res) => {
  58. router.push(res.url)
  59. }).finally(() => {
  60. setIsLoading(false)
  61. })
  62. }
  63. else {
  64. Toast.notify({
  65. type: 'error',
  66. message: 'invalid SSO protocol',
  67. })
  68. setIsLoading(false)
  69. }
  70. }
  71. return (
  72. <Button
  73. tabIndex={0}
  74. onClick={() => { handleSSOLogin() }}
  75. disabled={isLoading}
  76. className="w-full"
  77. >
  78. <Lock01 className='mr-2 h-5 w-5 text-text-accent-light-mode-only' />
  79. <span className="truncate">{t('login.withSSO')}</span>
  80. </Button>
  81. )
  82. }
  83. export default SSOAuth