diff --git a/src/components/Login.tsx b/src/components/Login.tsx index 12659d5..0426fb6 100644 --- a/src/components/Login.tsx +++ b/src/components/Login.tsx @@ -26,6 +26,7 @@ interface AuthResponse { record: UserRecord; } + const LoginPage = () => { const [email, setEmail] = useState('suvodip@siliconpin.com'); const [password, setPassword] = useState('Simple2pass'); @@ -35,18 +36,30 @@ const LoginPage = () => { const pb = new PocketBase("https://tst-pb.s38.siliconpin.com"); + interface AuthResponse { + token: string; + record: { + query: string; + id: string; + email: string; + name: string; + avatar: string; + }; + } + const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setIsLoading(true); setStatus({ message: '', isError: false }); - + try { const authData = await pb.collection("users").authWithPassword(email, password); - + const avatarUrl = authData.record.avatar ? pb.files.getUrl(authData.record, authData.record.avatar) : ''; + const authResponse: AuthResponse = { token: authData.token, record: { - query: 'new', + query: 'new', id: authData.record.id, email: authData.record.email, name: authData.record.name || '', @@ -54,8 +67,8 @@ const LoginPage = () => { } }; - await syncSessionWithBackend(authResponse); - // window.location.href = '/profile'; + await syncSessionWithBackend(authResponse, avatarUrl); + window.location.href = '/profile'; } catch (error) { console.error("Login failed:", error); setStatus({ @@ -77,20 +90,21 @@ const LoginPage = () => { if (!authData?.record) { throw new Error("No user record found"); } - + + const avatarUrl = authData.record.avatar ? pb.files.getUrl(authData.record, authData.record.avatar) : ''; const authResponse: AuthResponse = { token: authData.token, record: { - query: 'new', - id: authData.record.id, - email: authData.record.email || '', - name: authData.record.name || '', - avatar: authData.record.avatar || '' + query: 'new', + id: authData.record.id, + email: authData.record.email || '', + name: authData.record.name || '', + avatar: authData.record.avatar || '' } }; - await syncSessionWithBackend(authResponse); - // window.location.href = '/profile'; + await syncSessionWithBackend(authResponse, avatarUrl); + window.location.href = '/profile'; } catch (error) { console.error(`${provider} Login failed:`, error); setStatus({ @@ -101,33 +115,35 @@ const LoginPage = () => { setIsLoading(false); } }; - -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); - } -}; + + const syncSessionWithBackend = async (authData: AuthResponse, avatarUrl: string) => { + try { + const response = await fetch('http://localhost:2058/host-api/v1/users/session/', { + method: 'POST', + credentials: 'include', // Important for cookies + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + query: 'new', + accessToken: authData.token, + email: authData.record.email, + name: authData.record.name, + avatar: avatarUrl, + 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); + throw error; // Re-throw the error if you want calling functions to handle it + } + }; return (
+ JPG, GIF or PNG. 1MB max. +
+{selectedFile.name}
+{(selectedFile.size / 1024).toFixed(2)} KB
+- Update your profile settings. -
-Update your profile settings.
+- JPG, GIF or PNG. 1MB max. -
-Invoice ID | +Date | +Description | +Amount | +Action | +
---|---|---|---|---|
{invoice.invoice_id} | +{invoice.date} | +{invoice.description} | +{invoice.amount} | +|
dcdicdicib | +2023-10-01 | +Subscription Fee | +$10.00 | +|
dcibcdici | +2023-09-01 | +Subscription Fee | +$10.00 | +