| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 'use client' | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  | import React, { useEffect } from 'react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import Link from 'next/link' | 
					
						
							|  |  |  | import { useRouter } from 'next/navigation' | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import type { SubmitHandler } from 'react-hook-form' | 
					
						
							|  |  |  | import { useForm } from 'react-hook-form' | 
					
						
							|  |  |  | import { z } from 'zod' | 
					
						
							|  |  |  | import { zodResolver } from '@hookform/resolvers/zod' | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  | import Loading from '../components/base/loading' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import classNames from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | import Button from '@/app/components/base/button' | 
					
						
							| 
									
										
										
										
											2024-01-24 11:08:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-01 15:03:56 +08:00
										 |  |  | import { fetchInitValidateStatus, fetchSetupStatus, setup } from '@/service/common' | 
					
						
							|  |  |  | import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  | const accountFormSchema = z.object({ | 
					
						
							|  |  |  |   email: z | 
					
						
							|  |  |  |     .string() | 
					
						
							|  |  |  |     .min(1, { message: 'login.error.emailInValid' }) | 
					
						
							|  |  |  |     .email('login.error.emailInValid'), | 
					
						
							|  |  |  |   name: z.string().min(1, { message: 'login.error.nameEmpty' }), | 
					
						
							|  |  |  |   password: z.string().min(8, { | 
					
						
							|  |  |  |     message: 'login.error.passwordLengthInValid', | 
					
						
							|  |  |  |   }).regex(validPassword, 'login.error.passwordInvalid'), | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type AccountFormValues = z.infer<typeof accountFormSchema> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | const InstallForm = () => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const router = useRouter() | 
					
						
							| 
									
										
										
										
											2023-07-24 14:48:00 +08:00
										 |  |  |   const [showPassword, setShowPassword] = React.useState(false) | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |   const [loading, setLoading] = React.useState(true) | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     register, | 
					
						
							|  |  |  |     handleSubmit, | 
					
						
							|  |  |  |     formState: { errors }, | 
					
						
							|  |  |  |   } = useForm<AccountFormValues>({ | 
					
						
							|  |  |  |     resolver: zodResolver(accountFormSchema), | 
					
						
							|  |  |  |     defaultValues: { | 
					
						
							|  |  |  |       name: '', | 
					
						
							|  |  |  |       password: '', | 
					
						
							|  |  |  |       email: '', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |   const onSubmit: SubmitHandler<AccountFormValues> = async (data) => { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     await setup({ | 
					
						
							|  |  |  |       body: { | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |         ...data, | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     }) | 
					
						
							|  |  |  |     router.push('/signin') | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |   const handleSetting = async () => { | 
					
						
							|  |  |  |     handleSubmit(onSubmit)() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     fetchSetupStatus().then((res: SetupStatusResponse) => { | 
					
						
							| 
									
										
										
										
											2024-02-01 15:03:56 +08:00
										 |  |  |       if (res.step === 'finished') { | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |         localStorage.setItem('setup_status', 'finished') | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |         window.location.href = '/signin' | 
					
						
							| 
									
										
										
										
											2024-02-01 15:03:56 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         fetchInitValidateStatus().then((res: InitValidateStatusResponse) => { | 
					
						
							|  |  |  |           if (res.status === 'not_started') | 
					
						
							|  |  |  |             window.location.href = '/init' | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       setLoading(false) | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |     }) | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |     loading | 
					
						
							|  |  |  |       ? <Loading /> | 
					
						
							|  |  |  |       : <> | 
					
						
							|  |  |  |         <div className="sm:mx-auto sm:w-full sm:max-w-md"> | 
					
						
							|  |  |  |           <h2 className="text-[32px] font-bold text-gray-900">{t('login.setAdminAccount')}</h2> | 
					
						
							|  |  |  |           <p className=' | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |           mt-1 text-sm text-gray-600 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         '>{t('login.setAdminAccountDesc')}</p> | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |         </div> | 
					
						
							|  |  |  |         <div className="grow mt-8 sm:mx-auto sm:w-full sm:max-w-md"> | 
					
						
							|  |  |  |           <div className="bg-white "> | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |             <form onSubmit={handleSubmit(onSubmit)}> | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               <div className='mb-5'> | 
					
						
							|  |  |  |                 <label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900"> | 
					
						
							|  |  |  |                   {t('login.email')} | 
					
						
							|  |  |  |                 </label> | 
					
						
							|  |  |  |                 <div className="mt-1"> | 
					
						
							|  |  |  |                   <input | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |                     {...register('email')} | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |                     placeholder={t('login.emailPlaceholder') || ''} | 
					
						
							|  |  |  |                     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'} | 
					
						
							|  |  |  |                   /> | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |                   {errors.email && <span className='text-red-400 text-sm'>{t(`${errors.email?.message}`)}</span>} | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |                 </div> | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |               </div> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               <div className='mb-5'> | 
					
						
							|  |  |  |                 <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900"> | 
					
						
							|  |  |  |                   {t('login.name')} | 
					
						
							|  |  |  |                 </label> | 
					
						
							|  |  |  |                 <div className="mt-1 relative rounded-md shadow-sm"> | 
					
						
							|  |  |  |                   <input | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |                     {...register('name')} | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |                     placeholder={t('login.namePlaceholder') || ''} | 
					
						
							|  |  |  |                     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'} | 
					
						
							|  |  |  |                   /> | 
					
						
							|  |  |  |                 </div> | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |                 {errors.name && <span className='text-red-400 text-sm'>{t(`${errors.name.message}`)}</span>} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |               </div> | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               <div className='mb-5'> | 
					
						
							|  |  |  |                 <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900"> | 
					
						
							|  |  |  |                   {t('login.password')} | 
					
						
							|  |  |  |                 </label> | 
					
						
							|  |  |  |                 <div className="mt-1 relative rounded-md shadow-sm"> | 
					
						
							|  |  |  |                   <input | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |                     {...register('password')} | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |                     type={showPassword ? 'text' : 'password'} | 
					
						
							|  |  |  |                     placeholder={t('login.passwordPlaceholder') || ''} | 
					
						
							|  |  |  |                     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'} | 
					
						
							|  |  |  |                   /> | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |                   <div className="absolute inset-y-0 right-0 flex items-center pr-3"> | 
					
						
							|  |  |  |                     <button | 
					
						
							|  |  |  |                       type="button" | 
					
						
							|  |  |  |                       onClick={() => setShowPassword(!showPassword)} | 
					
						
							|  |  |  |                       className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500" | 
					
						
							|  |  |  |                     > | 
					
						
							|  |  |  |                       {showPassword ? '👀' : '😝'} | 
					
						
							|  |  |  |                     </button> | 
					
						
							|  |  |  |                   </div> | 
					
						
							| 
									
										
										
										
											2023-07-24 14:48:00 +08:00
										 |  |  |                 </div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 15:38:09 +08:00
										 |  |  |                 <div className={classNames('mt-1 text-xs text-gray-500', { | 
					
						
							|  |  |  |                   'text-red-400 !text-sm': errors.password, | 
					
						
							|  |  |  |                 })}>{t('login.error.passwordInvalid')}</div> | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               </div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               <div> | 
					
						
							| 
									
										
										
										
											2024-06-21 14:17:45 +08:00
										 |  |  |                 <Button variant='primary' className='w-full' onClick={handleSetting}> | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |                   {t('login.installBtn')} | 
					
						
							|  |  |  |                 </Button> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             </form> | 
					
						
							| 
									
										
										
										
											2024-10-21 09:23:20 +08:00
										 |  |  |             <div className="block w-full mt-2 text-xs text-gray-600"> | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               {t('login.license.tip')} | 
					
						
							| 
									
										
										
										
											2024-02-02 15:42:42 +08:00
										 |  |  |                 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               <Link | 
					
						
							|  |  |  |                 className='text-primary-600' | 
					
						
							| 
									
										
										
										
											2024-02-02 15:42:42 +08:00
										 |  |  |                 target='_blank' rel='noopener noreferrer' | 
					
						
							| 
									
										
										
										
											2024-01-24 11:08:11 +08:00
										 |  |  |                 href={'https://docs.dify.ai/user-agreement/open-source'} | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |               >{t('login.license.link')}</Link> | 
					
						
							|  |  |  |             </div> | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |       </> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default InstallForm |