Spaces:
Runtime error
Runtime error
Download apps/web/src/components/AuthPanel.tsx from Oheha/cybully-api: direct link, hf CLI and curl.
- Browser
- Download file 7.44 kB
-
https://huggingface.co/spaces/Oheha/cybully-api/resolve/main/apps/web/src/components/AuthPanel.tsx
- Command line
-
hf download hf://spaces/Oheha/cybully-api/apps/web/src/components/AuthPanel.tsx
-
curl -L -o AuthPanel.tsx https://huggingface.co/spaces/Oheha/cybully-api/resolve/main/apps/web/src/components/AuthPanel.tsx
7.44 kB
| "use client"; | |
| import Link from "next/link"; | |
| import { useRouter } from "next/navigation"; | |
| import { useState } from "react"; | |
| import { HugeiconsIcon } from "@hugeicons/react"; | |
| import { CheckmarkCircle01Icon, Login01Icon } from "@hugeicons/core-free-icons"; | |
| import { BrandLogo } from "@/components/BrandLogo"; | |
| import { createClient } from "@/utils/supabase/client"; | |
| type AuthPanelProps = { | |
| mode: "sign-in" | "sign-up"; | |
| appName: string; | |
| }; | |
| function validateEmail(email: string): boolean { | |
| return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); | |
| } | |
| function normalizeMessage(message: string): string { | |
| if (message.toLowerCase().includes("email not confirmed")) { | |
| return "Sign-up succeeded, but email confirmation is still required before sign-in."; | |
| } | |
| return message; | |
| } | |
| export function AuthPanel({ mode, appName }: AuthPanelProps) { | |
| const router = useRouter(); | |
| const [name, setName] = useState(""); | |
| const [email, setEmail] = useState(""); | |
| const [password, setPassword] = useState(""); | |
| const [isSubmitting, setSubmitting] = useState(false); | |
| const [error, setError] = useState<string | null>(null); | |
| const [notice, setNotice] = useState<string | null>(null); | |
| const isSignUp = mode === "sign-up"; | |
| async function handleSubmit(event: React.FormEvent<HTMLFormElement>) { | |
| event.preventDefault(); | |
| setError(null); | |
| setNotice(null); | |
| if (isSignUp && !name.trim()) { | |
| setError("Name is required."); | |
| return; | |
| } | |
| if (!validateEmail(email.trim().toLowerCase())) { | |
| setError("Enter a valid email address."); | |
| return; | |
| } | |
| if (password.length < 6) { | |
| setError("Password must be at least 6 characters."); | |
| return; | |
| } | |
| setSubmitting(true); | |
| const supabase = createClient(); | |
| if (isSignUp) { | |
| const { data, error: signUpError } = await supabase.auth.signUp({ | |
| email: email.trim().toLowerCase(), | |
| password, | |
| options: { | |
| emailRedirectTo: `${window.location.origin}/sign-in`, | |
| data: { | |
| full_name: name.trim() | |
| } | |
| } | |
| }); | |
| setSubmitting(false); | |
| if (signUpError) { | |
| setError(normalizeMessage(signUpError.message)); | |
| return; | |
| } | |
| if (data.session) { | |
| router.push("/app"); | |
| router.refresh(); | |
| return; | |
| } | |
| setNotice("Account created. Check your email for a confirmation link, then sign in."); | |
| return; | |
| } | |
| const { error: signInError } = await supabase.auth.signInWithPassword({ | |
| email: email.trim().toLowerCase(), | |
| password | |
| }); | |
| if (signInError) { | |
| console.warn("[AUTH DIAGNOSTIC] Supabase authentication failed. Attempting local mock fallback. Error:", signInError.message); | |
| try { | |
| const fallbackResponse = await fetch("/api/auth/login", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ email, password }) | |
| }); | |
| const fallbackData = await fallbackResponse.json().catch(() => null); | |
| if (fallbackResponse.ok && fallbackData?.success) { | |
| console.log("[AUTH FALLBACK] Successful mock sign-in for:", email); | |
| setSubmitting(false); | |
| router.push("/app"); | |
| router.refresh(); | |
| return; | |
| } else { | |
| setError( | |
| fallbackData?.error || normalizeMessage(signInError.message) | |
| ); | |
| setSubmitting(false); | |
| return; | |
| } | |
| } catch (fallbackErr) { | |
| console.error("[AUTH FALLBACK] Error calling mock sign-in endpoint:", fallbackErr); | |
| } | |
| setError(normalizeMessage(signInError.message)); | |
| setSubmitting(false); | |
| return; | |
| } | |
| setSubmitting(false); | |
| router.push("/app"); | |
| router.refresh(); | |
| } | |
| return ( | |
| <section className="ui-card w-full max-w-md px-6 py-8 sm:px-7 sm:py-9"> | |
| <BrandLogo href="/sign-in" className="mb-6" /> | |
| <h1 className="ui-section-title">{isSignUp ? `Create ${appName} account` : `Sign in to ${appName}`}</h1> | |
| <p className="mt-3 text-base leading-7 text-muted"> | |
| {isSignUp | |
| ? "Use Supabase email/password auth for the MVP workspace. New accounts must confirm their email before sign-in." | |
| : "Sign in with the email/password account provisioned in Supabase Auth."} | |
| </p> | |
| {error ? ( | |
| <p className="mt-5 rounded-xl bg-danger px-4 py-3 text-center text-sm font-bold text-white"> | |
| {error} | |
| </p> | |
| ) : null} | |
| {notice ? ( | |
| <p className="mt-5 rounded-xl border border-line bg-field px-4 py-3 text-center text-sm font-medium text-ink"> | |
| {notice} | |
| </p> | |
| ) : null} | |
| <form className="mt-6 space-y-4" onSubmit={handleSubmit}> | |
| {isSignUp ? ( | |
| <div> | |
| <label htmlFor="name" className="ui-label"> | |
| Full name | |
| </label> | |
| <input | |
| id="name" | |
| name="name" | |
| type="text" | |
| autoComplete="name" | |
| value={name} | |
| onChange={(event) => setName(event.target.value)} | |
| className="ui-input" | |
| placeholder="Your full name" | |
| required | |
| /> | |
| </div> | |
| ) : null} | |
| <div> | |
| <label htmlFor="email" className="ui-label"> | |
| </label> | |
| <input | |
| id="email" | |
| name="email" | |
| type="email" | |
| autoComplete="email" | |
| value={email} | |
| onChange={(event) => setEmail(event.target.value)} | |
| className="ui-input" | |
| placeholder="you@example.com" | |
| required | |
| /> | |
| </div> | |
| <div> | |
| <label htmlFor="password" className="ui-label"> | |
| Password | |
| </label> | |
| <input | |
| id="password" | |
| name="password" | |
| type="password" | |
| autoComplete={isSignUp ? "new-password" : "current-password"} | |
| value={password} | |
| onChange={(event) => setPassword(event.target.value)} | |
| className="ui-input" | |
| placeholder="At least 6 characters" | |
| required | |
| /> | |
| </div> | |
| <button type="submit" className="ui-primary-button" disabled={isSubmitting}> | |
| <HugeiconsIcon | |
| icon={isSignUp ? CheckmarkCircle01Icon : Login01Icon} | |
| size={17} | |
| strokeWidth={1.9} | |
| aria-hidden | |
| /> | |
| {isSubmitting ? "Please wait..." : isSignUp ? "Create account" : "Sign in"} | |
| </button> | |
| </form> | |
| <div className="mt-6 rounded-xl border border-line bg-field px-4 py-3 text-sm text-muted"> | |
| <p className="font-medium text-ink">Moderator access</p> | |
| <p className="mt-1"> | |
| Sign in with an email listed in <code>MODERATOR_EMAILS</code> to unlock moderation screens. | |
| </p> | |
| </div> | |
| <p className="mt-6 text-sm text-muted"> | |
| {isSignUp ? ( | |
| <> | |
| Already have an account?{" "} | |
| <Link href="/sign-in" className="font-bold text-brand hover:underline"> | |
| Sign in | |
| </Link> | |
| </> | |
| ) : ( | |
| <> | |
| Need an account?{" "} | |
| <Link href="/sign-up" className="font-bold text-brand hover:underline"> | |
| Create account | |
| </Link> | |
| </> | |
| )} | |
| </p> | |
| </section> | |
| ); | |
| } | |