inital working version

This commit is contained in:
Stefan Hardegger
2025-07-22 21:49:40 +02:00
parent bebb799784
commit 59d29dceaf
98 changed files with 8027 additions and 856 deletions

View File

@@ -0,0 +1,64 @@
'use client';
import { createContext, useContext, useEffect, useState } from 'react';
import { authApi } from '../lib/api';
interface AuthContextType {
isAuthenticated: boolean;
login: (password: string) => Promise<void>;
logout: () => void;
loading: boolean;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [loading, setLoading] = useState(true);
useEffect(() => {
// Check if user is already authenticated on app load
const checkAuth = async () => {
try {
const authenticated = authApi.isAuthenticated();
setIsAuthenticated(authenticated);
} catch (error) {
console.error('Auth check failed:', error);
setIsAuthenticated(false);
} finally {
setLoading(false);
}
};
checkAuth();
}, []);
const login = async (password: string) => {
try {
await authApi.login(password);
setIsAuthenticated(true);
} catch (error) {
console.error('Login failed:', error);
throw error;
}
};
const logout = () => {
authApi.logout();
setIsAuthenticated(false);
};
return (
<AuthContext.Provider value={{ isAuthenticated, login, logout, loading }}>
{children}
</AuthContext.Provider>
);
}
export function useAuth() {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
}