Refactor authentication and integration handling
- Exclude the 'MeView' endpoint from public API documentation, marking it as an internal authenticated endpoint. - Enhance error handling in the 'IntegrationSettingsViewSet' to gracefully manage empty request data and improve logging for account and settings lookups. - Update API key retrieval logic to ensure fallback mechanisms are more robust and informative. - Refactor user data fetching in the auth store to utilize a unified API system, improving error handling and data consistency.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
import { fetchAPI } from '../services/api';
|
||||
|
||||
interface User {
|
||||
id: number;
|
||||
@@ -192,27 +193,18 @@ export const useAuthStore = create<AuthState>()(
|
||||
}
|
||||
|
||||
try {
|
||||
const API_BASE_URL = import.meta.env.VITE_BACKEND_URL || 'https://api.igny8.com/api';
|
||||
const token = state.token || getAuthToken();
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/v1/auth/me/`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
||||
},
|
||||
credentials: 'include',
|
||||
});
|
||||
// Use unified API system - fetchAPI automatically handles auth token from store
|
||||
const response = await fetchAPI('/v1/auth/me/');
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok || !data.success) {
|
||||
throw new Error(data.message || 'Failed to refresh user data');
|
||||
// fetchAPI extracts data from unified format {success: true, data: {user: {...}}}
|
||||
// So response is {user: {...}}
|
||||
if (!response || !response.user) {
|
||||
throw new Error('Invalid user data received');
|
||||
}
|
||||
|
||||
// Update user data with latest from server
|
||||
// This ensures account/plan changes are reflected immediately
|
||||
set({ user: data.user });
|
||||
set({ user: response.user });
|
||||
} catch (error: any) {
|
||||
// If refresh fails, don't logout - just log the error
|
||||
// User might still be authenticated, just couldn't refresh data
|
||||
|
||||
Reference in New Issue
Block a user