viktor commited on
Commit
fa8d078
·
1 Parent(s): 8bc05ba

feat. Check if user authentication occurs when the “submit” or “improved prompt” button is clicked.

Browse files
Files changed (2) hide show
  1. src/components/prompt-input.tsx +19 -0
  2. src/lib/auth.ts +20 -0
src/components/prompt-input.tsx CHANGED
@@ -10,6 +10,7 @@ import { ColorPanel } from "./color-panel"
10
  import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
11
  import { FullscreenToggle } from "./ui/fullscreen-toggle"
12
  import { AuthErrorPopup } from "./auth-error-popup"
 
13
 
14
  interface PromptInputProps {
15
  onSubmit: (prompt: string, colors: string[]) => Promise<void>;
@@ -43,10 +44,24 @@ export function PromptInput({
43
  }
44
  }, [improveError, onImproveError]);
45
 
 
 
 
 
 
 
 
 
 
 
46
  const handleSubmit = async (e: React.FormEvent) => {
47
  e.preventDefault();
48
  if (prompt.trim() === '' || isLoading) return;
49
 
 
 
 
 
50
  // Clear any previous errors
51
  setImproveError(null);
52
  await onSubmit(prompt, selectedColors);
@@ -55,6 +70,10 @@ export function PromptInput({
55
  const improvePrompt = async () => {
56
  if (prompt.trim() === '' || isImprovingPrompt || isLoading) return;
57
 
 
 
 
 
58
  // Clear previous errors
59
  setImproveError(null);
60
  setShowAuthError(false);
 
10
  import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
11
  import { FullscreenToggle } from "./ui/fullscreen-toggle"
12
  import { AuthErrorPopup } from "./auth-error-popup"
13
+ import { getInferenceToken } from "@/lib/auth"
14
 
15
  interface PromptInputProps {
16
  onSubmit: (prompt: string, colors: string[]) => Promise<void>;
 
44
  }
45
  }, [improveError, onImproveError]);
46
 
47
+ const checkAuth = async (): Promise<boolean> => {
48
+ try {
49
+ const token = await getInferenceToken();
50
+ return !!token;
51
+ } catch (error) {
52
+ setShowAuthError(true);
53
+ return false;
54
+ }
55
+ }
56
+
57
  const handleSubmit = async (e: React.FormEvent) => {
58
  e.preventDefault();
59
  if (prompt.trim() === '' || isLoading) return;
60
 
61
+ // Check for authentication
62
+ const isAuthenticated = await checkAuth();
63
+ if (!isAuthenticated) return;
64
+
65
  // Clear any previous errors
66
  setImproveError(null);
67
  await onSubmit(prompt, selectedColors);
 
70
  const improvePrompt = async () => {
71
  if (prompt.trim() === '' || isImprovingPrompt || isLoading) return;
72
 
73
+ // Check for authentication
74
+ const isAuthenticated = await checkAuth();
75
+ if (!isAuthenticated) return;
76
+
77
  // Clear previous errors
78
  setImproveError(null);
79
  setShowAuthError(false);
src/lib/auth.ts ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Client-side authentication utilities
3
+ */
4
+
5
+ /**
6
+ * Get the inference token from cookies
7
+ * @returns Promise that resolves to the token string or throws an error if no token is found
8
+ */
9
+ export async function getInferenceToken(): Promise<string> {
10
+ // Get token from cookie
11
+ const cookies = document.cookie.split('; ');
12
+ const tokenCookie = cookies.find(cookie => cookie.startsWith('hf_token='));
13
+ const token = tokenCookie?.split('=')[1];
14
+
15
+ if (!token) {
16
+ throw new Error('Authentication required');
17
+ }
18
+
19
+ return token;
20
+ }