installForm.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. {/* 登录页login */}
  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={handleSubmit(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. {...register('email')}
  91. placeholder={t('login.emailPlaceholder') || ''}
  92. 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'}
  93. />
  94. {errors.email && <span className='text-red-400 text-sm'>{t(`${errors.email?.message}`)}</span>}
  95. </div>
  96. </div>
  97. <div className='mb-5'>
  98. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  99. {t('login.name')}
  100. </label>
  101. <div className="mt-1 relative rounded-md shadow-sm">
  102. <input
  103. {...register('name')}
  104. placeholder={t('login.namePlaceholder') || ''}
  105. 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'}
  106. />
  107. </div>
  108. {errors.name && <span className='text-red-400 text-sm'>{t(`${errors.name.message}`)}</span>}
  109. </div>
  110. <div className='mb-5'>
  111. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  112. {t('login.password')}
  113. </label>
  114. <div className="mt-1 relative rounded-md shadow-sm">
  115. <input
  116. {...register('password')}
  117. type={showPassword ? 'text' : 'password'}
  118. placeholder={t('login.passwordPlaceholder') || ''}
  119. 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'}
  120. />
  121. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  122. <button
  123. type="button"
  124. onClick={() => setShowPassword(!showPassword)}
  125. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  126. >
  127. {showPassword ? '👀' : '😝'}
  128. </button>
  129. </div>
  130. </div>
  131. <div className={classNames('mt-1 text-xs text-gray-500', {
  132. 'text-red-400 !text-sm': errors.password,
  133. })}>{t('login.error.passwordInvalid')}</div>
  134. </div>
  135. <div>
  136. <Button variant='primary' className='w-full' onClick={handleSetting}>
  137. {t('login.installBtn')}
  138. </Button>
  139. </div>
  140. </form>
  141. <div className="block w-full mt-2 text-xs text-gray-600">
  142. {t('login.license.tip')}
  143. &nbsp;
  144. <Link
  145. className='text-primary-600'
  146. target='_blank' rel='noopener noreferrer'
  147. href={'https://docs.dify.ai/user-agreement/open-source'}
  148. >{t('login.license.link')}</Link>
  149. </div>
  150. </div>
  151. </div>
  152. </>
  153. )
  154. }
  155. export default InstallForm