mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
fix(server): redirect to setup page if not initialized (#7875)
This commit is contained in:
@@ -48,7 +48,7 @@ function RootRoutes() {
|
||||
const config = useServerConfig();
|
||||
const location = useLocation();
|
||||
|
||||
if (!config.initialized) {
|
||||
if (!config.initialized && location.pathname !== '/admin/setup') {
|
||||
return <Navigate to="/admin/setup" />;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,14 +52,14 @@ export function Auth() {
|
||||
)
|
||||
.then(res => res.json())
|
||||
.then(
|
||||
({
|
||||
async ({
|
||||
data: {
|
||||
currentUser: { features },
|
||||
},
|
||||
}) => {
|
||||
if (features.includes(FeatureType.Admin)) {
|
||||
toast.success('Logged in successfully');
|
||||
revalidate();
|
||||
await revalidate();
|
||||
} else {
|
||||
toast.error('You are not an admin');
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useMutateQueryResource } from '@affine/core/hooks/use-mutation';
|
||||
import { useQueryImmutable } from '@affine/core/hooks/use-query';
|
||||
import { useQuery } from '@affine/core/hooks/use-query';
|
||||
import type { GetCurrentUserFeaturesQuery } from '@affine/graphql';
|
||||
import {
|
||||
adminServerConfigQuery,
|
||||
@@ -8,22 +8,30 @@ import {
|
||||
} from '@affine/graphql';
|
||||
|
||||
export const useServerConfig = () => {
|
||||
const { data } = useQueryImmutable({
|
||||
const { data } = useQuery({
|
||||
query: adminServerConfigQuery,
|
||||
});
|
||||
|
||||
return data.serverConfig;
|
||||
};
|
||||
|
||||
export const useRevalidateServerConfig = () => {
|
||||
const revalidate = useMutateQueryResource();
|
||||
|
||||
return () => {
|
||||
return revalidate(adminServerConfigQuery);
|
||||
};
|
||||
};
|
||||
|
||||
export const useRevalidateCurrentUser = () => {
|
||||
const revalidate = useMutateQueryResource();
|
||||
|
||||
return () => {
|
||||
revalidate(getCurrentUserFeaturesQuery);
|
||||
return revalidate(getCurrentUserFeaturesQuery);
|
||||
};
|
||||
};
|
||||
export const useCurrentUser = () => {
|
||||
const { data } = useQueryImmutable({
|
||||
const { data } = useQuery({
|
||||
query: getCurrentUserFeaturesQuery,
|
||||
});
|
||||
return data.currentUser;
|
||||
|
||||
@@ -6,13 +6,11 @@ import {
|
||||
CarouselItem,
|
||||
} from '@affine/admin/components/ui/carousel';
|
||||
import { validateEmailAndPassword } from '@affine/admin/utils';
|
||||
import { useMutateQueryResource } from '@affine/core/hooks/use-mutation';
|
||||
import { serverConfigQuery } from '@affine/graphql';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
import { useServerConfig } from '../common';
|
||||
import { useRevalidateServerConfig, useServerConfig } from '../common';
|
||||
import { CreateAdmin } from './create-admin';
|
||||
|
||||
export enum CarouselSteps {
|
||||
@@ -73,6 +71,7 @@ export const Form = () => {
|
||||
const [invalidPassword, setInvalidPassword] = useState(false);
|
||||
|
||||
const serverConfig = useServerConfig();
|
||||
const refreshServerConfig = useRevalidateServerConfig();
|
||||
const passwordLimits = serverConfig.credentialsRequirement.password;
|
||||
|
||||
const isCreateAdminStep = current - 1 === CarouselSteps.CreateAdmin;
|
||||
@@ -80,8 +79,6 @@ export const Form = () => {
|
||||
const disableContinue =
|
||||
(!nameValue || !emailValue || !passwordValue) && isCreateAdminStep;
|
||||
|
||||
const revalidate = useMutateQueryResource();
|
||||
|
||||
useEffect(() => {
|
||||
if (!api) {
|
||||
return;
|
||||
@@ -114,14 +111,14 @@ export const Form = () => {
|
||||
}
|
||||
|
||||
await createResponse.json();
|
||||
await revalidate(serverConfigQuery);
|
||||
await refreshServerConfig();
|
||||
toast.success('Admin account created successfully.');
|
||||
} catch (err) {
|
||||
toast.error((err as Error).message);
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}, [emailValue, passwordValue, revalidate]);
|
||||
}, [emailValue, passwordValue, refreshServerConfig]);
|
||||
|
||||
const onNext = useCallback(async () => {
|
||||
if (isCreateAdminStep) {
|
||||
|
||||
Reference in New Issue
Block a user