Gate the places query behind an authenticated user so the list is no longer public, and relax the place mutations from admin-only to any signed-in user. Hide the Places tab and skip the fetch entirely for anonymous visitors. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UKvnE448c1FeepmxZkTezz
This commit is contained in:
@@ -15,8 +15,8 @@ import (
|
|||||||
|
|
||||||
// CreatePlace is the resolver for the createPlace field.
|
// CreatePlace is the resolver for the createPlace field.
|
||||||
func (r *mutationResolver) CreatePlace(ctx context.Context, input model.CreatePlaceInput) (*models.Place, error) {
|
func (r *mutationResolver) CreatePlace(ctx context.Context, input model.CreatePlaceInput) (*models.Place, error) {
|
||||||
if !IsAdminFromCtx(ctx) {
|
if _, ok := UserIDFromCtx(ctx); !ok {
|
||||||
return nil, fmt.Errorf("admin access required")
|
return nil, fmt.Errorf("authentication required")
|
||||||
}
|
}
|
||||||
place := models.Place{
|
place := models.Place{
|
||||||
Title: input.Title,
|
Title: input.Title,
|
||||||
@@ -40,8 +40,8 @@ func (r *mutationResolver) CreatePlace(ctx context.Context, input model.CreatePl
|
|||||||
|
|
||||||
// UpdatePlace is the resolver for the updatePlace field.
|
// UpdatePlace is the resolver for the updatePlace field.
|
||||||
func (r *mutationResolver) UpdatePlace(ctx context.Context, id int, input model.UpdatePlaceInput) (*models.Place, error) {
|
func (r *mutationResolver) UpdatePlace(ctx context.Context, id int, input model.UpdatePlaceInput) (*models.Place, error) {
|
||||||
if !IsAdminFromCtx(ctx) {
|
if _, ok := UserIDFromCtx(ctx); !ok {
|
||||||
return nil, fmt.Errorf("admin access required")
|
return nil, fmt.Errorf("authentication required")
|
||||||
}
|
}
|
||||||
var place models.Place
|
var place models.Place
|
||||||
if err := r.Store.DB.First(&place, id).Error; err != nil {
|
if err := r.Store.DB.First(&place, id).Error; err != nil {
|
||||||
@@ -79,8 +79,8 @@ func (r *mutationResolver) UpdatePlace(ctx context.Context, id int, input model.
|
|||||||
|
|
||||||
// DeletePlace is the resolver for the deletePlace field.
|
// DeletePlace is the resolver for the deletePlace field.
|
||||||
func (r *mutationResolver) DeletePlace(ctx context.Context, id int) (*models.Place, error) {
|
func (r *mutationResolver) DeletePlace(ctx context.Context, id int) (*models.Place, error) {
|
||||||
if !IsAdminFromCtx(ctx) {
|
if _, ok := UserIDFromCtx(ctx); !ok {
|
||||||
return nil, fmt.Errorf("admin access required")
|
return nil, fmt.Errorf("authentication required")
|
||||||
}
|
}
|
||||||
var place models.Place
|
var place models.Place
|
||||||
if err := r.Store.DB.First(&place, id).Error; err != nil {
|
if err := r.Store.DB.First(&place, id).Error; err != nil {
|
||||||
@@ -99,6 +99,9 @@ func (r *placeResolver) ID(ctx context.Context, obj *models.Place) (int, error)
|
|||||||
|
|
||||||
// Places is the resolver for the places field.
|
// Places is the resolver for the places field.
|
||||||
func (r *queryResolver) Places(ctx context.Context) ([]*models.Place, error) {
|
func (r *queryResolver) Places(ctx context.Context) ([]*models.Place, error) {
|
||||||
|
if _, ok := UserIDFromCtx(ctx); !ok {
|
||||||
|
return nil, fmt.Errorf("authentication required")
|
||||||
|
}
|
||||||
var places []models.Place
|
var places []models.Place
|
||||||
if err := r.Store.DB.Order("done ASC, priority DESC, created_at DESC").Find(&places).Error; err != nil {
|
if err := r.Store.DB.Order("done ASC, priority DESC, created_at DESC").Find(&places).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, computed } from "vue";
|
import { ref, computed, watch } from "vue";
|
||||||
import Slideshow from "@/components/util/Slideshow.vue";
|
import Slideshow from "@/components/util/Slideshow.vue";
|
||||||
import Modal from "@/components/util/Modal.vue";
|
import Modal from "@/components/util/Modal.vue";
|
||||||
import PlacesTable from "./PlacesTable.vue";
|
import PlacesTable from "./PlacesTable.vue";
|
||||||
@@ -8,16 +8,20 @@ import { useAuthStore } from "@/stores/auth";
|
|||||||
|
|
||||||
const images = [{ url: "/img/memes/pidgeon.gif", comment: "鸟" }];
|
const images = [{ url: "/img/memes/pidgeon.gif", comment: "鸟" }];
|
||||||
|
|
||||||
const tabs = [
|
const auth = useAuthStore();
|
||||||
|
const loggedIn = computed(() => auth.loggedIn);
|
||||||
|
|
||||||
|
const tabs = computed(() => [
|
||||||
{ id: "collage", label: "鸟" },
|
{ id: "collage", label: "鸟" },
|
||||||
{ id: "places", label: "Places" },
|
...(loggedIn.value ? [{ id: "places", label: "Places" }] : []),
|
||||||
];
|
]);
|
||||||
|
|
||||||
const activeTab = ref("collage");
|
const activeTab = ref("collage");
|
||||||
const showAdd = ref(false);
|
const showAdd = ref(false);
|
||||||
|
|
||||||
const auth = useAuthStore();
|
watch(loggedIn, (isIn) => {
|
||||||
const isAdmin = computed(() => !!auth.user?.admin);
|
if (!isIn && activeTab.value === "places") activeTab.value = "collage";
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -34,7 +38,7 @@ const isAdmin = computed(() => !!auth.user?.admin);
|
|||||||
</button>
|
</button>
|
||||||
<div class="tab-spacer" />
|
<div class="tab-spacer" />
|
||||||
<button
|
<button
|
||||||
v-if="isAdmin && activeTab === 'places'"
|
v-if="loggedIn && activeTab === 'places'"
|
||||||
class="tab add-btn"
|
class="tab add-btn"
|
||||||
title="Add a place / thing to do"
|
title="Add a place / thing to do"
|
||||||
@click="showAdd = true"
|
@click="showAdd = true"
|
||||||
@@ -45,7 +49,7 @@ const isAdmin = computed(() => !!auth.user?.admin);
|
|||||||
|
|
||||||
<div class="tab-body">
|
<div class="tab-body">
|
||||||
<Slideshow v-if="activeTab === 'collage'" :images="images" />
|
<Slideshow v-if="activeTab === 'collage'" :images="images" />
|
||||||
<PlacesTable v-else />
|
<PlacesTable v-else-if="loggedIn" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Modal v-model="showAdd">
|
<Modal v-model="showAdd">
|
||||||
|
|||||||
@@ -1,18 +1,22 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { computed, onMounted } from "vue";
|
import { computed, onMounted, watch } from "vue";
|
||||||
import { usePlacesStore } from "@/stores/places";
|
import { usePlacesStore } from "@/stores/places";
|
||||||
import { useAuthStore } from "@/stores/auth";
|
import { useAuthStore } from "@/stores/auth";
|
||||||
|
|
||||||
const places = usePlacesStore();
|
const places = usePlacesStore();
|
||||||
const auth = useAuthStore();
|
const auth = useAuthStore();
|
||||||
const isAdmin = computed(() => !!auth.user?.admin);
|
const loggedIn = computed(() => auth.loggedIn);
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
if (!places.loaded && !places.loading) places.fetch();
|
if (loggedIn.value && !places.loaded && !places.loading) places.fetch();
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(loggedIn, (isIn) => {
|
||||||
|
if (isIn && !places.loaded && !places.loading) places.fetch();
|
||||||
});
|
});
|
||||||
|
|
||||||
async function toggleDone(place) {
|
async function toggleDone(place) {
|
||||||
if (!isAdmin.value) return;
|
if (!loggedIn.value) return;
|
||||||
try {
|
try {
|
||||||
await places.toggleDone(place);
|
await places.toggleDone(place);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -21,7 +25,7 @@ async function toggleDone(place) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function remove(place) {
|
async function remove(place) {
|
||||||
if (!isAdmin.value) return;
|
if (!loggedIn.value) return;
|
||||||
if (!confirm(`Delete "${place.title}"?`)) return;
|
if (!confirm(`Delete "${place.title}"?`)) return;
|
||||||
try {
|
try {
|
||||||
await places.remove(place.id);
|
await places.remove(place.id);
|
||||||
@@ -49,7 +53,7 @@ async function remove(place) {
|
|||||||
<th class="col-loc">Where</th>
|
<th class="col-loc">Where</th>
|
||||||
<th class="col-cat">Type</th>
|
<th class="col-cat">Type</th>
|
||||||
<th class="col-cost">Cost</th>
|
<th class="col-cost">Cost</th>
|
||||||
<th v-if="isAdmin" class="col-act"></th>
|
<th v-if="loggedIn" class="col-act"></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -61,7 +65,7 @@ async function remove(place) {
|
|||||||
<td>
|
<td>
|
||||||
<div class="title-cell">
|
<div class="title-cell">
|
||||||
<button
|
<button
|
||||||
v-if="isAdmin"
|
v-if="loggedIn"
|
||||||
class="done-toggle"
|
class="done-toggle"
|
||||||
:title="place.done ? 'Mark as pending' : 'Mark as done'"
|
:title="place.done ? 'Mark as pending' : 'Mark as done'"
|
||||||
@click="toggleDone(place)"
|
@click="toggleDone(place)"
|
||||||
@@ -75,7 +79,7 @@ async function remove(place) {
|
|||||||
<td class="col-loc">{{ place.location || "—" }}</td>
|
<td class="col-loc">{{ place.location || "—" }}</td>
|
||||||
<td class="col-cat">{{ place.category || "—" }}</td>
|
<td class="col-cat">{{ place.category || "—" }}</td>
|
||||||
<td class="col-cost">{{ place.cost || "—" }}</td>
|
<td class="col-cost">{{ place.cost || "—" }}</td>
|
||||||
<td v-if="isAdmin" class="col-act">
|
<td v-if="loggedIn" class="col-act">
|
||||||
<button class="row-action" @click="remove(place)" title="Delete">
|
<button class="row-action" @click="remove(place)" title="Delete">
|
||||||
×
|
×
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user