Overview
TheAuthService class provides comprehensive authentication functionality for the Flutter application, including email/password authentication, Google Sign-In, user profile management, and user search capabilities.
Location: lib/services/auth_service.dart
Dependencies
firebase_auth- Firebase Authenticationcloud_firestore- Firestore databasefirebase_storage- Firebase Storage for profile imagesgoogle_sign_in- Google Sign-In integrationlogger- Logging functionality
Authentication Methods
registerWithEmailAndPassword
Registers a new user with email and password credentials.The user’s email address
The user’s password
Returns a
User object if registration is successful, or null if an error occursExample
Error Handling
- Returns
nullif registration fails - Errors are logged using the Logger instance
- Common errors include: invalid email format, weak password, email already in use
signInWithEmailAndPassword
Authenticates an existing user with email and password.The user’s email address
The user’s password
Returns a
User object if sign-in is successful, or null if an error occursExample
Error Handling
- Returns
nullif authentication fails - Errors are logged automatically
- Common errors include: user not found, wrong password, too many requests
signInWithGoogle
Authenticates a user using Google Sign-In.Returns a
User object if sign-in is successful, or null if the user cancels or an error occursBehavior
- Opens Google Sign-In dialog
- Automatically creates user profile in Firestore if it doesn’t exist
- Uses
displayNamefrom Google account as the user’s name - Returns
nullif user cancels the sign-in dialog
Example
Firestore Integration
signOut
Signs out the current user from both Firebase Auth and Google Sign-In.Example
Profile Management
saveUserProfile
Saves or updates user profile information in Firestore, including optional profile image upload to Firebase Storage.The user’s unique identifier from Firebase Auth
The user’s first name
The user’s surname/last name
Optional profile image file. If provided, it will be uploaded to Firebase Storage at
profileImages/{uid}.jpgFirestore Document Structure
Example
Storage Behavior
getUserProfile
Retrieves user profile data from Firestore.The user’s unique identifier
Returns a map containing user profile data, or
null if the profile doesn’t exist or an error occursExample
User Search
searchUsersByName
Searches for users by name using Firestore queries.The name or partial name to search for
Returns a list of
UserProfile objects matching the search query. Returns empty list if no matches found or error occurs.Search Behavior
- Uses Firestore range queries with
\uf8ffcharacter for prefix matching - Searches the
namefield in the users collection - Case-sensitive search
Example
searchUsersByNameOrEmail
Searches for users by either name or email address.The search query to match against name or email fields
Returns a combined list of
UserProfile objects matching either name or email. Returns empty list if no matches found or error occurs.Search Strategy
Example
Properties
currentUser
Gets the currently authenticated user.Returns the current
User object if authenticated, or null if no user is signed inExample
displayName
Gets the display name of the current user.Returns the display name, or
null if not set or no user is signed inphotoURL
Gets the photo URL of the current user.Returns the photo URL, or
null if not set or no user is signed inError Handling Best Practices
All methods inAuthService handle errors gracefully:
- Methods return
nullor empty lists on failure - Errors are automatically logged using Logger
- No exceptions are thrown to the caller
- Always check return values before proceeding