import { useState } from 'react'; import type { FormEvent } from 'react'; import PocketBase from 'pocketbase'; import { Input } from "./ui/input"; import { Button } from "./ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "./ui/card"; import { Eye, EyeOff, Loader2 } from "lucide-react"; import { Separator } from "./ui/separator"; import { Label } from "./ui/label"; interface AuthStatus { message: string; isError: boolean; } interface UserRecord { id: string; email: string; name?: string; avatar?: string; [key: string]: any; } interface AuthResponse { token: string; record: UserRecord; } const LoginPage = () => { const [email, setEmail] = useState('suvodip@siliconpin.com'); const [password, setPassword] = useState('Simple2pass'); const [passwordVisible, setPasswordVisible] = useState(false); const [status, setStatus] = useState({ message: '', isError: false }); const [isLoading, setIsLoading] = useState(false); const pb = new PocketBase("https://tst-pb.s38.siliconpin.com"); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setIsLoading(true); setStatus({ message: '', isError: false }); try { const authData = await pb.collection("users").authWithPassword(email, password); 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 || '' } }; await syncSessionWithBackend(authResponse); // window.location.href = '/profile'; } catch (error) { console.error("Login failed:", error); setStatus({ message: "Login failed. Please check your credentials.", isError: true }); } finally { setIsLoading(false); } }; const loginWithOAuth2 = async (provider: 'google' | 'facebook' | 'github') => { try { setIsLoading(true); setStatus({ message: '', isError: false }); const authData = await pb.collection('users').authWithOAuth2({ provider }); if (!authData?.record) { throw new Error("No user record found"); } 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 || '' } }; await syncSessionWithBackend(authResponse); // window.location.href = '/profile'; } catch (error) { console.error(`${provider} Login failed:`, error); setStatus({ message: `${provider} login failed. Please try again.`, isError: true }); } finally { 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); } }; return (
Welcome Back Sign in to access your account {status.message && (
{status.message}
)}
setEmail(e.target.value)} placeholder="you@example.com" required className="focus:ring-2 focus:ring-blue-500" />
setPassword(e.target.value)} placeholder="••••••••" required className="focus:ring-2 focus:ring-blue-500 pr-10" />
Or continue with
Don't have an account?{' '} Sign up
); }; export default LoginPage;