Various Fixes and QoL enhancements.

This commit is contained in:
Stefan Hardegger
2025-07-26 12:05:54 +02:00
parent 5e8164c6a4
commit f95d7aa8bb
32 changed files with 758 additions and 136 deletions

View File

@@ -1,7 +1,8 @@
'use client';
import { createContext, useContext, useEffect, useState } from 'react';
import { authApi } from '../lib/api';
import { useRouter } from 'next/navigation';
import { authApi, setGlobalAuthFailureHandler } from '../lib/api';
import { preloadSanitizationConfig } from '../lib/sanitization';
interface AuthContextType {
@@ -16,8 +17,18 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [loading, setLoading] = useState(true);
const router = useRouter();
// Handle authentication failures from API calls
const handleAuthFailure = () => {
console.log('Authentication token expired, logging out user');
setIsAuthenticated(false);
router.push('/login');
};
useEffect(() => {
// Register the auth failure handler for API interceptor
setGlobalAuthFailureHandler(handleAuthFailure);
// Check if user is already authenticated on app load
const checkAuth = async () => {
try {
@@ -42,7 +53,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
checkAuth();
loadSanitizationConfig();
}, []);
}, [router]);
const login = async (password: string) => {
try {
@@ -57,6 +68,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const logout = () => {
authApi.logout();
setIsAuthenticated(false);
router.push('/login');
};
return (