fix(auth): useLogin maps login response to User, ProtectedRoute calls useAuth()

This commit is contained in:
Agent Zero
2026-08-16 23:32:07 +02:00
parent f265eef5ae
commit c3595a1bac
2 changed files with 16 additions and 1 deletions
+12 -1
View File
@@ -26,7 +26,18 @@ export function useLogin() {
mutationFn: (payload: LoginPayload) => mutationFn: (payload: LoginPayload) =>
apiPost('/auth/login', payload), apiPost('/auth/login', payload),
onSuccess: (data: any) => { onSuccess: (data: any) => {
setUser(data.user || data); // Map flat login response to User interface
const user = data.user || {
id: data.user_id,
email: data.email,
first_name: data.name?.split(' ')[0] || '',
last_name: data.name?.split(' ').slice(1).join(' ') || '',
role: data.role,
is_system_admin: data.is_system_admin,
tenants: [{ id: data.tenant_id, name: data.tenant_name, slug: '' }],
avatar_url: null,
};
setUser(user);
setError(null); setError(null);
// Store CSRF token for subsequent unsafe requests // Store CSRF token for subsequent unsafe requests
if (data.csrf_token) { if (data.csrf_token) {
+4
View File
@@ -1,11 +1,15 @@
import React from 'react'; import React from 'react';
import { Navigate, useLocation } from 'react-router-dom'; import { Navigate, useLocation } from 'react-router-dom';
import { useAuthStore } from '@/store/authStore'; import { useAuthStore } from '@/store/authStore';
import { useAuth } from '@/hooks/useAuth';
export function ProtectedRoute({ children }: { children: React.ReactNode }) { export function ProtectedRoute({ children }: { children: React.ReactNode }) {
const { isAuthenticated } = useAuthStore(); const { isAuthenticated } = useAuthStore();
const location = useLocation(); const location = useLocation();
// Load user data on mount (handles page refresh)
useAuth();
if (!isAuthenticated) { if (!isAuthenticated) {
return <Navigate to="/login" state={{ from: location }} replace />; return <Navigate to="/login" state={{ from: location }} replace />;
} }