refactor: reorganize imports and improve error handling across components

This commit is contained in:
Wikid82
2025-11-20 22:21:32 -05:00
parent 9f62a4a2df
commit 62904858b2
23 changed files with 271 additions and 169 deletions
+3 -25
View File
@@ -1,20 +1,6 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
import React, { useState, useEffect } from 'react';
import client from '../api/client';
interface User {
user_id: number;
role: string;
}
interface AuthContextType {
user: User | null;
login: () => Promise<void>;
logout: () => void;
isAuthenticated: boolean;
isLoading: boolean;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
import { AuthContext, User } from './AuthContextValue';
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [user, setUser] = useState<User | null>(null);
@@ -25,7 +11,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
try {
const response = await client.get('/auth/me');
setUser(response.data);
} catch (error) {
} catch {
setUser(null);
} finally {
setIsLoading(false);
@@ -96,11 +82,3 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
</AuthContext.Provider>
);
};
export const useAuth = () => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};