sso-auth.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use client'
  2. import { useRouter, useSearchParams } from 'next/navigation'
  3. import type { FC } from 'react'
  4. import { useState } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import { Lock01 } from '@/app/components/base/icons/src/vender/solid/security'
  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 { SSOProtocol } from '@/types/feature'
  11. type SSOAuthProps = {
  12. protocol: SSOProtocol | ''
  13. }
  14. const SSOAuth: FC<SSOAuthProps> = ({
  15. protocol,
  16. }) => {
  17. const router = useRouter()
  18. const { t } = useTranslation()
  19. const searchParams = useSearchParams()
  20. const invite_token = decodeURIComponent(searchParams.get('invite_token') || '')
  21. const [isLoading, setIsLoading] = useState(false)
  22. const handleSSOLogin = () => {
  23. setIsLoading(true)
  24. if (protocol === SSOProtocol.SAML) {
  25. getUserSAMLSSOUrl(invite_token).then((res) => {
  26. router.push(res.url)
  27. }).finally(() => {
  28. setIsLoading(false)
  29. })
  30. }
  31. else if (protocol === SSOProtocol.OIDC) {
  32. getUserOIDCSSOUrl(invite_token).then((res) => {
  33. document.cookie = `user-oidc-state=${res.state}`
  34. router.push(res.url)
  35. }).finally(() => {
  36. setIsLoading(false)
  37. })
  38. }
  39. else if (protocol === SSOProtocol.OAuth2) {
  40. getUserOAuth2SSOUrl(invite_token).then((res) => {
  41. document.cookie = `user-oauth2-state=${res.state}`
  42. router.push(res.url)
  43. }).finally(() => {
  44. setIsLoading(false)
  45. })
  46. }
  47. else {
  48. Toast.notify({
  49. type: 'error',
  50. message: 'invalid SSO protocol',
  51. })
  52. setIsLoading(false)
  53. }
  54. }
  55. return (
  56. <Button
  57. tabIndex={0}
  58. onClick={() => { handleSSOLogin() }}
  59. disabled={isLoading}
  60. className="w-full"
  61. >
  62. <Lock01 className='mr-2 w-5 h-5 text-text-accent-light-mode-only' />
  63. <span className="truncate">{t('login.withSSO')}</span>
  64. </Button>
  65. )
  66. }
  67. export default SSOAuth