You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

normal-form.tsx 11KB

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