From 440a637c1827a597340efb39f33e59c99f63e413 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 11:00:16 +0200 Subject: [PATCH] Page logic! --- app/pages/page.tsx | 17 +++++-- app/signin/page.tsx | 113 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 6 deletions(-) diff --git a/app/pages/page.tsx b/app/pages/page.tsx index a20a97dc..9c004a63 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -73,11 +73,13 @@ export default function CarnetPage() { useEffect(() => { const fetchNextcloudFolders = async () => { - // Check cache first - if (foldersCache.current) { - const cacheAge = Date.now() - foldersCache.current.timestamp; + // First check localStorage cache + const cachedData = localStorage.getItem('nextcloud_folders'); + if (cachedData) { + const { folders, timestamp } = JSON.parse(cachedData); + const cacheAge = Date.now() - timestamp; if (cacheAge < 5 * 60 * 1000) { // 5 minutes cache - setNextcloudFolders(foldersCache.current.folders); + setNextcloudFolders(folders); return; } } @@ -90,7 +92,12 @@ export default function CarnetPage() { const data = await response.json(); const folders = data.folders || []; - // Update cache + // Update both localStorage and memory cache + localStorage.setItem('nextcloud_folders', JSON.stringify({ + folders, + timestamp: Date.now() + })); + foldersCache.current = { folders, timestamp: Date.now() diff --git a/app/signin/page.tsx b/app/signin/page.tsx index 3a3a687b..010a2b0f 100644 --- a/app/signin/page.tsx +++ b/app/signin/page.tsx @@ -1,8 +1,14 @@ +"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", @@ -14,6 +20,9 @@ export default async function SignIn({ 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 @@ -21,6 +30,55 @@ export default async function SignIn({ 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}
+ )} + +
+ +
+
+
); }