installForm.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use client'
  2. import React, { useEffect } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Link from 'next/link'
  5. import { useRouter } from 'next/navigation'
  6. import { useContext } from 'use-context-selector'
  7. import Toast from '../components/base/toast'
  8. import Loading from '../components/base/loading'
  9. import Button from '@/app/components/base/button'
  10. import I18n from '@/context/i18n'
  11. import { fetchSetupStatus, setup } from '@/service/common'
  12. import type { SetupStatusResponse } from '@/models/common'
  13. const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/
  14. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  15. const InstallForm = () => {
  16. const { t } = useTranslation()
  17. const { locale } = useContext(I18n)
  18. const router = useRouter()
  19. const [email, setEmail] = React.useState('')
  20. const [name, setName] = React.useState('')
  21. const [password, setPassword] = React.useState('')
  22. const [showPassword, setShowPassword] = React.useState(false)
  23. const [loading, setLoading] = React.useState(true)
  24. const showErrorMessage = (message: string) => {
  25. Toast.notify({
  26. type: 'error',
  27. message,
  28. })
  29. }
  30. const valid = () => {
  31. if (!email) {
  32. showErrorMessage(t('login.error.emailEmpty'))
  33. return false
  34. }
  35. if (!validEmailReg.test(email)) {
  36. showErrorMessage(t('login.error.emailInValid'))
  37. return false
  38. }
  39. if (!name.trim()) {
  40. showErrorMessage(t('login.error.nameEmpty'))
  41. return false
  42. }
  43. if (!password.trim()) {
  44. showErrorMessage(t('login.error.passwordEmpty'))
  45. return false
  46. }
  47. if (!validPassword.test(password))
  48. showErrorMessage(t('login.error.passwordInvalid'))
  49. return true
  50. }
  51. const handleSetting = async () => {
  52. if (!valid())
  53. return
  54. await setup({
  55. body: {
  56. email,
  57. name,
  58. password,
  59. },
  60. })
  61. router.push('/signin')
  62. }
  63. useEffect(() => {
  64. fetchSetupStatus().then((res: SetupStatusResponse) => {
  65. if (res.step === 'finished')
  66. window.location.href = '/signin'
  67. else
  68. setLoading(false)
  69. })
  70. }, [])
  71. return (
  72. loading
  73. ? <Loading />
  74. : <>
  75. <div className="sm:mx-auto sm:w-full sm:max-w-md">
  76. <h2 className="text-[32px] font-bold text-gray-900">{t('login.setAdminAccount')}</h2>
  77. <p className='
  78. mt-1 text-sm text-gray-600
  79. '>{t('login.setAdminAccountDesc')}</p>
  80. </div>
  81. <div className="grow mt-8 sm:mx-auto sm:w-full sm:max-w-md">
  82. <div className="bg-white ">
  83. <form onSubmit={() => { }}>
  84. <div className='mb-5'>
  85. <label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  86. {t('login.email')}
  87. </label>
  88. <div className="mt-1">
  89. <input
  90. id="email"
  91. type="email"
  92. value={email}
  93. onChange={e => setEmail(e.target.value)}
  94. placeholder={t('login.emailPlaceholder') || ''}
  95. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm'}
  96. />
  97. </div>
  98. </div>
  99. <div className='mb-5'>
  100. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  101. {t('login.name')}
  102. </label>
  103. <div className="mt-1 relative rounded-md shadow-sm">
  104. <input
  105. id="name"
  106. type="text"
  107. value={name}
  108. onChange={e => setName(e.target.value)}
  109. placeholder={t('login.namePlaceholder') || ''}
  110. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'}
  111. />
  112. </div>
  113. </div>
  114. <div className='mb-5'>
  115. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  116. {t('login.password')}
  117. </label>
  118. <div className="mt-1 relative rounded-md shadow-sm">
  119. <input
  120. id="password"
  121. type={showPassword ? 'text' : 'password'}
  122. value={password}
  123. onChange={e => setPassword(e.target.value)}
  124. placeholder={t('login.passwordPlaceholder') || ''}
  125. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'}
  126. />
  127. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  128. <button
  129. type="button"
  130. onClick={() => setShowPassword(!showPassword)}
  131. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  132. >
  133. {showPassword ? '👀' : '😝'}
  134. </button>
  135. </div>
  136. </div>
  137. <div className='mt-1 text-xs text-gray-500'>{t('login.error.passwordInvalid')}</div>
  138. </div>
  139. {/* <div className="flex items-center justify-between">
  140. <div className="text-sm">
  141. <div className="flex items-center mb-4">
  142. <input id="default-checkbox" type="checkbox" className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 rounded" />
  143. <label htmlFor="default-checkbox" className="ml-2 text-sm font-medium cursor-pointer text-primary-600 hover:text-gray-500">{t('login.acceptPP')}</label>
  144. </div>
  145. </div>
  146. </div> */}
  147. <div>
  148. <Button type='primary' className='w-full !fone-medium !text-sm' onClick={handleSetting}>
  149. {t('login.installBtn')}
  150. </Button>
  151. </div>
  152. </form>
  153. <div className="block w-hull mt-2 text-xs text-gray-600">
  154. {t('login.license.tip')}
  155. &nbsp;
  156. <Link
  157. className='text-primary-600'
  158. target={'_blank'}
  159. href={`https://docs.dify.ai/${locale === 'en' ? '' : `v/${locale.toLowerCase()}/`}community/open-source`}
  160. >{t('login.license.link')}</Link>
  161. </div>
  162. </div>
  163. </div>
  164. </>
  165. )
  166. }
  167. export default InstallForm