Files
pc_builder/inertia/pages/widgets/ComputerShareModal.vue
T
Kevin L'Hereec da71d33450
Build & Deploy / build (push) Failing after 5s
Build & Deploy / docker (push) Skipped
feat: add public read-only sharing for computers
Adds the ability for an owner to share a computer build via a public,
unauthenticated link, inspired by a feature request from the original
pcbuilder repo (no such feature actually existed there — implemented
from scratch here).

- New columns on computers: share_token (unique, nullable), share_enabled,
  share_show_sell_price, share_show_cost
- computer_share_service.ts: generates a non-guessable share token
  (crypto.randomBytes, base64url), enables/disables sharing, and supports
  rotating the token to invalidate previously distributed links
- ComputerShareController (authenticated, owner-scoped like the rest of
  the app): PUT /computers/:id/share to update settings, POST
  /computers/:id/share/rotate to rotate the token
- PublicComputerController + GET /share/:token (outside any auth/guest
  group, reachable anonymously): resolves only computers with
  share_enabled = true, 404 otherwise
- ComputerPublicTransformer: dedicated read-only projection that never
  leaks userId, and only includes sellPrice/cost when the owner opted in
  via share_show_sell_price / share_show_cost — everything else (title,
  scores, state, components, pictures) is always visible on a shared link
- ComputerShareModal.vue: owner-facing UI (toggle sharing, choose which
  prices to expose, copy the public link, revoke/rotate it) wired into
  computers/show.vue via a new 'Share' button
- share/show.vue: public page rendered with EmptyLayout (no authenticated
  header/sidebar)
- Boolean columns use a  cast since MySQL returns tinyint(1),
  not native booleans, through Lucid
- 6 new functional tests covering: enabling sharing, price visibility
  toggles, disabling sharing revokes the link, cross-user protection on
  the share settings endpoint (404), token rotation invalidating the old
  link, and anonymous access to a shared computer
2026-07-23 11:43:38 +02:00

112 lines
3.4 KiB
Vue

<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { useForm } from '@inertiajs/vue3'
import type { Computer } from '~/pages/types/UITypes'
const props = defineProps<{
show: boolean
computer: Computer
}>()
const emit = defineEmits(['close'])
const form = reactive({
enabled: props.computer.shareEnabled,
showSellPrice: props.computer.shareShowSellPrice,
showCost: props.computer.shareShowCost,
})
const copied = ref(false)
const shareUrl = computed(() => {
if (!props.computer.shareToken) return ''
return `${window.location.origin}/share/${props.computer.shareToken}`
})
function submit() {
useForm(form).put(`/computers/${props.computer.id}/share`, {
preserveScroll: true,
onSuccess: () => emit('close'),
})
}
function rotateToken() {
useForm({}).post(`/computers/${props.computer.id}/share/rotate`, {
preserveScroll: true,
})
}
async function copyLink() {
await navigator.clipboard.writeText(shareUrl.value)
copied.value = true
setTimeout(() => (copied.value = false), 2000)
}
</script>
<template>
<div v-if="show" class="fixed inset-0 flex items-center justify-center z-50">
<div class="fixed inset-0 bg-black opacity-50" @click="emit('close')"></div>
<div class="bg-white rounded-lg p-6 z-10 w-full max-w-md mx-4">
<h2 class="text-lg font-semibold mb-4 text-gray-800">Share this build</h2>
<label class="flex items-center gap-2 mb-4 cursor-pointer">
<input v-model="form.enabled" type="checkbox" class="w-4 h-4" />
<span class="text-sm font-medium text-gray-700">Enable public link</span>
</label>
<div v-if="form.enabled" class="space-y-3 mb-4 pl-6">
<label class="flex items-center gap-2 cursor-pointer">
<input v-model="form.showSellPrice" type="checkbox" class="w-4 h-4" />
<span class="text-sm text-gray-700">Show sell price</span>
</label>
<label class="flex items-center gap-2 cursor-pointer">
<input v-model="form.showCost" type="checkbox" class="w-4 h-4" />
<span class="text-sm text-gray-700">Show components cost</span>
</label>
</div>
<div v-if="computer.shareEnabled && computer.shareToken" class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2">Public link</label>
<div class="flex gap-2">
<input
:value="shareUrl"
readonly
class="flex-1 border-gray-200 px-3 py-2 border rounded text-gray-700 text-sm bg-gray-50"
/>
<button
type="button"
@click="copyLink"
class="px-3 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300 text-sm shrink-0"
>
{{ copied ? 'Copied!' : 'Copy' }}
</button>
</div>
<button
type="button"
@click="rotateToken"
class="mt-2 text-xs text-red-600 hover:text-red-800 underline"
>
Revoke and generate a new link
</button>
</div>
<div class="flex justify-end space-x-2 mt-6">
<button
type="button"
@click="submit"
class="px-4 py-2 bg-indigo-600 text-white rounded hover:bg-indigo-700"
>
Save
</button>
<button
type="button"
@click="emit('close')"
class="px-4 py-2 bg-gray-300 text-gray-700 rounded hover:bg-gray-400"
>
Close
</button>
</div>
</div>
</div>
</template>