A crucial part of many applications is the ability to create user accounts and authenticate existing users. Firebase Authentication provides a complete and secure solution for this and is easy to integrate with Flutter. In this chapter, we are going to explore how to implement Firebase authentication in a Flutter application.
Firebase Configuration
Before we get started, we need to configure Firebase. First, create a new Firebase project and follow the instructions to add Firebase to your Flutter app. Make sure to enable email and password authentication method in Firebase Authentication settings.
Flutter Package Installation
To use Firebase Authentication, we need to install the firebase_auth package. Add the following line to your pubspec.yaml file and run 'flutter packages get' in the terminal:
dependencies: firebase_auth: ^0.18.4
Creating the User Interface
Let's create a simple user interface with two text fields for email and password and two buttons for login and registration. To keep things simple, we won't deal with form validation in this example.
TextField( decoration: InputDecoration( labelText: 'Email', ), onChanged: (value) { email = value; }, ), TextField( decoration: InputDecoration( labelText: 'Password', ), obscureText: true, onChanged: (value) { password = value; }, ), RaisedButton( child: Text('Sign In'), onPressed: signIn, ), RaisedButton( child: Text('Register'), onPressed: register, ),
Firebase Authentication
Now let's implement the signIn and register functions. First, we need to create a new instance of FirebaseAuth:
final FirebaseAuth auth = FirebaseAuth.instance;
The signIn function attempts to authenticate a user with the provided email and password. If authentication is successful, the user is automatically logged into the app and can access protected features.
FuturesignIn() async { try { UserCredential userCredential = await auth.signInWithEmailAndPassword( email: email, password: password, ); } on FirebaseAuthException catch (e) { if (e.code == 'user-not-found') { print('No user found for that email.'); } else if (e.code == 'wrong-password') { print('Wrong password provided for that user.'); } } }
The register function creates a new user account with the provided email and password. If the account creation is successful, the user will be automatically logged into the application.
Futureregister() async { try { UserCredential userCredential = await auth.createUserWithEmailAndPassword( email: email, password: password, ); } on FirebaseAuthException catch (e) { if (e.code == 'weak-password') { print('The password provided is too weak.'); } else if (e.code == 'email-already-in-use') { print('The account already exists for that email.'); } } catch (e) { print(e); } }
Conclusion
With this, you should have a basic understanding of how to implement Firebase authentication in a Flutter app. There are many other features that Firebase Authentication offers like Phone Authentication, Google Sign-In, Facebook Login and more. We hope this chapter has provided a solid foundation for you to explore these features on your own.