Add order page with real API calls

This commit is contained in:
Urtzi Alfaro
2025-09-19 11:44:38 +02:00
parent 447e2a5012
commit 105410c9d3
22 changed files with 2556 additions and 463 deletions

View File

@@ -1,5 +1,6 @@
import React, { createContext, useContext, useEffect, ReactNode } from 'react';
import React, { createContext, useContext, useEffect, ReactNode, useState } from 'react';
import { useAuthStore, User } from '../stores/auth.store';
import { authService } from '../api/services/auth';
interface AuthContextType {
user: User | null;
@@ -31,29 +32,43 @@ interface AuthProviderProps {
export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const authStore = useAuthStore();
const [isInitializing, setIsInitializing] = useState(true);
// Initialize auth on mount
useEffect(() => {
const initializeAuth = async () => {
setIsInitializing(true);
// Wait a bit for zustand persist to rehydrate
await new Promise(resolve => setTimeout(resolve, 100));
// Check if we have stored auth data
const storedAuth = localStorage.getItem('auth-storage');
if (storedAuth) {
if (authStore.token && authStore.refreshToken) {
try {
const { state } = JSON.parse(storedAuth);
if (state.token && state.user) {
// Validate token by attempting to refresh
try {
await authStore.refreshAuth();
} catch (error) {
// Token is invalid, clear auth
authStore.logout();
}
}
// Validate current token by trying to verify it
await authService.verifyToken();
console.log('Token is valid, user authenticated');
} catch (error) {
console.error('Error parsing stored auth:', error);
authStore.logout();
console.log('Token expired, attempting refresh...');
// Token is invalid, try to refresh
try {
await authStore.refreshAuth();
console.log('Token refreshed successfully');
} catch (refreshError) {
console.log('Token refresh failed, logging out:', refreshError);
// Refresh failed, clear auth
authStore.logout();
}
}
} else if (authStore.isAuthenticated) {
// User is marked as authenticated but no tokens, logout
console.log('No tokens found but user marked as authenticated, logging out');
authStore.logout();
} else {
console.log('No stored auth data found');
}
setIsInitializing(false);
};
initializeAuth();
@@ -63,7 +78,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const contextValue: AuthContextType = {
user: authStore.user,
isAuthenticated: authStore.isAuthenticated,
isLoading: authStore.isLoading,
isLoading: authStore.isLoading || isInitializing,
error: authStore.error,
login: authStore.login,
logout: authStore.logout,