Various Fixes and QoL enhancements.
This commit is contained in:
@@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user