This commit addresses the remaining high-priority issues identified in the
comprehensive codebase analysis, implementing proper notification systems,
error handling, input validation, and race condition fixes.
1. CRITICAL: Implement Stripe webhook notifications
- Created new NotificationService for centralized notification handling
- Supports both in-app notifications and email queuing
- Implemented all 4 missing webhook notifications:
* Subscription downgrade notifications
* Payment success receipts
* Payment failure alerts with recovery action
* Trial ending reminders with days remaining calculation
- Notifications stored in database and emitted via Socket.io
- File: src/backend/services/notificationService.js (NEW)
- Updated: src/backend/routes/webhooks/stripeWebhook.js
2. HIGH: Add comprehensive error handling to socket event handlers
- Wrapped all socket event handlers in try-catch blocks
- Emit error events back to clients when operations fail
- Prevents server crashes from unhandled socket errors
- Provides user feedback for failed socket operations
- File: src/backend/services/socketService.js
3. HIGH: Fix race condition in Chat component
- Added activeConversationRef to track current conversation
- Check conversation ID before updating messages after async load
- Clear messages immediately when switching conversations
- Prevents stale messages from appearing when rapidly switching
- File: src/frontend/components/Chat/Chat.jsx
4. HIGH: Add input validation to messaging service
- Validate userId and conversationId are valid strings
- Ensure message content is not empty and under 10K chars
- Validate contentType against allowed types
- Validate metadata structure
- Provides clear error messages for invalid input
- File: src/backend/services/messagingService.js
5. MEDIUM: Replace hardcoded URLs with environment variables
- Updated AuthContext to use VITE_API_URL env variable
- Maintains localhost fallback for development
- File: src/frontend/contexts/AuthContext.jsx
6. Documentation: Update .env.example
- Added FRONTEND_URL configuration
- Documented ALLOW_DEV_BYPASS security flag
- Added critical warnings for TURN server configuration
- Added Stripe configuration variables
- File: .env.example
These fixes significantly improve:
- User experience (notifications for all payment events)
- System reliability (proper error handling, race condition fixes)
- Security (input validation prevents malicious input)
- Maintainability (proper environment configuration)
81 lines
2 KiB
JavaScript
81 lines
2 KiB
JavaScript
import React, { createContext, useContext, useState, useEffect } from 'react';
|
|
|
|
const AuthContext = createContext();
|
|
|
|
export function useAuth() {
|
|
const context = useContext(AuthContext);
|
|
if (!context) {
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
|
}
|
|
return context;
|
|
}
|
|
|
|
export function AuthProvider({ children }) {
|
|
const [user, setUser] = useState(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
// Initialize with demo user for development
|
|
const demoUser = {
|
|
id: 'demo-user-123',
|
|
name: 'Demo User',
|
|
email: 'demo@aethex.dev',
|
|
verifiedDomain: 'demo.aethex',
|
|
domainVerifiedAt: new Date().toISOString(),
|
|
isPremium: false,
|
|
avatar: null
|
|
};
|
|
|
|
setUser(demoUser);
|
|
setLoading(false);
|
|
}, []);
|
|
|
|
const login = async (email, password) => {
|
|
// Mock login - in production, call actual API
|
|
try {
|
|
const apiUrl = import.meta.env.VITE_API_URL || 'http://localhost:3000';
|
|
const response = await fetch(`${apiUrl}/api/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
setUser(data.user);
|
|
localStorage.setItem('token', data.token);
|
|
return { success: true };
|
|
}
|
|
return { success: false, error: 'Login failed' };
|
|
} catch (error) {
|
|
console.error('Login error:', error);
|
|
return { success: false, error: error.message };
|
|
}
|
|
};
|
|
|
|
const logout = () => {
|
|
setUser(null);
|
|
localStorage.removeItem('token');
|
|
};
|
|
|
|
const updateUser = (updates) => {
|
|
setUser(prev => ({ ...prev, ...updates }));
|
|
};
|
|
|
|
const value = {
|
|
user,
|
|
loading,
|
|
login,
|
|
logout,
|
|
updateUser,
|
|
isAuthenticated: !!user
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider value={value}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|
|
|
|
export default AuthContext;
|