Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions web/components/registry/forms/shared/use-entity-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { useState } from "react";
import { useQueryClient } from "@tanstack/react-query";

import { useAcquireToken } from "@/hooks/use-acquire-token";
import {
createEntity,
updateEntity,
Expand Down Expand Up @@ -59,6 +60,7 @@ export function useEntityForm({
apiOptions,
}: UseEntityFormArgs): UseEntityFormResult {
const queryClient = useQueryClient();
const getToken = useAcquireToken();
const [state, setState] = useState<EntityFormState>("idle");
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
const [successMessage, setSuccessMessage] = useState<string | undefined>(undefined);
Expand All @@ -71,6 +73,17 @@ export function useEntityForm({
setState("submitting");
setErrorMessage(undefined);

// Acquire an access token if the caller didn't already supply one in
// apiOptions. The registry create + update endpoints require a bearer
// token (FR-037 AuthN-only); without this the form silently 401s.
let resolvedApiOptions = apiOptions;
if (!resolvedApiOptions?.accessToken) {
const token = await getToken();
if (token) {
resolvedApiOptions = { ...(apiOptions ?? {}), accessToken: token };
}
}

const payload = {
id,
entityType,
Expand All @@ -87,7 +100,7 @@ export function useEntityForm({

try {
if (mode === "create") {
const result = await createEntity(payload as RegistryEntityCreateRequest, apiOptions);
const result = await createEntity(payload as RegistryEntityCreateRequest, resolvedApiOptions);
await queryClient.invalidateQueries({ queryKey: registryQueryKeys.entities.all });
// T125 / FR-033 — the new entity's Created audit event should be visible
// immediately on its detail page (quickstart §7 expectation).
Expand All @@ -109,7 +122,7 @@ export function useEntityForm({
id,
updateBody,
persistedEtag,
apiOptions,
resolvedApiOptions,
);
if (!result.ok) {
setConflict(result.conflict);
Expand Down
Loading