page.tsx (2331B)
1 "use client"; 2 3 import { signInWithPopup, signOut, onAuthStateChanged } from "firebase/auth"; 4 import { auth, authProvider } from "@/lib/firebase"; 5 import { useState } from "react"; 6 import { handleTap } from "@/lib/handleTap"; 7 import OneSignal from "react-onesignal"; 8 9 export default function Home() { 10 const [isSignedIn, setIsSignedIn] = useState(false); 11 function subscribe() { 12 if (OneSignal.User) { 13 return <button onClick={toggleSubscribe}>Subscribe</button>; 14 } else { 15 return <button onClick={toggleSubscribe}>Subscribe</button>; 16 } 17 } 18 function toggleSubscribe() { 19 const unsubscribe = onAuthStateChanged(auth, async (user) => { 20 if (user) { 21 setIsSignedIn(true); 22 const userId = user.uid; 23 24 try { 25 await OneSignal.init({ 26 appId: process.env.NEXT_PUBLIC_ONESIGNAL_APP_ID!, 27 }); 28 29 await OneSignal.login(userId); 30 console.log("OneSignal login successful"); 31 } catch (error) { 32 console.error("OneSignal operation failed:", error); 33 } 34 } else { 35 setIsSignedIn(false); 36 console.log("User is signed out"); 37 } 38 }); 39 40 return () => unsubscribe(); 41 } 42 function Signin() { 43 const handleSignIn = async () => { 44 try { 45 await signInWithPopup(auth, authProvider); 46 console.log("Sign-in popup successful"); 47 } catch (error) { 48 console.error("Error signing in:", error); 49 } 50 }; 51 const handleSignOut = async () => { 52 try { 53 await signOut(auth); 54 console.log("Signed out"); 55 } catch (error) { 56 console.error("Error signing out:", error); 57 } 58 }; 59 if (isSignedIn == false) { 60 return <button onClick={handleSignIn}>Sign in with Google</button>; 61 } else { 62 return <button onClick={handleSignOut}>Sign out</button>; 63 } 64 } 65 return ( 66 <main className="flex min-h-screen flex-col items-center justify-center p-8 bg-gray-50"> 67 <h1 className="text-3xl font-bold text-center text-gray-800"> 68 Café Timer 69 </h1> 70 {Signin()} 71 {subscribe()} 72 73 <button onClick={() => handleTap("normal")}>Taped !!!</button> 74 75 <button onClick={() => handleTap("ticket1")}>Ticket 1</button> 76 <button onClick={() => handleTap("ticket2")}>Ticket 2</button> 77 </main> 78 ); 79 }