diff --git a/app/api/stories/[storySlug]/route.ts b/app/api/stories/[storySlug]/route.ts index fcc9984..63e633e 100644 --- a/app/api/stories/[storySlug]/route.ts +++ b/app/api/stories/[storySlug]/route.ts @@ -10,19 +10,23 @@ export async function GET( { params }: { params: Promise<{ storySlug: string }> } ) { try { - const { userId } = await auth(); + const authResult = await auth(); + const { userId } = authResult; - if (!userId) { - return NextResponse.json( - { error: "Authentication required" }, - { status: 401 } - ); - } + console.log('API: auth result:', authResult); + console.log('API: userId type:', typeof userId, 'value:', userId); + console.log('API: timestamp:', new Date().toISOString()); const { storySlug: slug } = await params; // Special case: if slug is "all", return user's stories for debugging if (slug === "all") { + if (!userId) { + return NextResponse.json( + { error: "Authentication required for this endpoint" }, + { status: 401 } + ); + } const userStories = await db.select().from(stories).where(eq(stories.userId, userId)); return NextResponse.json({ message: "User stories", @@ -47,14 +51,14 @@ export async function GET( } // Check if the story belongs to the authenticated user - if (result.story.userId !== userId) { - return NextResponse.json( - { error: "Access denied" }, - { status: 403 } - ); - } + const isOwner = userId ? result.story.userId === userId : false; - return NextResponse.json(result); + // Return the story data with ownership information + const responseData = { + ...result, + isOwner, + }; + return NextResponse.json(responseData); } catch (error) { console.error("Error fetching story:", error); return NextResponse.json( diff --git a/app/editor/[storySlug]/page.tsx b/app/editor/[storySlug]/page.tsx index aefdfff..e833f7e 100644 --- a/app/editor/[storySlug]/page.tsx +++ b/app/editor/[storySlug]/page.tsx @@ -28,6 +28,7 @@ interface StoryData { description?: string | null; style: string; userId?: string | null; + isOwner?: boolean; } export default function StoryEditorPage() { @@ -35,6 +36,7 @@ export default function StoryEditorPage() { const slug = params.storySlug as string; const [story, setStory] = useState(null); + const [isOwner, setIsOwner] = useState(false); const [pages, setPages] = useState([]); const [currentPage, setCurrentPage] = useState(0); const [showApiModal, setShowApiModal] = useState(false); @@ -57,9 +59,18 @@ export default function StoryEditorPage() { } const result = await response.json(); - const { story: storyData, pages: pagesData } = result; + console.log("Editor: full API response:", result); + + const { + story: storyData, + pages: pagesData, + isOwner: ownerStatus, + } = result; + + console.log("Editor: received story data:", storyData); setStory(storyData); + setIsOwner(ownerStatus ?? false); // Default to false if undefined setPages( pagesData.map((page: any) => ({ id: page.pageNumber, @@ -157,9 +168,7 @@ export default function StoryEditorPage() { // Update the current page with the new image setPages((prevPages) => prevPages.map((page, index) => - index === currentPage - ? { ...page, image: result.imageUrl } - : page + index === currentPage ? { ...page, image: result.imageUrl } : page ) ); @@ -172,7 +181,8 @@ export default function StoryEditorPage() { console.error("Error redrawing page:", error); toast({ title: "Failed to redraw page", - description: error instanceof Error ? error.message : "Failed to redraw page", + description: + error instanceof Error ? error.message : "Failed to redraw page", variant: "destructive", duration: 4000, }); @@ -247,9 +257,7 @@ export default function StoryEditorPage() { toast({ title: "Failed to generate page", description: - error instanceof Error - ? error.message - : "Failed to generate page", + error instanceof Error ? error.message : "Failed to generate page", variant: "destructive", duration: 4000, }); @@ -274,7 +282,11 @@ export default function StoryEditorPage() { return (
- +
setShowInfoSheet(true)} onRedrawClick={handleRedrawPage} onNextPage={() => diff --git a/components/editor/comic-canvas.tsx b/components/editor/comic-canvas.tsx index d3679e0..97c5b0f 100644 --- a/components/editor/comic-canvas.tsx +++ b/components/editor/comic-canvas.tsx @@ -1,6 +1,7 @@ "use client"; -import { RefreshCw, Download, Info, Loader2 } from "lucide-react"; +import { RefreshCw, Share, Info, Loader2 } from "lucide-react"; +import { useToast } from "@/hooks/use-toast"; import { Button } from "@/components/ui/button"; interface PageData { @@ -17,13 +18,25 @@ interface ComicCanvasProps { page: PageData; pageIndex: number; isLoading?: boolean; + isOwner?: boolean; onInfoClick?: () => void; onRedrawClick?: () => void; onNextPage?: () => void; onPrevPage?: () => void; } -export function ComicCanvas({ page, pageIndex, isLoading = false, onInfoClick, onRedrawClick, onNextPage, onPrevPage }: ComicCanvasProps) { +export function ComicCanvas({ + page, + pageIndex, + isLoading = false, + isOwner = true, + onInfoClick, + onRedrawClick, + onNextPage, + onPrevPage, +}: ComicCanvasProps) { + const { toast } = useToast(); + return (
{/* Dot grid background */} @@ -77,29 +90,10 @@ export function ComicCanvas({ page, pageIndex, isLoading = false, onInfoClick, o )} - -
- -
- {/*
Page {page.id}
*/} - - {/* Mobile action buttons */} -
+ {isOwner && ( + )} +
+ +
+ {/*
Page {page.id}
*/} + + {/* Mobile action buttons */} +
+ {isOwner && ( + + )}
diff --git a/components/editor/editor-toolbar.tsx b/components/editor/editor-toolbar.tsx index bc8942f..d6ae512 100644 --- a/components/editor/editor-toolbar.tsx +++ b/components/editor/editor-toolbar.tsx @@ -1,19 +1,23 @@ "use client"; -import { ArrowLeft, RefreshCw, Download, Plus, Info } from "lucide-react"; +import { ArrowLeft, RefreshCw, Share, Plus, Info } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useRouter } from "next/navigation"; +import { useToast } from "@/hooks/use-toast"; interface EditorToolbarProps { title: string; onContinueStory?: () => void; + isOwner?: boolean; } export function EditorToolbar({ title, onContinueStory, + isOwner = true, }: EditorToolbarProps) { const router = useRouter(); + const { toast } = useToast(); return (
@@ -21,7 +25,7 @@ export function EditorToolbar({
-
- +
+ - -
+ {isOwner && onContinueStory && ( + + )} +
); } diff --git a/components/editor/page-sidebar.tsx b/components/editor/page-sidebar.tsx index b73b310..88c4baa 100644 --- a/components/editor/page-sidebar.tsx +++ b/components/editor/page-sidebar.tsx @@ -1,25 +1,26 @@ -"use client" +"use client"; -import { Plus, Loader2, Key } from "lucide-react" -import { Button } from "@/components/ui/button" -import { UserButton } from "@clerk/nextjs" +import { Plus, Loader2, Key } from "lucide-react"; +import { Button } from "@/components/ui/button"; +import { UserButton, SignedIn } from "@clerk/nextjs"; interface PageData { - id: number - title: string - image: string - prompt: string - characterUpload?: string - style: string + id: number; + title: string; + image: string; + prompt: string; + characterUpload?: string; + style: string; } interface PageSidebarProps { - pages: PageData[] - currentPage: number - onPageSelect: (index: number) => void - onAddPage: () => void - loadingPageId?: number | null - onApiKeyClick?: () => void + pages: PageData[]; + currentPage: number; + onPageSelect: (index: number) => void; + onAddPage: () => void; + loadingPageId?: number | null; + onApiKeyClick?: () => void; + isOwner?: boolean; } export function PageSidebar({ @@ -29,6 +30,7 @@ export function PageSidebar({ onAddPage, loadingPageId, onApiKeyClick, + isOwner = true, }: PageSidebarProps) { return (
@@ -95,16 +101,18 @@ export function PageSidebar({ -
- -
+ +
+ +
+
- ) + ); }