- Updated auth verification to use Neon database instead of mock data - Implemented proper JWT token authentication with localStorage storage - Created beautiful login page with admin quick-login for development - Updated all admin auth hooks to use JWT tokens from localStorage - Added automatic token cleanup on authentication failures - Enhanced AdminProtected component with proper token validation - Created logout functionality that clears tokens and redirects - Maintained fallback admin access for development (no token = admin) - Real admin credentials: admin@tcgvault.com / admin123 - Seamless integration with existing admin card editor workflow
120 lines
No EOL
2.8 KiB
JavaScript
120 lines
No EOL
2.8 KiB
JavaScript
import { createContext, useContext, useState, useEffect } from 'react';
|
|
|
|
// Create admin context
|
|
const AdminContext = createContext();
|
|
|
|
export function AdminProvider({ children }) {
|
|
const [user, setUser] = useState(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
checkAdminAuth();
|
|
}, []);
|
|
|
|
const checkAdminAuth = async () => {
|
|
try {
|
|
// Get token from localStorage
|
|
const token = localStorage.getItem('auth_token');
|
|
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
// Add authorization header if token exists
|
|
if (token) {
|
|
headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await fetch('/api/auth/verify', { headers });
|
|
if (response.ok) {
|
|
const userData = await response.json();
|
|
setUser(userData);
|
|
} else {
|
|
setUser(null);
|
|
// Clear invalid token
|
|
if (token) {
|
|
localStorage.removeItem('auth_token');
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Auth check failed:', error);
|
|
setUser(null);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const isAdmin = () => {
|
|
return user && user.role === 'admin';
|
|
};
|
|
|
|
const isAuthenticated = () => {
|
|
return user !== null;
|
|
};
|
|
|
|
const value = {
|
|
user,
|
|
loading,
|
|
isAdmin,
|
|
isAuthenticated,
|
|
checkAdminAuth
|
|
};
|
|
|
|
return (
|
|
<AdminContext.Provider value={value}>
|
|
{children}
|
|
</AdminContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useAdmin() {
|
|
const context = useContext(AdminContext);
|
|
if (!context) {
|
|
throw new Error('useAdmin must be used within an AdminProvider');
|
|
}
|
|
return context;
|
|
}
|
|
|
|
// Simple hook for checking admin status without context
|
|
export function useIsAdmin() {
|
|
const [isAdmin, setIsAdmin] = useState(false);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
const checkAdmin = async () => {
|
|
try {
|
|
// Get token from localStorage
|
|
const token = localStorage.getItem('auth_token');
|
|
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
// Add authorization header if token exists
|
|
if (token) {
|
|
headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await fetch('/api/auth/verify', { headers });
|
|
if (response.ok) {
|
|
const userData = await response.json();
|
|
setIsAdmin(userData.role === 'admin');
|
|
} else {
|
|
setIsAdmin(false);
|
|
// Clear invalid token
|
|
if (token) {
|
|
localStorage.removeItem('auth_token');
|
|
}
|
|
}
|
|
} catch (error) {
|
|
setIsAdmin(false);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
checkAdmin();
|
|
}, []);
|
|
|
|
return { isAdmin, loading };
|
|
}
|