Skip to content

Commit 1184920

Browse files
refactor(auth): simplify user ID fetching logic and remove unnecessary useEffect
1 parent 7ab0604 commit 1184920

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/App.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "./App.css";
22
import { useQuery, useStatus } from "@powersync/react";
33
import { COUNTER_TABLE, type CounterRecord } from "./powersync/AppSchema";
4-
import { useEffect, useState, useCallback } from "react";
4+
import { useState } from "react";
55
import { powerSync } from "./powersync/System";
66
import { 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

Comments
 (0)