mirror of
https://github.com/langgenius/dify.git
synced 2026-01-24 20:52:16 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
208 lines
8.1 KiB
TypeScript
208 lines
8.1 KiB
TypeScript
'use client'
|
|
import React, { useCallback, useEffect } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { useDebounceFn } from 'ahooks'
|
|
|
|
import Link from 'next/link'
|
|
import { useRouter } from 'next/navigation'
|
|
|
|
import type { SubmitHandler } from 'react-hook-form'
|
|
import { useForm } from 'react-hook-form'
|
|
import { z } from 'zod'
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import Loading from '../components/base/loading'
|
|
import classNames from '@/utils/classnames'
|
|
import Button from '@/app/components/base/button'
|
|
|
|
import { fetchInitValidateStatus, fetchSetupStatus, login, setup } from '@/service/common'
|
|
import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common'
|
|
import useDocumentTitle from '@/hooks/use-document-title'
|
|
import { useDocLink } from '@/context/i18n'
|
|
import { validPassword } from '@/config'
|
|
|
|
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>
|
|
|
|
const InstallForm = () => {
|
|
useDocumentTitle('')
|
|
const { t } = useTranslation()
|
|
const docLink = useDocLink()
|
|
const router = useRouter()
|
|
const [showPassword, setShowPassword] = React.useState(false)
|
|
const [loading, setLoading] = React.useState(true)
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors, isSubmitting },
|
|
} = useForm<AccountFormValues>({
|
|
resolver: zodResolver(accountFormSchema),
|
|
defaultValues: {
|
|
name: '',
|
|
password: '',
|
|
email: '',
|
|
},
|
|
})
|
|
|
|
const onSubmit: SubmitHandler<AccountFormValues> = async (data) => {
|
|
// First, setup the admin account
|
|
await setup({
|
|
body: {
|
|
...data,
|
|
},
|
|
})
|
|
|
|
// Then, automatically login with the same credentials
|
|
const loginRes = await login({
|
|
url: '/login',
|
|
body: {
|
|
email: data.email,
|
|
password: data.password,
|
|
},
|
|
})
|
|
|
|
// Store tokens and redirect to apps if login successful
|
|
if (loginRes.result === 'success') {
|
|
localStorage.setItem('console_token', loginRes.data.access_token)
|
|
localStorage.setItem('refresh_token', loginRes.data.refresh_token)
|
|
router.replace('/apps')
|
|
}
|
|
else {
|
|
// Fallback to signin page if auto-login fails
|
|
router.replace('/signin')
|
|
}
|
|
}
|
|
|
|
const handleSetting = async () => {
|
|
if (isSubmitting) return
|
|
handleSubmit(onSubmit)()
|
|
}
|
|
|
|
const { run: debouncedHandleKeyDown } = useDebounceFn(
|
|
(e: React.KeyboardEvent) => {
|
|
if (e.key === 'Enter') {
|
|
e.preventDefault()
|
|
handleSetting()
|
|
}
|
|
},
|
|
{ wait: 200 },
|
|
)
|
|
|
|
const handleKeyDown = useCallback(debouncedHandleKeyDown, [debouncedHandleKeyDown])
|
|
|
|
useEffect(() => {
|
|
fetchSetupStatus().then((res: SetupStatusResponse) => {
|
|
if (res.step === 'finished') {
|
|
localStorage.setItem('setup_status', 'finished')
|
|
router.push('/signin')
|
|
}
|
|
else {
|
|
fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
|
|
if (res.status === 'not_started')
|
|
router.push('/init')
|
|
})
|
|
}
|
|
setLoading(false)
|
|
})
|
|
}, [])
|
|
|
|
return (
|
|
loading
|
|
? <Loading />
|
|
: <>
|
|
<div className="sm:mx-auto sm:w-full sm:max-w-md">
|
|
<h2 className="text-[32px] font-bold text-text-primary">{t('login.setAdminAccount')}</h2>
|
|
<p className='mt-1 text-sm text-text-secondary'>{t('login.setAdminAccountDesc')}</p>
|
|
</div>
|
|
<div className="mt-8 grow sm:mx-auto sm:w-full sm:max-w-md">
|
|
<div className="relative">
|
|
<form onSubmit={handleSubmit(onSubmit)} onKeyDown={handleKeyDown}>
|
|
<div className='mb-5'>
|
|
<label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-text-primary">
|
|
{t('login.email')}
|
|
</label>
|
|
<div className="mt-1 rounded-md shadow-sm">
|
|
<input
|
|
{...register('email')}
|
|
placeholder={t('login.emailPlaceholder') || ''}
|
|
className={'w-full appearance-none rounded-md border border-transparent bg-components-input-bg-normal py-[7px] pl-2 text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs'}
|
|
/>
|
|
{errors.email && <span className='text-sm text-red-400'>{t(`${errors.email?.message}`)}</span>}
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className='mb-5'>
|
|
<label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-text-primary">
|
|
{t('login.name')}
|
|
</label>
|
|
<div className="relative mt-1 rounded-md shadow-sm">
|
|
<input
|
|
{...register('name')}
|
|
placeholder={t('login.namePlaceholder') || ''}
|
|
className={'w-full appearance-none rounded-md border border-transparent bg-components-input-bg-normal py-[7px] pl-2 text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs'}
|
|
/>
|
|
</div>
|
|
{errors.name && <span className='text-sm text-red-400'>{t(`${errors.name.message}`)}</span>}
|
|
</div>
|
|
|
|
<div className='mb-5'>
|
|
<label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-text-primary">
|
|
{t('login.password')}
|
|
</label>
|
|
<div className="relative mt-1 rounded-md shadow-sm">
|
|
<input
|
|
{...register('password')}
|
|
type={showPassword ? 'text' : 'password'}
|
|
placeholder={t('login.passwordPlaceholder') || ''}
|
|
className={'w-full appearance-none rounded-md border border-transparent bg-components-input-bg-normal py-[7px] pl-2 text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs'}
|
|
/>
|
|
|
|
<div className="absolute inset-y-0 right-0 flex items-center pr-3">
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
className="text-text-quaternary hover:text-text-tertiary focus:text-text-tertiary focus:outline-none"
|
|
>
|
|
{showPassword ? '👀' : '😝'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className={classNames('mt-1 text-xs text-text-tertiary', {
|
|
'text-red-400 !text-sm': errors.password,
|
|
})}>{t('login.error.passwordInvalid')}</div>
|
|
</div>
|
|
|
|
<div>
|
|
<Button variant='primary' className='w-full' onClick={handleSetting}>
|
|
{t('login.installBtn')}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
<div className="mt-2 block w-full text-xs text-text-tertiary">
|
|
{t('login.license.tip')}
|
|
|
|
<Link
|
|
className='text-text-accent'
|
|
target='_blank' rel='noopener noreferrer'
|
|
href={docLink('/policies/open-source')}
|
|
>{t('login.license.link')}</Link>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default InstallForm
|