Public read-only share link (no login / no OIDC) (#41) (#43)
Build image / build-and-push (push) Successful in 8s
Build image / build-and-push (push) Successful in 8s
Per-garden public read-only link: unauthenticated GET /api/v1/public/gardens/:token (no requireAuth, no OIDC), owner-only enable/rotate/disable, and a /g/$token page rendering GardenCanvas read-only. Review fixes: redact plant owner ids, Cache-Control: no-store, 400 on malformed body, shared resetTransient store action. Closes #41. Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #43.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// Public read-only garden data layer: the unauthenticated /full payload behind a
|
||||
// share token. Shares the FullGarden shape with the editor's own load, so the
|
||||
// public page can reuse toEditorObject / toEditorPlanting and GardenCanvas.
|
||||
|
||||
import { queryOptions, useQuery } from '@tanstack/react-query'
|
||||
import { api } from './api'
|
||||
import { fullGardenSchema, type FullGarden } from './objects'
|
||||
|
||||
export function publicGardenQueryOptions(token: string) {
|
||||
return queryOptions({
|
||||
queryKey: ['public-garden', token] as const,
|
||||
queryFn: async (): Promise<FullGarden> =>
|
||||
fullGardenSchema.parse(await api.get(`/public/gardens/${encodeURIComponent(token)}`)),
|
||||
// A disabled/rotated token is a 404, not a transient failure — don't retry.
|
||||
retry: false,
|
||||
staleTime: 30_000,
|
||||
})
|
||||
}
|
||||
|
||||
export function usePublicGarden(token: string) {
|
||||
return useQuery(publicGardenQueryOptions(token))
|
||||
}
|
||||
@@ -68,3 +68,49 @@ export function useRemoveShare(gardenId: number) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// --- public share link (owner-only management) -----------------------------
|
||||
// The read-only public link that anyone (no account) can open. token is present
|
||||
// only when enabled; the page builds the URL as `${origin}/g/${token}`.
|
||||
|
||||
export const shareLinkSchema = z.object({
|
||||
enabled: z.boolean(),
|
||||
token: z.string().optional(),
|
||||
})
|
||||
export type ShareLinkState = z.infer<typeof shareLinkSchema>
|
||||
|
||||
const shareLinkKey = (gardenId: number) => ['share-link', gardenId] as const
|
||||
|
||||
export function shareLinkQueryOptions(gardenId: number) {
|
||||
return queryOptions({
|
||||
queryKey: shareLinkKey(gardenId),
|
||||
queryFn: async (): Promise<ShareLinkState> =>
|
||||
shareLinkSchema.parse(await api.get(`/gardens/${gardenId}/share-link`)),
|
||||
})
|
||||
}
|
||||
|
||||
/** The garden's public-link state (owner-only endpoint). */
|
||||
export function useShareLink(gardenId: number) {
|
||||
return useQuery(shareLinkQueryOptions(gardenId))
|
||||
}
|
||||
|
||||
/** Enable the public link, or with { rotate: true } issue a fresh token that
|
||||
* invalidates the old URL. Writes the returned state straight into the cache. */
|
||||
export function useEnableShareLink(gardenId: number) {
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: async ({ rotate = false }: { rotate?: boolean }): Promise<ShareLinkState> =>
|
||||
shareLinkSchema.parse(await api.post(`/gardens/${gardenId}/share-link`, { rotate })),
|
||||
onSuccess: (state) => qc.setQueryData(shareLinkKey(gardenId), state),
|
||||
})
|
||||
}
|
||||
|
||||
/** Disable the public link. */
|
||||
export function useDisableShareLink(gardenId: number) {
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: async (): Promise<ShareLinkState> =>
|
||||
shareLinkSchema.parse(await api.delete(`/gardens/${gardenId}/share-link`)),
|
||||
onSuccess: (state) => qc.setQueryData(shareLinkKey(gardenId), state),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user