"use client"; import { Metadata } from "next"; import { getServerSession } from "next-auth/next"; import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { redirect } from "next/navigation"; import { SignInForm } from "@/components/auth/signin-form"; import { signIn } from "next-auth/react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { initializeNextcloudStructure } from "@/lib/nextcloud-init"; export const metadata: Metadata = { title: "Enkun - Connexion", }; export default async function SignIn({ searchParams, }: { searchParams: { callbackUrl?: string }; }) { const session = await getServerSession(authOptions); const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // If user is already authenticated and there's no specific callback URL, // redirect to the home page if (session && !searchParams.callbackUrl) { redirect("/"); } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); setError(null); try { const formData = new FormData(e.currentTarget); const email = formData.get("email"); const password = formData.get("password"); const result = await signIn("credentials", { email, password, redirect: false, }); if (result?.error) { setError("Invalid credentials"); return; } // Initialize folders after successful login try { const response = await fetch('/api/nextcloud/status'); if (!response.ok) { throw new Error('Failed to fetch Nextcloud folders'); } const data = await response.json(); const folders = data.folders || []; // Cache the folders in localStorage for immediate access in Pages localStorage.setItem('nextcloud_folders', JSON.stringify({ folders, timestamp: Date.now() })); } catch (error) { console.error('Error initializing folders:', error); // Continue with navigation even if folder initialization fails } router.push("/"); } catch (error) { setError("An error occurred during sign in"); console.error("Sign in error:", error); } finally { setIsLoading(false); } }; return (

Sign in to your account

{error && (
{error}
)}
); }