🔧 Gemini AI Integration: - Added Google Gemini API as default OCR service - Auto-configures from GEMINI_AI_API_KEY environment variable - Fixed Puter.js authentication issues - Enhanced OCR settings with connection testing 🎨 Redesigned Scanner Queue: - New thumbnail + content layout with checkbox overlay - Smart quantity management (duplicates increment quantity) - Complete card information display from database - Two-row action layout (primary/secondary actions) - Floating bottom toolbar for bulk actions - Real card images from database �� Enhanced User Experience: - Fixed Canvas2D performance warnings - Better error handling and fallbacks - Improved responsive design - Database confirmation indicators - Professional card scanning workflow 📱 Mobile Ready: - Optimized layouts for mobile scanning - Touch-friendly controls and interactions - Improved visual feedback and status indicators
116 lines
No EOL
2.8 KiB
JavaScript
116 lines
No EOL
2.8 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('auth_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); // API returns user data directly, not wrapped in .user
|
|
} else {
|
|
localStorage.removeItem('auth_token');
|
|
}
|
|
} catch (error) {
|
|
console.error('Token verification failed:', error);
|
|
localStorage.removeItem('auth_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('auth_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('auth_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('auth_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;
|
|
}
|