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

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