Merge pull request 's11' (#15) from login-session into staging

Reviewed-on: #15
pull/17/head
Subhodip Ghosh 2025-03-27 15:03:47 +00:00
commit daa4702904
2 changed files with 37 additions and 44 deletions

View File

@ -102,34 +102,32 @@ const LoginPage = () => {
} }
}; };
const syncSessionWithBackend = async (authData: AuthResponse) => { const syncSessionWithBackend = async (authData: AuthResponse) => {
try { try {
const response = await fetch('http://localhost:2058/host-api/v1/users/session/', { const response = await fetch('http://localhost:2058/host-api/v1/users/session/', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, credentials: 'include', // Crucial for cookies
body: JSON.stringify({ headers: { 'Content-Type': 'application/json' },
query: 'new', body: JSON.stringify({
accessToken: authData.token, accessToken: authData.token,
email: authData.record.email, email: authData.record.email,
name: authData.record.name, name: authData.record.name,
avatar: authData.record.avatar avatar: authData.record.avatar
? pb.files.getUrl(authData.record, authData.record.avatar) ? pb.files.getUrl(authData.record, authData.record.avatar)
: '', : '',
isAuthenticated: true, isAuthenticated: true,
id: authData.record.id id: authData.record.id
}) })
}); });
if (!response.ok) { if (!response.ok) throw new Error('Failed to sync session');
throw new Error('Failed to sync session');
} const data = await response.json();
console.log('Session synced:', data);
const data = await response.json(); } catch (error) {
console.log('Session synced with backend:', data); console.error('Error syncing session:', error);
} catch (error) { }
console.error('Error syncing session:', error); };
}
};
return ( return (
<div className="min-h-screen flex items-center justify-center p-4"> <div className="min-h-screen flex items-center justify-center p-4">

View File

@ -24,34 +24,29 @@ export default function ProfilePage() {
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
useEffect(() => { useEffect(() => {
const fetchData = async () => { const fetchSessionData = async () => {
try { try {
const response = await fetch( const response = await fetch(
'http://localhost:2058/host-api/v1/users/get-profile-data/', 'http://localhost:2058/host-api/v1/users/get-profile-data/',
{ {
credentials: 'include', credentials: 'include', // Crucial for cookies
headers: { headers: { 'Accept': 'application/json' }
'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(); setUserData(data);
// console.log('success message', data.success); return data.session_data;
if(data.success === true){
setUserData(data);
// console.log('User Data', data);
}
} catch (error) { } catch (error) {
console.error('Fetch error:', error); console.error('Fetch error:', error);
setError(error instanceof Error ? error.message : 'An unknown error occurred'); throw error;
} }
}; };
fetchData(); fetchSessionData();
}, []); }, []);
if (error) { if (error) {