Skip to content

feat(react-native): implements useAuthStateReady hook #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions react-native/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./useSignOutMutation";
export * from "./useIdToken";
export * from "./useReauthenticateWitCredentialMutation";
export * from "./useReauthenticateWitRedirectMutation";
export * from "./useAuthStateReady";
38 changes: 38 additions & 0 deletions react-native/auth/useAuthStateReady.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useAuth } from "./useAuth";
import { useEffect, useState } from "react";

/**
* A custom hook that determines if the Firebase authentication state is ready.
* It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
*
* @group Hook
*
* @returns {boolean}
*
* @example
* ```jsx
* export const MyComponent = () => {
* const isAuthStateReady = useAuthStateReady();
* console.log(isAuthStateReady);
* };
* ```
*/
export const useAuthStateReady = () => {
const firebaseAuth = useAuth();

const [isAuthStateReady, setIsAuthStateReady] = useState(false);

useEffect(() => {
const subscription = firebaseAuth.onAuthStateChanged(() => {
if (!isAuthStateReady) {
setIsAuthStateReady(true);
}
});

return () => {
subscription();
};
}, [firebaseAuth, isAuthStateReady]);

return isAuthStateReady;
};
13 changes: 12 additions & 1 deletion web/auth/useAuthStateReady.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ import { useCallback, useEffect, useState } from "react";
/**
* A custom hook that determines if the Firebase authentication state is ready.
* It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
* @returns {boolean} Indicates whether the authentication state is ready.
*
* @group Hook
*
* @returns {boolean}
*
* @example
* ```jsx
* export const MyComponent = () => {
* const isAuthStateReady = useAuthStateReady();
* console.log(isAuthStateReady);
* };
* ```
*/
export const useAuthStateReady = () => {
const firebaseAuth = useAuth();
Expand Down