'use client'; import React, { useEffect, useState } from 'react'; import { getAuth, onAuthStateChanged, User } from 'firebase/auth'; import { useRouter } from 'next/navigation'; import { app } from '@/app/firebase/config'; import { SOLOGIN_API } from './shared'; const HomePage = () => { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); const auth = getAuth(app); const [userPubkey, setUserPubkey] = useState(); const router = useRouter(); const getSologinUser = async (currentUser: User)=>{ const url = `${SOLOGIN_API}getPubkey?email=${currentUser.uid}`; const userDataRes = await fetch(url); const userDataJson = await userDataRes.json(); return userDataJson; } const SetUser = async(currentUser:User)=>{ const sologinUser = await getSologinUser(currentUser); console.log(sologinUser); setUser({ name: currentUser.displayName || 'Anonymous User', email: currentUser.email, id: currentUser.uid, tokenId: await currentUser.getIdToken(), pubkey: sologinUser.pub_key }); } const signout = async()=>{ await auth.signOut(); }; useEffect(() => { const unsubscribe = onAuthStateChanged(auth, async (currentUser) => { if (currentUser) { SetUser(currentUser); } else { router.push('/signup'); // Redirect to signup if not logged in } setLoading(false); }); return () => unsubscribe(); // Cleanup subscription on unmount }, [auth, router]); if (loading) { return (

Loading...

); } if (!user) { return null; // Return null to avoid rendering during redirection } return (

Welcome

Name: {user.name}

Email: {user.email}

ID: {user.id}

PubKey: {user.pubkey}

); }; export default HomePage;