- Redesigned card display with 2.5:3.5 aspect ratio and image-only view - Added infinite scroll to replace pagination - Implemented authentic card back placeholders for MTG, Pokemon, and Lorcana - Added rarity-based particle effects with tiered intensity (mythic/enchanted/rare/uncommon) - Enhanced hover details panel with structured card information - Fixed search functionality with debouncing and Enter key support - Improved filter system with working TCG, rarity, set, and price filters - Added favorite system for cards in both hover and detail views - Updated card detail page with comprehensive metadata and actions - Fixed API filtering with proper Vercel Postgres implementation - Added particle animations and rarity glow effects - Improved overall UX with better visual hierarchy and interactions
116 lines
No EOL
2.7 KiB
JavaScript
116 lines
No EOL
2.7 KiB
JavaScript
import { createContext, useContext, useState, useEffect } from 'react';
|
|
|
|
const AuthContext = createContext();
|
|
|
|
export function AuthProvider({ children }) {
|
|
const [user, setUser] = useState(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
// Check for existing token on app load
|
|
const token = localStorage.getItem('token');
|
|
if (token) {
|
|
// Verify token and set user
|
|
verifyToken(token);
|
|
} else {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
const verifyToken = async (token) => {
|
|
try {
|
|
const response = await fetch('/api/auth/verify', {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
const userData = await response.json();
|
|
setUser(userData.user);
|
|
} else {
|
|
localStorage.removeItem('token');
|
|
}
|
|
} catch (error) {
|
|
console.error('Token verification failed:', error);
|
|
localStorage.removeItem('token');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const login = async (email, password) => {
|
|
try {
|
|
const response = await fetch('/api/auth/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok) {
|
|
localStorage.setItem('token', data.token);
|
|
setUser(data.user);
|
|
return { success: true };
|
|
} else {
|
|
return { success: false, error: data.error };
|
|
}
|
|
} catch (error) {
|
|
return { success: false, error: 'Network error' };
|
|
}
|
|
};
|
|
|
|
const register = async (email, password) => {
|
|
try {
|
|
const response = await fetch('/api/auth/register', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok) {
|
|
localStorage.setItem('token', data.token);
|
|
setUser(data.user);
|
|
return { success: true };
|
|
} else {
|
|
return { success: false, error: data.error };
|
|
}
|
|
} catch (error) {
|
|
return { success: false, error: 'Network error' };
|
|
}
|
|
};
|
|
|
|
const logout = () => {
|
|
localStorage.removeItem('token');
|
|
setUser(null);
|
|
};
|
|
|
|
const value = {
|
|
user,
|
|
loading,
|
|
login,
|
|
register,
|
|
logout,
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider value={value}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useAuth() {
|
|
const context = useContext(AuthContext);
|
|
if (!context) {
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
|
}
|
|
return context;
|
|
}
|