first commit

This commit is contained in:
DerJesen
2025-11-29 12:26:58 +01:00
parent 2fae31f20f
commit fe5bbc1410
142 changed files with 19585 additions and 1 deletions

View File

@@ -0,0 +1,69 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
interface AuthContextType {
isAuthenticated: boolean;
login: (password: string) => Promise<boolean>;
logout: () => void;
}
const AuthContext = createContext<AuthContextType | null>(null);
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const token = sessionStorage.getItem('eventqr_token');
if (token) {
setIsAuthenticated(true);
}
setIsLoading(false);
}, []);
const login = async (password: string) => {
try {
const response = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password }),
});
if (response.ok) {
const data = await response.json();
if (data.accessToken) {
sessionStorage.setItem('eventqr_token', data.accessToken);
setIsAuthenticated(true);
return true;
}
}
return false;
} catch (error) {
console.error('Login failed:', error);
return false;
}
};
const logout = () => {
sessionStorage.removeItem('eventqr_token');
setIsAuthenticated(false);
fetch('/api/logout', { method: 'POST' });
};
if (isLoading) {
return null;
}
return (
<AuthContext.Provider value={{ isAuthenticated, login, logout }}>
{children}
</AuthContext.Provider>
);
};
export const useAuth = () => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};