"use client" import { signIn } from "next-auth/react" import { useState } from "react" import { useSearchParams, useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Alert, AlertDescription } from "@/components/ui/alert" import { Loader2, Cloud } from "lucide-react" export default function SignInPage() { const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const searchParams = useSearchParams() const router = useRouter() const urlError = searchParams.get("error") const callbackUrl = searchParams.get("callbackUrl") || "/admin" const showAdminLogin = searchParams.get("admin") === "true" const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setError(null) const formData = new FormData(e.currentTarget) const email = formData.get("email") as string const password = formData.get("password") as string try { const result = await signIn("credentials", { email, password, redirect: false, }) if (result?.error) { setError("Invalid email or password. Please try again.") } else if (result?.ok) { // Successful signin - redirect to admin router.push(callbackUrl) router.refresh() } } catch (error) { setError("An error occurred during sign in. Please try again.") } finally { setIsLoading(false) } } const handleNextcloudSignIn = () => { setIsLoading(true) setError(null) // Redirect to custom OAuth authorization route window.location.href = `/api/auth/nextcloud/authorize?callbackUrl=${encodeURIComponent(callbackUrl)}` } return (
Sign In {showAdminLogin ? "Admin emergency access" : "Access the United Tattoo Studio dashboard"} {(error || urlError) && ( {error || (urlError === "CredentialsSignin" ? "Invalid email or password. Please try again." : urlError === "OAuthSignin" ? "Unable to sign in with Nextcloud. Please ensure you are in the 'artists' or 'shop_admins' group." : "An error occurred during sign in. Please try again.")} )} {!showAdminLogin ? ( <> {/* Nextcloud OAuth Primary Sign In */} {/* Info Text */}

Use your Nextcloud credentials to sign in.

You must be a member of the 'artists' or 'shop_admins' group.

) : ( <> {/* Credentials Form (Admin Fallback) */}
{/* Development Note */}

Admin emergency access only.
For normal sign in, use Nextcloud OAuth.

)}
) }