Authentication
Authentication
Firebase Authentication provides a complete, secure authentication system for React Native applications, supporting email/password, social logins, and phone-based authentication.
Installing Firebase Auth
bash
npm install @react-native-firebase/auth
Creating a New User
javascript
import auth from '@react-native-firebase/auth';
async function register(email, password) {
await auth().createUserWithEmailAndPassword(email, password);
}
Signing In a User
javascript
async function login(email, password) {
await auth().signInWithEmailAndPassword(email, password);
}
Listening for Authentication State Changes
javascript
useEffect(() => {
const subscriber = auth().onAuthStateChanged((user) => {
setUser(user);
});
return subscriber;
}, []);
Signing Out
javascript
await auth().signOut();
Handling Authentication Errors
javascript
try {
await auth().signInWithEmailAndPassword(email, password);
} catch (error) {
if (error.code === 'auth/wrong-password') {
Alert.alert('Incorrect password');
}
}
Password Reset
javascript
await auth().sendPasswordResetEmail(email);
Conclusion
Firebase Authentication provides a robust, secure foundation for user login and registration in React Native applications, with the onAuthStateChanged listener making it easy to manage authentication state throughout your app.

