installForm.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 type { SubmitHandler } from 'react-hook-form'
  7. import { useForm } from 'react-hook-form'
  8. import { z } from 'zod'
  9. import { zodResolver } from '@hookform/resolvers/zod'
  10. import Loading from '../components/base/loading'
  11. import classNames from '@/utils/classnames'
  12. import Button from '@/app/components/base/button'
  13. import { fetchInitValidateStatus, fetchSetupStatus, setup } from '@/service/common'
  14. import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common'
  15. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  16. const accountFormSchema = z.object({
  17. email: z
  18. .string()
  19. .min(1, { message: 'login.error.emailInValid' })
  20. .email('login.error.emailInValid'),
  21. name: z.string().min(1, { message: 'login.error.nameEmpty' }),
  22. password: z.string().min(8, {
  23. message: 'login.error.passwordLengthInValid',
  24. }).regex(validPassword, 'login.error.passwordInvalid'),
  25. })
  26. type AccountFormValues = z.infer<typeof accountFormSchema>
  27. const InstallForm = () => {
  28. const { t } = useTranslation()
  29. const router = useRouter()
  30. const [showPassword, setShowPassword] = React.useState(false)
  31. const [loading, setLoading] = React.useState(true)
  32. const {
  33. register,
  34. handleSubmit,
  35. formState: { errors },
  36. } = useForm<AccountFormValues>({
  37. resolver: zodResolver(accountFormSchema),
  38. defaultValues: {
  39. name: '',
  40. password: '',
  41. email: '',
  42. },
  43. })
  44. const onSubmit: SubmitHandler<AccountFormValues> = async (data) => {
  45. await setup({
  46. body: {
  47. ...data,
  48. },
  49. })
  50. router.push('/signin')
  51. }
  52. const handleSetting = async () => {
  53. handleSubmit(onSubmit)()
  54. }
  55. useEffect(() => {
  56. fetchSetupStatus().then((res: SetupStatusResponse) => {
  57. if (res.step === 'finished') {
  58. localStorage.setItem('setup_status', 'finished')
  59. window.location.href = '/signin'
  60. }
  61. else {
  62. fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
  63. if (res.status === 'not_started')
  64. window.location.href = '/init'
  65. })
  66. }
  67. setLoading(false)
  68. })
  69. }, [])
  70. return (
  71. loading
  72. ? <Loading />
  73. : <>
  74. <div className="sm:mx-auto sm:w-full sm:max-w-md">
  75. <h2 className="text-[32px] font-bold text-gray-900">{t('login.setAdminAccount')}</h2>
  76. <p className='
  77. mt-1 text-sm text-gray-600
  78. '>{t('login.setAdminAccountDesc')}</p>
  79. </div>
  80. <div className="grow mt-8 sm:mx-auto sm:w-full sm:max-w-md">
  81. <div className="bg-white ">
  82. <form onSubmit={handleSubmit(onSubmit)}>
  83. <div className='mb-5'>
  84. <label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  85. {t('login.email')}
  86. </label>
  87. <div className="mt-1">
  88. <input
  89. {...register('email')}
  90. placeholder={t('login.emailPlaceholder') || ''}
  91. 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'}
  92. />
  93. {errors.email && <span className='text-red-400 text-sm'>{t(`${errors.email?.message}`)}</span>}
  94. </div>
  95. </div>
  96. <div className='mb-5'>
  97. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  98. {t('login.name')}
  99. </label>
  100. <div className="mt-1 relative rounded-md shadow-sm">
  101. <input
  102. {...register('name')}
  103. placeholder={t('login.namePlaceholder') || ''}
  104. 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'}
  105. />
  106. </div>
  107. {errors.name && <span className='text-red-400 text-sm'>{t(`${errors.name.message}`)}</span>}
  108. </div>
  109. <div className='mb-5'>
  110. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  111. {t('login.password')}
  112. </label>
  113. <div className="mt-1 relative rounded-md shadow-sm">
  114. <input
  115. {...register('password')}
  116. type={showPassword ? 'text' : 'password'}
  117. placeholder={t('login.passwordPlaceholder') || ''}
  118. 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'}
  119. />
  120. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  121. <button
  122. type="button"
  123. onClick={() => setShowPassword(!showPassword)}
  124. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  125. >
  126. {showPassword ? '👀' : '😝'}
  127. </button>
  128. </div>
  129. </div>
  130. <div className={classNames('mt-1 text-xs text-gray-500', {
  131. 'text-red-400 !text-sm': errors.password,
  132. })}>{t('login.error.passwordInvalid')}</div>
  133. </div>
  134. <div>
  135. <Button variant='primary' className='w-full' onClick={handleSetting}>
  136. {t('login.installBtn')}
  137. </Button>
  138. </div>
  139. </form>
  140. <div className="block w-full mt-2 text-xs text-gray-600">
  141. {t('login.license.tip')}
  142. &nbsp;
  143. <Link
  144. className='text-primary-600'
  145. target='_blank' rel='noopener noreferrer'
  146. href={'https://docs.dify.ai/user-agreement/open-source'}
  147. >{t('login.license.link')}</Link>
  148. </div>
  149. </div>
  150. </div>
  151. </>
  152. )
  153. }
  154. export default InstallForm