normalForm.tsx 11 KB

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