fix: when auth is disabled, the users entity doesn't exist

This commit is contained in:
dswbx
2024-11-21 08:23:16 +01:00
parent 4e7c1e6e9f
commit 0df5c761ec
14 changed files with 212 additions and 74 deletions

View File

@@ -1,13 +1,28 @@
import { cloneDeep, omit } from "lodash-es";
import { useEffect, useRef } from "react";
import { useBknd } from "ui/client";
import { useBkndAuth } from "ui/client/schema/auth/use-bknd-auth";
import { useBkndData } from "ui/client/schema/data/use-bknd-data";
import { Button } from "ui/components/buttons/Button";
import { JsonSchemaForm } from "ui/components/form/json-schema/JsonSchemaForm";
import { Alert } from "ui/components/display/Alert";
import {
JsonSchemaForm,
type JsonSchemaFormRef
} from "ui/components/form/json-schema/JsonSchemaForm";
import * as AppShell from "ui/layouts/AppShell/AppShell";
import { useNavigate } from "ui/lib/routes";
import { extractSchema } from "../settings/utils/schema";
// @todo: improve the inline editing expierence, for now redirect to settings
export function AuthSettingsList() {
useBknd({ withSecrets: true });
return <AuthSettingsListInternal />;
const { app } = useBknd();
const [navigate] = useNavigate();
useEffect(() => {
navigate(app.getSettingsPath(["auth"]));
}, []);
/*useBknd({ withSecrets: true });
return <AuthSettingsListInternal />;*/
}
const uiSchema = {
@@ -21,9 +36,11 @@ const uiSchema = {
};
function AuthSettingsListInternal() {
const s = useBknd();
const config = s.config.auth;
const schema = cloneDeep(omit(s.schema.auth, ["title"]));
const $auth = useBkndAuth();
const { entities } = useBkndData();
const formRef = useRef<JsonSchemaFormRef>(null);
const config = $auth.config;
const schema = cloneDeep(omit($auth.schema, ["title"]));
const [generalSchema, generalConfig, extracted] = extractSchema(schema as any, config, [
"jwt",
"roles",
@@ -32,7 +49,6 @@ function AuthSettingsListInternal() {
]);
try {
const user_entity = config.entity_name ?? "users";
const entities = s.config.data.entities ?? {};
const user_fields = Object.entries(entities[user_entity]?.fields ?? {})
.map(([name, field]) => (!field.config?.virtual ? name : undefined))
.filter(Boolean);
@@ -42,18 +58,34 @@ function AuthSettingsListInternal() {
extracted.jwt.schema.properties.fields.items.enum = user_fields;
extracted.jwt.schema.properties.fields.uniqueItems = true;
uiSchema.jwt.fields["ui:widget"] = "checkboxes";
} else {
uiSchema.jwt.fields["ui:widget"] = "hidden";
}
} catch (e) {
console.error(e);
}
console.log({ generalSchema, generalConfig, extracted });
async function handleSubmit() {
console.log(formRef.current?.validateForm(), formRef.current?.formData());
}
return (
<>
<AppShell.SectionHeader right={<Button variant="primary">Update</Button>}>
<AppShell.SectionHeader
right={
<Button variant="primary" onClick={handleSubmit}>
Update
</Button>
}
>
Settings
</AppShell.SectionHeader>
<AppShell.Scrollable>
<Alert.Warning
visible={!config.enabled}
title="Auth not enabled"
message="Enable it by toggling the switch below. Please also make sure set a secure secret to sign JWT tokens."
/>
<div className="flex flex-col flex-grow px-5 py-4 gap-8">
<div>
<JsonSchemaForm
@@ -65,6 +97,7 @@ function AuthSettingsListInternal() {
<div className="flex flex-col gap-3">
<h3 className="font-bold">JWT Settings</h3>
<JsonSchemaForm
ref={formRef}
schema={extracted.jwt.schema}
uiSchema={uiSchema.jwt}
className="legacy hide-required-mark fieldset-alternative mute-root"