This commit is contained in:
2025-07-07 16:03:37 +02:00
parent 12a3537028
commit ef42a8ad0b
9 changed files with 129 additions and 29 deletions

View File

@@ -0,0 +1,44 @@
import { useState, useEffect } from 'react';
import { adminLogin } from '@/lib/pocketbase';
export function useAdmin() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
let mounted = true;
const initializeAdmin = async () => {
try {
setLoading(true);
await adminLogin();
if (mounted) {
setIsLoggedIn(true);
setError(null);
}
} catch (err) {
if (mounted) {
setError(err instanceof Error ? err.message : 'Failed to login as admin');
setIsLoggedIn(false);
}
} finally {
if (mounted) {
setLoading(false);
}
}
};
initializeAdmin();
return () => {
mounted = false;
};
}, []); // Empty dependency array ensures this only runs once on mount
return {
isLoggedIn,
loading,
error
};
}