Page logic!
This commit is contained in:
parent
63f85de6cf
commit
440a637c18
@ -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()
|
||||
|
||||
@ -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<string | null>(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<HTMLFormElement>) => {
|
||||
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 (
|
||||
<div
|
||||
className="min-h-screen flex items-center justify-center"
|
||||
@ -31,7 +89,60 @@ export default async function SignIn({
|
||||
backgroundRepeat: 'no-repeat'
|
||||
}}
|
||||
>
|
||||
<SignInForm callbackUrl={searchParams.callbackUrl} />
|
||||
<div className="w-full max-w-md space-y-8">
|
||||
<div>
|
||||
<h2 className="mt-6 text-center text-3xl font-bold tracking-tight text-white">
|
||||
Sign in to your account
|
||||
</h2>
|
||||
</div>
|
||||
|
||||
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
|
||||
<div className="-space-y-px rounded-md shadow-sm">
|
||||
<div>
|
||||
<label htmlFor="email" className="sr-only">
|
||||
Email address
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
name="email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
required
|
||||
className="relative block w-full rounded-t-md border-0 py-1.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:z-10 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
|
||||
placeholder="Email address"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="password" className="sr-only">
|
||||
Password
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
name="password"
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
required
|
||||
className="relative block w-full rounded-b-md border-0 py-1.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:z-10 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
|
||||
placeholder="Password"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="text-red-500 text-sm text-center">{error}</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
className="group relative flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:opacity-50"
|
||||
>
|
||||
{isLoading ? "Signing in..." : "Sign in"}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user