diff --git a/src/components/Login.tsx b/src/components/Login.tsx index 16828d7..12659d5 100644 --- a/src/components/Login.tsx +++ b/src/components/Login.tsx @@ -102,34 +102,32 @@ const LoginPage = () => { } }; - const syncSessionWithBackend = async (authData: AuthResponse) => { - try { - const response = await fetch('http://localhost:2058/host-api/v1/users/session/', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - query: 'new', - accessToken: authData.token, - email: authData.record.email, - name: authData.record.name, - avatar: authData.record.avatar - ? pb.files.getUrl(authData.record, authData.record.avatar) - : '', - isAuthenticated: true, - id: authData.record.id - }) - }); - - if (!response.ok) { - throw new Error('Failed to sync session'); - } - - const data = await response.json(); - console.log('Session synced with backend:', data); - } catch (error) { - console.error('Error syncing session:', error); - } - }; +const syncSessionWithBackend = async (authData: AuthResponse) => { + try { + const response = await fetch('http://localhost:2058/host-api/v1/users/session/', { + method: 'POST', + credentials: 'include', // Crucial for cookies + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + accessToken: authData.token, + email: authData.record.email, + name: authData.record.name, + avatar: authData.record.avatar + ? pb.files.getUrl(authData.record, authData.record.avatar) + : '', + isAuthenticated: true, + id: authData.record.id + }) + }); + + if (!response.ok) throw new Error('Failed to sync session'); + + const data = await response.json(); + console.log('Session synced:', data); + } catch (error) { + console.error('Error syncing session:', error); + } +}; return (
diff --git a/src/components/UserProfile.tsx b/src/components/UserProfile.tsx index 744f0d2..4e7f83c 100644 --- a/src/components/UserProfile.tsx +++ b/src/components/UserProfile.tsx @@ -24,34 +24,29 @@ export default function ProfilePage() { const [error, setError] = useState(null); useEffect(() => { - const fetchData = async () => { + const fetchSessionData = async () => { try { const response = await fetch( 'http://localhost:2058/host-api/v1/users/get-profile-data/', { - credentials: 'include', - headers: { - 'Accept': 'application/json', - } + credentials: 'include', // Crucial for cookies + headers: { 'Accept': 'application/json' } } ); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); + + const data = await response.json(); + if (!response.ok || !data.success) { + throw new Error(data.error || 'Session fetch failed'); } - const data: UserData = await response.json(); - // console.log('success message', data.success); - if(data.success === true){ - setUserData(data); - // console.log('User Data', data); - } - + setUserData(data); + return data.session_data; } catch (error) { console.error('Fetch error:', error); - setError(error instanceof Error ? error.message : 'An unknown error occurred'); + throw error; } }; - fetchData(); + fetchSessionData(); }, []); if (error) {