A per-garden public link anyone can open read-only, with no login and no OIDC. Backend: - Migration 0004 adds gardens.public_token (nullable, unique over non-NULL). - Owner-only management: GET/POST/DELETE /gardens/:id/share-link (state / enable-or-rotate / disable). The token is stored raw (it must be shown back to the owner) and never selected into the normal garden payload. - Unauthenticated GET /api/v1/public/gardens/:token returns the read-only /full payload — the token is the capability, so no requireAuth and no redirect. The public view drops MyRole and OwnerID to minimize what an anonymous viewer learns. Unknown/rotated/disabled tokens are masked as 404. - assembleFull is extracted from GardenFull so both reads return one shape. Frontend: - /g/$token route with NO auth guard (SPA fallback already serves it), rendering GardenCanvas read-only via usePublicGarden. - Share dialog gains a Public link section: create, copy, regenerate, turn off. Tests: service lifecycle + ACL (owner-only, non-owner masked, rotate/disable invalidation) and the HTTP surface incl. the cookieless public read. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
117 lines
4.2 KiB
TypeScript
117 lines
4.2 KiB
TypeScript
// Garden sharing data layer: zod shapes for /gardens/:id/shares plus the
|
|
// react-query hooks the share dialog and "leave garden" action use.
|
|
|
|
import { queryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { z } from 'zod'
|
|
import { api } from './api'
|
|
|
|
export const shareRoleSchema = z.enum(['viewer', 'editor'])
|
|
export type ShareRole = z.infer<typeof shareRoleSchema>
|
|
|
|
export const shareSchema = z.object({
|
|
id: z.number(),
|
|
gardenId: z.number(),
|
|
userId: z.number(),
|
|
role: shareRoleSchema,
|
|
createdBy: z.number(),
|
|
version: z.number(),
|
|
createdAt: z.string(),
|
|
updatedAt: z.string(),
|
|
email: z.string(),
|
|
displayName: z.string(),
|
|
})
|
|
export type Share = z.infer<typeof shareSchema>
|
|
|
|
const sharesKey = (gardenId: number) => ['shares', gardenId] as const
|
|
|
|
export function sharesQueryOptions(gardenId: number) {
|
|
return queryOptions({
|
|
queryKey: sharesKey(gardenId),
|
|
queryFn: async (): Promise<Share[]> => z.array(shareSchema).parse(await api.get(`/gardens/${gardenId}/shares`)),
|
|
})
|
|
}
|
|
|
|
/** The shares on a garden (owner-only endpoint). */
|
|
export function useShares(gardenId: number) {
|
|
return useQuery(sharesQueryOptions(gardenId))
|
|
}
|
|
|
|
// The add/update responses are a bare GardenShare (no email/displayName), so we
|
|
// don't parse them into the list's Share shape; onSuccess refetches the list.
|
|
export function useAddShare(gardenId: number) {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (input: { email: string; role: ShareRole }) => api.post(`/gardens/${gardenId}/shares`, input),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: sharesKey(gardenId) }),
|
|
})
|
|
}
|
|
|
|
export function useUpdateShareRole(gardenId: number) {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ userId, role }: { userId: number; role: ShareRole }) =>
|
|
api.patch(`/gardens/${gardenId}/shares/${userId}`, { role }),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: sharesKey(gardenId) }),
|
|
})
|
|
}
|
|
|
|
/** Remove a share. Used both by the owner (revoke) and by a recipient leaving a
|
|
* garden (their own userId). Refreshes the garden list too, since a leave
|
|
* changes what the current user can see. */
|
|
export function useRemoveShare(gardenId: number) {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (userId: number) => api.delete(`/gardens/${gardenId}/shares/${userId}`),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: sharesKey(gardenId) })
|
|
qc.invalidateQueries({ queryKey: ['gardens'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// --- 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),
|
|
})
|
|
}
|