My react native app will hang indefinitely without any API calls made
Sometimes when I open my app, it will hang indefinitely without any API calls made. I have to fully close the app and open it again and it works like normal. I notice (according to the Supabase API Gateway logs) that this happens when the token refresh happens. Note that this only happens after about 30min-1h of not using the app. Here's the code I think might be causing this:
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [user, setUser] = useState<User | null>(null);
const [session, setSession] = useState<Session | null>(null);
const [profile, setProfile] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
checkUser();
const { data: authListener } = supabase.auth.onAuthStateChange(
async (event, session) => {
try {
const currentUser = session?.user;
setSession(session ?? null);
setUser(currentUser ?? null);
// Handle password reset
if (event === "PASSWORD_RECOVERY") {
router.replace("/(auth)/update-password");
return;
}
if (currentUser) {
const { data, error } = await supabase
.from("profiles")
.select("*")
.eq("uid", currentUser.id)
.single();
if (error) {
console.error("Error fetching profile:", error);
return;
}
setProfile(data || null);
}
setLoading(false);
} catch (error) {
console.error("Error in auth state change:", error);
setLoading(false); // Ensure loading is set to false even on error
}
}
);
return () => {
if (authListener) authListener.subscription.unsubscribe();
};
}, [user?.id]);export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [user, setUser] = useState<User | null>(null);
const [session, setSession] = useState<Session | null>(null);
const [profile, setProfile] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
checkUser();
const { data: authListener } = supabase.auth.onAuthStateChange(
async (event, session) => {
try {
const currentUser = session?.user;
setSession(session ?? null);
setUser(currentUser ?? null);
// Handle password reset
if (event === "PASSWORD_RECOVERY") {
router.replace("/(auth)/update-password");
return;
}
if (currentUser) {
const { data, error } = await supabase
.from("profiles")
.select("*")
.eq("uid", currentUser.id)
.single();
if (error) {
console.error("Error fetching profile:", error);
return;
}
setProfile(data || null);
}
setLoading(false);
} catch (error) {
console.error("Error in auth state change:", error);
setLoading(false); // Ensure loading is set to false even on error
}
}
);
return () => {
if (authListener) authListener.subscription.unsubscribe();
};
}, [user?.id]);