Skip to content

Commit

Permalink
Remove unnecessary promise wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
benmalcom committed Feb 12, 2024
1 parent d097d1f commit 753bb32
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions packages/near-fast-auth-signer/src/hooks/useFirebaseUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ import { useState, useEffect } from 'react';

import { firebaseAuth } from '../utils/firebase';

// Custom hook to manage Firebase auth state
function useFirebaseUser() {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true); // Tracks the loading state
const [loading, setLoading] = useState(true);

useEffect(() => {
// This function returns a promise that resolves once Firebase auth state is known
const getAuthState = () => new Promise((resolve) => {
const unsubscribe = firebaseAuth.onAuthStateChanged((user) => {
resolve(user);
unsubscribe(); // Cleanup the subscription
});
const unsubscribe = firebaseAuth.onAuthStateChanged((data) => {
setUser(data);
setLoading(false);
});

// Call getAuthState and update the state based on its result
getAuthState().then((user) => {
setUser(user);
setLoading(false); // Set loading to false once we have the auth state
});
return () => {
unsubscribe(); // Cleanup the subscription when component unmounts
};
}, []);

return { user, loading };
Expand Down

0 comments on commit 753bb32

Please sign in to comment.