normal-form.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import React, { useCallback, useEffect, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useRouter, useSearchParams } from 'next/navigation'
  4. import { RiContractLine, RiDoorLockLine, RiErrorWarningFill } from '@remixicon/react'
  5. import Loading from '../components/base/loading'
  6. import MailAndCodeAuth from './components/mail-and-code-auth'
  7. import MailAndPasswordAuth from './components/mail-and-password-auth'
  8. import SocialAuth from './components/social-auth'
  9. import SSOAuth from './components/sso-auth'
  10. import cn from '@/utils/classnames'
  11. import { invitationCheck } from '@/service/common'
  12. import { LicenseStatus } from '@/types/feature'
  13. import Toast from '@/app/components/base/toast'
  14. import { useGlobalPublicStore } from '@/context/global-public-context'
  15. const NormalForm = () => {
  16. const { t } = useTranslation()
  17. const router = useRouter()
  18. const searchParams = useSearchParams()
  19. const consoleToken = decodeURIComponent(searchParams.get('access_token') || '')
  20. const refreshToken = decodeURIComponent(searchParams.get('refresh_token') || '')
  21. const message = decodeURIComponent(searchParams.get('message') || '')
  22. const invite_token = decodeURIComponent(searchParams.get('invite_token') || '')
  23. const [isLoading, setIsLoading] = useState(true)
  24. const { systemFeatures } = useGlobalPublicStore()
  25. const [authType, updateAuthType] = useState<'code' | 'password'>('password')
  26. const [showORLine, setShowORLine] = useState(false)
  27. const [allMethodsAreDisabled, setAllMethodsAreDisabled] = useState(false)
  28. const [workspaceName, setWorkSpaceName] = useState('')
  29. const isInviteLink = Boolean(invite_token && invite_token !== 'null')
  30. const init = useCallback(async () => {
  31. try {
  32. if (consoleToken && refreshToken) {
  33. localStorage.setItem('console_token', consoleToken)
  34. localStorage.setItem('refresh_token', refreshToken)
  35. router.replace('/apps')
  36. return
  37. }
  38. if (message) {
  39. Toast.notify({
  40. type: 'error',
  41. message,
  42. })
  43. }
  44. setAllMethodsAreDisabled(!systemFeatures.enable_social_oauth_login && !systemFeatures.enable_email_code_login && !systemFeatures.enable_email_password_login && !systemFeatures.sso_enforced_for_signin)
  45. setShowORLine((systemFeatures.enable_social_oauth_login || systemFeatures.sso_enforced_for_signin) && (systemFeatures.enable_email_code_login || systemFeatures.enable_email_password_login))
  46. updateAuthType(systemFeatures.enable_email_password_login ? 'password' : 'code')
  47. if (isInviteLink) {
  48. const checkRes = await invitationCheck({
  49. url: '/activate/check',
  50. params: {
  51. token: invite_token,
  52. },
  53. })
  54. setWorkSpaceName(checkRes?.data?.workspace_name || '')
  55. }
  56. }
  57. catch (error) {
  58. console.error(error)
  59. setAllMethodsAreDisabled(true)
  60. }
  61. finally { setIsLoading(false) }
  62. }, [consoleToken, refreshToken, message, router, invite_token, isInviteLink, systemFeatures])
  63. useEffect(() => {
  64. init()
  65. }, [init])
  66. if (isLoading || consoleToken) {
  67. return <div className={
  68. cn(
  69. 'flex w-full grow flex-col items-center justify-center',
  70. 'px-6',
  71. 'md:px-[108px]',
  72. )
  73. }>
  74. <Loading type='area' />
  75. </div>
  76. }
  77. if (systemFeatures.license?.status === LicenseStatus.LOST) {
  78. return <div className='mx-auto mt-8 w-full'>
  79. <div className='relative'>
  80. <div className="rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2 p-4">
  81. <div className='shadows-shadow-lg relative mb-2 flex h-10 w-10 items-center justify-center rounded-xl bg-components-card-bg shadow'>
  82. <RiContractLine className='h-5 w-5' />
  83. <RiErrorWarningFill className='absolute -right-1 -top-1 h-4 w-4 text-text-warning-secondary' />
  84. </div>
  85. <p className='system-sm-medium text-text-primary'>{t('login.licenseLost')}</p>
  86. <p className='system-xs-regular mt-1 text-text-tertiary'>{t('login.licenseLostTip')}</p>
  87. </div>
  88. </div>
  89. </div>
  90. }
  91. if (systemFeatures.license?.status === LicenseStatus.EXPIRED) {
  92. return <div className='mx-auto mt-8 w-full'>
  93. <div className='relative'>
  94. <div className="rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2 p-4">
  95. <div className='shadows-shadow-lg relative mb-2 flex h-10 w-10 items-center justify-center rounded-xl bg-components-card-bg shadow'>
  96. <RiContractLine className='h-5 w-5' />
  97. <RiErrorWarningFill className='absolute -right-1 -top-1 h-4 w-4 text-text-warning-secondary' />
  98. </div>
  99. <p className='system-sm-medium text-text-primary'>{t('login.licenseExpired')}</p>
  100. <p className='system-xs-regular mt-1 text-text-tertiary'>{t('login.licenseExpiredTip')}</p>
  101. </div>
  102. </div>
  103. </div>
  104. }
  105. if (systemFeatures.license?.status === LicenseStatus.INACTIVE) {
  106. return <div className='mx-auto mt-8 w-full'>
  107. <div className='relative'>
  108. <div className="rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2 p-4">
  109. <div className='shadows-shadow-lg relative mb-2 flex h-10 w-10 items-center justify-center rounded-xl bg-components-card-bg shadow'>
  110. <RiContractLine className='h-5 w-5' />
  111. <RiErrorWarningFill className='absolute -right-1 -top-1 h-4 w-4 text-text-warning-secondary' />
  112. </div>
  113. <p className='system-sm-medium text-text-primary'>{t('login.licenseInactive')}</p>
  114. <p className='system-xs-regular mt-1 text-text-tertiary'>{t('login.licenseInactiveTip')}</p>
  115. </div>
  116. </div>
  117. </div>
  118. }
  119. return (
  120. <>
  121. <div className="mx-auto mt-8 w-full">
  122. {isInviteLink
  123. ? <div className="mx-auto w-full">
  124. <h2 className="title-4xl-semi-bold text-text-primary">{t('login.join')}{workspaceName}</h2>
  125. {!systemFeatures.branding.enabled && <p className='body-md-regular mt-2 text-text-tertiary'>{t('login.joinTipStart')}{workspaceName}{t('login.joinTipEnd')}</p>}
  126. </div>
  127. : <div className="mx-auto w-full">
  128. <h2 className="title-4xl-semi-bold text-text-primary">{t('login.pageTitle')}</h2>
  129. {!systemFeatures.branding.enabled && <p className='body-md-regular mt-2 text-text-tertiary'>{t('login.welcome')}</p>}
  130. </div>}
  131. <div className="relative">
  132. <div className="mt-6 flex flex-col gap-3">
  133. {systemFeatures.enable_social_oauth_login && <SocialAuth />}
  134. {systemFeatures.sso_enforced_for_signin && <div className='w-full'>
  135. <SSOAuth protocol={systemFeatures.sso_enforced_for_signin_protocol} />
  136. </div>}
  137. </div>
  138. {showORLine && <div className="relative mt-6">
  139. <div className="flex items-center">
  140. <div className="h-px flex-1 bg-gradient-to-r from-background-gradient-mask-transparent to-divider-regular"></div>
  141. <span className="system-xs-medium-uppercase px-3 text-text-tertiary">{t('login.or')}</span>
  142. <div className="h-px flex-1 bg-gradient-to-l from-background-gradient-mask-transparent to-divider-regular"></div>
  143. </div>
  144. </div>}
  145. {
  146. (systemFeatures.enable_email_code_login || systemFeatures.enable_email_password_login) && <>
  147. {systemFeatures.enable_email_code_login && authType === 'code' && <>
  148. <MailAndCodeAuth isInvite={isInviteLink} />
  149. {systemFeatures.enable_email_password_login && <div className='cursor-pointer py-1 text-center' onClick={() => { updateAuthType('password') }}>
  150. <span className='system-xs-medium text-components-button-secondary-accent-text'>{t('login.usePassword')}</span>
  151. </div>}
  152. </>}
  153. {systemFeatures.enable_email_password_login && authType === 'password' && <>
  154. <MailAndPasswordAuth isInvite={isInviteLink} isEmailSetup={systemFeatures.is_email_setup} allowRegistration={systemFeatures.is_allow_register} />
  155. {systemFeatures.enable_email_code_login && <div className='cursor-pointer py-1 text-center' onClick={() => { updateAuthType('code') }}>
  156. <span className='system-xs-medium text-components-button-secondary-accent-text'>{t('login.useVerificationCode')}</span>
  157. </div>}
  158. </>}
  159. </>
  160. }
  161. {allMethodsAreDisabled && <>
  162. <div className="rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2 p-4">
  163. <div className='shadows-shadow-lg mb-2 flex h-10 w-10 items-center justify-center rounded-xl bg-components-card-bg shadow'>
  164. <RiDoorLockLine className='h-5 w-5' />
  165. </div>
  166. <p className='system-sm-medium text-text-primary'>{t('login.noLoginMethod')}</p>
  167. <p className='system-xs-regular mt-1 text-text-tertiary'>{t('login.noLoginMethodTip')}</p>
  168. </div>
  169. <div className="relative my-2 py-2">
  170. <div className="absolute inset-0 flex items-center" aria-hidden="true">
  171. <div className='h-px w-full bg-gradient-to-r from-background-gradient-mask-transparent via-divider-regular to-background-gradient-mask-transparent'></div>
  172. </div>
  173. </div>
  174. </>}
  175. {!systemFeatures.branding.enabled && <>
  176. {/* <div className="system-xs-regular mt-2 block w-full text-text-tertiary">
  177. {t('login.tosDesc')}
  178. &nbsp;
  179. <Link
  180. className='system-xs-medium text-text-secondary hover:underline'
  181. target='_blank' rel='noopener noreferrer'
  182. href='https://dify.ai/terms'
  183. >{t('login.tos')}</Link>
  184. &nbsp;&&nbsp;
  185. <Link
  186. className='system-xs-medium text-text-secondary hover:underline'
  187. target='_blank' rel='noopener noreferrer'
  188. href='https://dify.ai/privacy'
  189. >{t('login.pp')}</Link>
  190. </div> */}
  191. </>}
  192. </div>
  193. </div>
  194. </>
  195. )
  196. }
  197. export default NormalForm