Auth Provider
User authentication and profile state management
AuthProvider
The AuthProvider is a core ChangeNotifier responsible for managing the user's authentication lifecycle and synchronizing profile data with Firestore.
Core Responsibilities
- Authentication State: Listening to Firebase Auth streams.
- User Data Sync: Fetching the
users/{uid}document. - Authentication Methods: Handling Login, Registration, Google Sign-In, and Logout.
- Error Handling: Catching Auth exceptions and providing readable errors to the UI.
Implementation Details
Key Methods
signInWithEmail(String email, String password)
Authenticates the user via Firebase. Throws custom exceptions if the user is not found or the password is wrong. Sets _isLoading to true during the network request.
registerWithEmail(String email, String password, String name)
- Creates the user in Firebase Auth.
- Updates the Auth
displayName. - Creates a new document in the
usersFirestore collection.
signInWithGoogle()
Invokes the google_sign_in plugin to obtain auth credentials, then authenticates with Firebase. If the user is new, it initializes their Firestore document.
signOut()
Clears local state, signs out of Firebase, and signs out of Google SignIn if applicable.
UI Integration
Widgets should consume this provider using Consumer<AuthProvider> or context.watch<AuthProvider>().