"use client"; import React, { useCallback, useEffect, useState, Suspense } from "react"; import { useSearchParams, useRouter } from "next/navigation"; function HomeContent() { const searchParams = useSearchParams(); const router = useRouter(); const accessToken = searchParams.get("access_token"); // Replace with your actual CLIENT_ID const CLIENT_ID = "****************"; // Use state to manage user details const [userDetails, setUserDetails] = useState({ countryCode: "", phoneNo: "", phEmailJwt: "", }); const httpRequest = async () => { try { const url = "https://eapi.phone.email/getuser"; const data = new FormData(); data.append("access_token", accessToken); data.append("client_id", CLIENT_ID); const response = await fetch(url, { method: "POST", body: data }); if (!response.ok) { throw new Error("Network response was not ok"); } const responseData = await response.json(); if (responseData.status !== 200) { throw new Error("Something went wrong"); } const phEmailJwt = responseData.ph_email_jwt; setUserDetails({ countryCode: responseData.country_code, phoneNo: responseData.phone_no, phEmailJwt: phEmailJwt, }); // Set cookie with 1-day expiration const cookieExpire = new Date( Date.now() + 1 * 24 * 60 * 60 * 1000 ).toUTCString(); document.cookie = `ph_email_jwt=${phEmailJwt}; expires=${cookieExpire}; path=/`; } catch (error) { console.error("Fetch error:", error); } }; const openLoginWindow = useCallback(() => { const top = (window.screen.height - 600) / 2; const left = (window.screen.width - 500) / 2; const REDIRECT_URL = window.location.href; const AUTH_URL = `https://www.phone.email/auth/log-in?client_id=${CLIENT_ID}&redirect_url=${REDIRECT_URL}`; const APP_SETTING = `toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0, width=500, height=560, top=${top}, left=${left}`; window.open(AUTH_URL, "peLoginWindow", APP_SETTING); }, []); useEffect(() => { if (accessToken) { httpRequest(); } }, [accessToken]); return ( {!accessToken && (
phone email login demo

Sign In

Welcome to Sign In with Phone

)} {accessToken && (
phone email login demo

Welcome!

You are logged in successfully with
{userDetails.countryCode} {userDetails.phoneNo}

)}
); } export default function Home() { return ( Loading...}> ); }