From 6242e91a6b476ed2d30fe7e80dcb125c7b907ee2 Mon Sep 17 00:00:00 2001 From: Matri Date: Mon, 7 Aug 2023 13:19:47 +0800 Subject: [PATCH] Fix: Install page redirects to signin if Dify finished setup. (#762) --- api/controllers/console/setup.py | 17 +-- web/app/install/installForm.tsx | 178 +++++++++++++++++-------------- web/app/install/page.tsx | 48 ++++----- web/models/common.ts | 5 + web/service/common.ts | 8 +- 5 files changed, 139 insertions(+), 117 deletions(-) diff --git a/api/controllers/console/setup.py b/api/controllers/console/setup.py index 4677a2075b..3724bcd254 100644 --- a/api/controllers/console/setup.py +++ b/api/controllers/console/setup.py @@ -19,15 +19,16 @@ from .wraps import only_edition_self_hosted class SetupApi(Resource): - @only_edition_self_hosted def get(self): - setup_status = get_setup_status() - if setup_status: - return { - 'step': 'finished', - 'setup_at': setup_status.setup_at.isoformat() - } - return {'step': 'not_start'} + if current_app.config['EDITION'] == 'SELF_HOSTED': + setup_status = get_setup_status() + if setup_status: + return { + 'step': 'finished', + 'setup_at': setup_status.setup_at.isoformat() + } + return {'step': 'not_start'} + return {'step': 'finished'} @only_edition_self_hosted def post(self): diff --git a/web/app/install/installForm.tsx b/web/app/install/installForm.tsx index b728478bb3..b951a9a8bd 100644 --- a/web/app/install/installForm.tsx +++ b/web/app/install/installForm.tsx @@ -1,13 +1,15 @@ 'use client' -import React from 'react' +import React, { useEffect } from 'react' import { useTranslation } from 'react-i18next' import Link from 'next/link' import { useRouter } from 'next/navigation' import { useContext } from 'use-context-selector' import Toast from '../components/base/toast' +import Loading from '../components/base/loading' import Button from '@/app/components/base/button' -import { setup } from '@/service/common' import I18n from '@/context/i18n' +import { fetchSetupStatus, setup } from '@/service/common' +import type { SetupStatusResponse } from '@/models/common' const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/ const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/ @@ -21,6 +23,8 @@ const InstallForm = () => { const [name, setName] = React.useState('') const [password, setPassword] = React.useState('') const [showPassword, setShowPassword] = React.useState(false) + const [loading, setLoading] = React.useState(true) + const showErrorMessage = (message: string) => { Toast.notify({ type: 'error', @@ -61,78 +65,90 @@ const InstallForm = () => { }) router.push('/signin') } + + useEffect(() => { + fetchSetupStatus().then((res: SetupStatusResponse) => { + if (res.step === 'finished') + window.location.href = '/signin' + else + setLoading(false) + }) + }, []) + return ( - <> -
-

{t('login.setAdminAccount')}

-

+

{t('login.setAdminAccountDesc')}

-
+ -
-
-
{ }}> -
- -
- setEmail(e.target.value)} - 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'} - /> -
-
- -
- -
- setName(e.target.value)} - 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'} - /> -
-
- -
- -
- setPassword(e.target.value)} - 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'} - /> -
- +
+
+ { }}> +
+ +
+ setEmail(e.target.value)} + 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'} + />
-
{t('login.error.passwordInvalid')}
-
+
+ +
+ setName(e.target.value)} + 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'} + /> +
+
- {/*
+
+ +
+ setPassword(e.target.value)} + 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'} + /> +
+ +
+
+
{t('login.error.passwordInvalid')}
+ +
+ + {/*
@@ -140,24 +156,24 @@ const InstallForm = () => {
*/} -
- -
- -
- {t('login.license.tip')} +
+ +
+ +
+ {t('login.license.tip')}   - {t('login.license.link')} + {t('login.license.link')} +
-
- + ) } diff --git a/web/app/install/page.tsx b/web/app/install/page.tsx index 8e9f31229f..b8faf57951 100644 --- a/web/app/install/page.tsx +++ b/web/app/install/page.tsx @@ -1,36 +1,32 @@ import React from 'react' -import InstallForm from './installForm' +import classNames from 'classnames' import Header from '../signin/_header' import style from '../signin/page.module.css' -import classNames from 'classnames' - -const SignIn = () => { +import InstallForm from './installForm' +const Install = () => { return ( - <> -
-
-
- -
- © {new Date().getFullYear()} Dify, Inc. All rights reserved. -
+
+
+
+ +
+ © {new Date().getFullYear()} Dify, Inc. All rights reserved.
- - +
) } -export default SignIn +export default Install diff --git a/web/models/common.ts b/web/models/common.ts index 826759bb3f..2c7eed3bf2 100644 --- a/web/models/common.ts +++ b/web/models/common.ts @@ -6,6 +6,11 @@ export type OauthResponse = { redirect_url: string } +export type SetupStatusResponse = { + step: 'finished' | 'not_started' + setup_at?: Date +} + export type UserProfileResponse = { id: string name: string diff --git a/web/service/common.ts b/web/service/common.ts index 426ac6a48c..87f0186678 100644 --- a/web/service/common.ts +++ b/web/service/common.ts @@ -3,8 +3,8 @@ import { del, get, patch, post, put } from './base' import type { AccountIntegrate, CommonResponse, DataSourceNotion, IWorkspace, LangGeniusVersionResponse, Member, - OauthResponse, PluginProvider, Provider, ProviderAnthropicToken, ProviderAzureToken, TenantInfoResponse, - UserProfileOriginResponse, + OauthResponse, PluginProvider, Provider, ProviderAnthropicToken, ProviderAzureToken, + SetupStatusResponse, TenantInfoResponse, UserProfileOriginResponse, } from '@/models/common' import type { UpdateOpenAIKeyResponse, @@ -19,6 +19,10 @@ export const setup: Fetcher }> = ({ return post('/setup', { body }) as Promise } +export const fetchSetupStatus = () => { + return get('/setup') as Promise +} + export const fetchUserProfile: Fetcher }> = ({ url, params }) => { return get(url, params, { needAllResponseContent: true }) as Promise }