AuthContext
The AuthContext is a React Context that provides a centralized way to manage Firebase Authentication state and functions throughout your application. It makes the current authenticated user and authentication-related functions available to all components wrapped by its AuthProvider.
AuthContextType Interface
Defines the shape of the context value provided by AuthProvider.
Properties
currentUser:User | nullThe currently authenticated FirebaseUserobject, ornullif no user is authenticated.signup:(email: string, password: string) => Promise<any>An asynchronous function to create a new user with email and password. Returns a FirebaseUserCredential.signin:(email: string, password: string) => Promise<any>An asynchronous function to sign in an existing user with email and password. Returns a FirebaseUserCredential.signout:() => Promise<void>An asynchronous function to sign out the current user.auth:AuthThe Firebase Authentication instance.
AuthProvider Component
A React Context Provider that manages the Firebase Authentication state and makes it available to all its children components. It initializes the Firebase auth instance from ConfigContext and sets up an observer for authentication state changes.
Props
children:React.ReactNodeThe child components that will have access to the authentication context.
Hooks Used Internally
useState,useEffect: To manage the internalcurrentUserstate and loading status.useConfig: To obtain the Firebaseauthinstance from the application’s configuration.onAuthStateChanged: Firebase Authentication listener to observe changes in the user’s sign-in state.
Firebase Authentication
This context directly initializes and interacts with Firebase Authentication:
createUserWithEmailAndPassword: Used by thesignupfunction.signInWithEmailAndPassword: Used by thesigninfunction.signOut: Used by thesignoutfunction.onAuthStateChanged: Observes the authentication state.
Usage
The AuthProvider should wrap the part of your application that needs access to authentication state, typically at the root level, after ConfigProvider.
import React from 'react';
import { AuthProvider, ConfigProvider, LoadingProvider } from '@fireact.dev/app';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import firebaseConfig from './config/firebase.config.json'; // Your Firebase config
import appConfig from './config/app.config.json'; // Your app config
import stripeConfig from './config/stripe.config.json'; // Your Stripe config
function App() {
return (
<Router>
<ConfigProvider firebaseConfig={firebaseConfig.firebase} appConfig={appConfig} stripeConfig={stripeConfig.stripe}>
<AuthProvider>
<LoadingProvider>
<Routes>
{/* Your application routes go here */}
<Route path="/signin" element={<div>Sign In Page</div>} />
<Route path="/dashboard" element={<div>Dashboard Page (requires auth)</div>} />
</Routes>
</LoadingProvider>
</AuthProvider>
</ConfigProvider>
</Router>
);
}
export default App;
Important Notes
- The
AuthProviderensures thatchildrenare only rendered once the authentication state has been loaded (!loading). - It relies on the
ConfigContextto get the Firebaseauthinstance, soConfigProvidermust be a parent ofAuthProvider. - The
Userinterface fromfirebase/authis augmented insrc/types.tsto include custom permissions.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.