11import "./App.css" ;
22import { useQuery , useStatus } from "@powersync/react" ;
33import { COUNTER_TABLE , type CounterRecord } from "./powersync/AppSchema" ;
4- import { useEffect , useState , useCallback } from "react" ;
4+ import { useState } from "react" ;
55import { powerSync } from "./powersync/System" ;
66import { connector } from "./powersync/SupabaseConnector" ;
77
@@ -17,7 +17,9 @@ function App() {
1717 `SELECT * FROM ${ COUNTER_TABLE } ORDER BY created_at ASC`
1818 ) ;
1919
20- const fetchUserID = useCallback ( async ( ) => {
20+ // Function to fetch and set the current user's ID from Supabase auth session
21+ // Handles both existing sessions and new anonymous authentication
22+ const fetchUserID = async ( ) => {
2123 if ( isAuthenticating ) {
2224 console . log ( "Already authenticating, skipping..." ) ;
2325 return ;
@@ -53,13 +55,7 @@ function App() {
5355 } finally {
5456 setIsAuthenticating ( false ) ;
5557 }
56- } , [ isAuthenticating ] ) ;
57-
58- // Effect hook to fetch and set the current user's ID from Supabase auth session
59- // Runs once when the component mounts
60- useEffect ( ( ) => {
61- fetchUserID ( ) ;
62- } , [ fetchUserID ] ) ;
58+ } ;
6359
6460 // Example of executing a native SQLite query using PowerSync
6561 // This demonstrates how to directly execute SQL commands for data mutations
@@ -104,6 +100,12 @@ function App() {
104100 }
105101 } ;
106102
103+ // Check for existing session when component mounts
104+ // This runs only once when the app first loads
105+ if ( ! userID && ! isAuthenticating && ! authError ) {
106+ fetchUserID ( ) ;
107+ }
108+
107109 return (
108110 < div className = "app-container" >
109111 < div className = "status-card" >
0 commit comments