Add authentication checks to StoryEditorClient and ComicCreationForm

This commit is contained in:
Riccardo Giorato
2026-01-06 13:25:09 +01:00
parent 6adde0ed17
commit dfcfe4d394
2 changed files with 35 additions and 3 deletions
@@ -4,6 +4,7 @@ import { useState, useEffect } from "react";
import { useParams } from "next/navigation"; import { useParams } from "next/navigation";
import { useToast } from "@/hooks/use-toast"; import { useToast } from "@/hooks/use-toast";
import { useApiKey } from "@/hooks/use-api-key"; import { useApiKey } from "@/hooks/use-api-key";
import { useAuth } from "@clerk/nextjs";
import { EditorToolbar } from "@/components/editor/editor-toolbar"; import { EditorToolbar } from "@/components/editor/editor-toolbar";
import { PageSidebar } from "@/components/editor/page-sidebar"; import { PageSidebar } from "@/components/editor/page-sidebar";
import { ComicCanvas } from "@/components/editor/comic-canvas"; import { ComicCanvas } from "@/components/editor/comic-canvas";
@@ -45,6 +46,7 @@ interface StoryData {
export function StoryEditorClient() { export function StoryEditorClient() {
const params = useParams(); const params = useParams();
const slug = params.storySlug as string; const slug = params.storySlug as string;
const { isSignedIn, isLoaded } = useAuth();
const [story, setStory] = useState<StoryData | null>(null); const [story, setStory] = useState<StoryData | null>(null);
const [isOwner, setIsOwner] = useState<boolean>(false); const [isOwner, setIsOwner] = useState<boolean>(false);
@@ -161,6 +163,9 @@ export function StoryEditorClient() {
}, [pages.length]); }, [pages.length]);
const handleAddPage = () => { const handleAddPage = () => {
if (!isLoaded || !isSignedIn) {
return;
}
if (!apiKey && pages.length >= 1) { if (!apiKey && pages.length >= 1) {
setShowApiModal(true); setShowApiModal(true);
return; return;
@@ -169,6 +174,9 @@ export function StoryEditorClient() {
}; };
const handleRedrawPage = () => { const handleRedrawPage = () => {
if (!isLoaded || !isSignedIn) {
return;
}
if (!apiKey) { if (!apiKey) {
setShowApiModal(true); setShowApiModal(true);
return; return;
+26 -2
View File
@@ -6,7 +6,7 @@ import { Upload, X, Check, ArrowRight, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useToast } from "@/hooks/use-toast"; import { useToast } from "@/hooks/use-toast";
import { useS3Upload } from "next-s3-upload"; import { useS3Upload } from "next-s3-upload";
import { useAuth, SignInButton } from "@clerk/nextjs"; import { useAuth, SignInButton, useClerk } from "@clerk/nextjs";
import { COMIC_STYLES } from "@/lib/constants"; import { COMIC_STYLES } from "@/lib/constants";
import { useKeyboardShortcut } from "@/hooks/use-keyboard-shortcut"; import { useKeyboardShortcut } from "@/hooks/use-keyboard-shortcut";
import { useApiKey } from "@/hooks/use-api-key"; import { useApiKey } from "@/hooks/use-api-key";
@@ -38,6 +38,7 @@ export function ComicCreationForm({
const { toast } = useToast(); const { toast } = useToast();
const { uploadToS3 } = useS3Upload(); const { uploadToS3 } = useS3Upload();
const { isSignedIn, isLoaded } = useAuth(); const { isSignedIn, isLoaded } = useAuth();
const { openSignIn } = useClerk();
const [apiKey] = useApiKey(); const [apiKey] = useApiKey();
const hasApiKey = !!apiKey; const hasApiKey = !!apiKey;
const [previews, setPreviews] = useState<string[]>([]); const [previews, setPreviews] = useState<string[]>([]);
@@ -46,6 +47,8 @@ export function ComicCreationForm({
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null); const textareaRef = useRef<HTMLTextAreaElement>(null);
const PROMPT_STORAGE_KEY = 'comic-prompt-draft';
useEffect(() => { useEffect(() => {
if (isLoading) { if (isLoading) {
@@ -60,12 +63,31 @@ export function ComicCreationForm({
} }
}, []); }, []);
// Persist prompt to localStorage
useEffect(() => {
if (prompt) {
localStorage.setItem(PROMPT_STORAGE_KEY, prompt);
}
}, [prompt]);
// Restore prompt from localStorage only once on mount
useEffect(() => {
const saved = localStorage.getItem(PROMPT_STORAGE_KEY);
if (saved && !prompt) {
setPrompt(saved);
}
}, []); // Run only on mount
// Keyboard shortcut for form submission // Keyboard shortcut for form submission
useKeyboardShortcut(() => { useKeyboardShortcut(() => {
if (!isLoading && prompt.trim()) { if (!isLoading && prompt.trim()) {
if (!isSignedIn) {
openSignIn();
} else {
handleCreate(); handleCreate();
} }
}, { disabled: isLoading }); }
}, { disabled: isLoading || !isLoaded });
const handleFiles = (newFiles: FileList | null) => { const handleFiles = (newFiles: FileList | null) => {
if (!newFiles) return; if (!newFiles) return;
@@ -168,6 +190,8 @@ export function ComicCreationForm({
const result = await response.json(); const result = await response.json();
// Clear the draft since submission was successful
localStorage.removeItem(PROMPT_STORAGE_KEY);
// Redirect to the story editor using slug // Redirect to the story editor using slug
router.push(`/story/${result.storySlug}`); router.push(`/story/${result.storySlug}`);
} catch (error) { } catch (error) {