login-session
Suvodip 2025-03-27 20:33:15 +05:30
parent 026c6c34b2
commit c3faedf6f8
2 changed files with 37 additions and 44 deletions

View File

@ -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 (
<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);
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) {