Browse Visual Learning with Flutter

Implementing Sign-In and Sign-Up with Firebase Authentication in Flutter

Learn how to implement sign-in and sign-up functionalities using Firebase Authentication in Flutter, including email/password and Google Sign-In methods.

13.2.2 Implementing Sign-In and Sign-Up with Firebase Authentication in Flutter

In this section, we will explore how to implement sign-in and sign-up functionalities in a Flutter application using Firebase Authentication. We will cover both email/password authentication and third-party authentication using Google Sign-In. By the end of this guide, you will be able to integrate these authentication methods into your Flutter app, providing a seamless user experience.

Adding firebase_auth to Your Project

To begin, you need to add the firebase_auth package to your Flutter project. This package provides the necessary tools to implement authentication features.

  1. Open your pubspec.yaml file and add the firebase_auth dependency:

    dependencies:
      firebase_auth: ^3.0.0
    
  2. Run the following command to install the package:

    flutter pub get
    

This will download and integrate the firebase_auth package into your project, allowing you to use Firebase Authentication services.

Email and Password Sign-Up

Implementing email and password sign-up involves creating a user interface for input and handling the authentication logic.

Creating a Sign-Up Form

First, let’s create a simple sign-up form using TextFormField widgets for email and password inputs. Ensure that you include validation to enhance user experience and data integrity.

import 'package:flutter/material.dart';

class SignUpForm extends StatefulWidget {
  @override
  _SignUpFormState createState() => _SignUpFormState();
}

class _SignUpFormState extends State<SignUpForm> {
  final _formKey = GlobalKey<FormState>();
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        children: [
          TextFormField(
            controller: _emailController,
            decoration: InputDecoration(labelText: 'Email'),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Please enter your email';
              }
              if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
                return 'Enter a valid email';
              }
              return null;
            },
          ),
          TextFormField(
            controller: _passwordController,
            decoration: InputDecoration(labelText: 'Password'),
            obscureText: true,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Please enter your password';
              }
              if (value.length < 6) {
                return 'Password must be at least 6 characters long';
              }
              return null;
            },
          ),
          ElevatedButton(
            onPressed: () {
              if (_formKey.currentState!.validate()) {
                // Proceed with sign-up
                signUpWithEmail(_emailController.text, _passwordController.text);
              }
            },
            child: Text('Sign Up'),
          ),
        ],
      ),
    );
  }

  Future<void> signUpWithEmail(String email, String password) async {
    // Sign-up logic will be implemented here
  }
}

Sign-Up Functionality

Now, let’s implement the sign-up logic using Firebase Authentication. This involves creating a new user with the provided email and password.

import 'package:firebase_auth/firebase_auth.dart';

Future<void> signUpWithEmail(String email, String password) async {
  try {
    UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
      email: email,
      password: password,
    );
    // Navigate to home screen or display success message
    print('Sign-up successful!');
  } 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('An account already exists for that email.');
    }
  } catch (e) {
    print(e);
  }
}

Explanation:

  • FirebaseAuthException: This is used to handle specific authentication errors, such as weak passwords or duplicate email accounts. It’s crucial to provide user-friendly messages instead of console prints for a better user experience.
  • Error Handling: Always handle exceptions securely and avoid logging sensitive information.

Email and Password Sign-In

The sign-in process is similar to sign-up, but it involves authenticating existing users.

Sign-In Functionality

Implement the sign-in logic using Firebase Authentication:

Future<void> signInWithEmail(String email, String password) async {
  try {
    UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: email,
      password: password,
    );
    // Navigate to home screen or display success message
    print('Sign-in successful!');
  } 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.');
    }
  }
}

Explanation:

  • User Feedback: Instead of using print, consider using SnackBar or dialog boxes to inform users about the authentication status.
  • Security Considerations: Ensure that error messages do not reveal sensitive information that could be exploited.

Third-Party Authentication (e.g., Google Sign-In)

In addition to email/password authentication, you can offer users the option to sign in using their Google account.

Setting Up Google Sign-In

  1. Add the google_sign_in package to your pubspec.yaml:

    dependencies:
      google_sign_in: ^5.0.0
    
  2. Implement the Google Sign-In flow:

import 'package:google_sign_in/google_sign_in.dart';

Future<void> signInWithGoogle() async {
  final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

  if (googleUser == null) {
    // The user canceled the sign-in
    return;
  }

  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  final credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

  await FirebaseAuth.instance.signInWithCredential(credential);
  // Navigate to home screen or display success message
  print('Google Sign-in successful!');
}

Platform-Specific Configurations:

  • Android: Ensure that you have configured your android/app/google-services.json file and updated your AndroidManifest.xml with the necessary permissions and metadata.
  • iOS: Update your ios/Runner/Info.plist with the required configurations and ensure you have the correct GoogleService-Info.plist file.

Best Practices and Security Considerations

  • User Feedback: Always provide clear and concise feedback to users during the authentication process. Use UI elements like SnackBar or dialogs instead of console prints.
  • Validation: Implement thorough validation for user inputs to prevent invalid data from being processed.
  • Security: Handle exceptions securely and avoid logging sensitive information. Ensure that your app complies with privacy regulations and best practices.
  • Testing: Test your authentication flows thoroughly on different devices and platforms to ensure a seamless user experience.

Conclusion

Implementing sign-in and sign-up functionalities using Firebase Authentication in Flutter is a powerful way to manage user authentication in your app. By following the steps outlined in this guide, you can provide a secure and user-friendly authentication experience. Remember to handle errors gracefully and provide meaningful feedback to users. With these skills, you can enhance your app’s functionality and improve user engagement.

Quiz Time!

### What package is used for Firebase Authentication in Flutter? - [x] firebase_auth - [ ] firebase_core - [ ] firebase_database - [ ] firebase_storage > **Explanation:** The `firebase_auth` package is specifically used for implementing authentication features in Flutter applications. ### Which widget is commonly used to create input fields for email and password in a sign-up form? - [x] TextFormField - [ ] TextField - [ ] InputField - [ ] FormField > **Explanation:** `TextFormField` is used within a `Form` widget to create input fields with validation capabilities. ### What method is used to create a new user with email and password in Firebase Authentication? - [x] createUserWithEmailAndPassword - [ ] signInWithEmailAndPassword - [ ] signInWithCredential - [ ] createUser > **Explanation:** The `createUserWithEmailAndPassword` method is used to register a new user with an email and password. ### What exception is caught to handle specific authentication errors in Firebase? - [x] FirebaseAuthException - [ ] Exception - [ ] AuthException - [ ] FirebaseException > **Explanation:** `FirebaseAuthException` is used to catch and handle specific errors related to Firebase Authentication. ### What package is used for implementing Google Sign-In in Flutter? - [x] google_sign_in - [ ] google_auth - [ ] google_login - [ ] google_services > **Explanation:** The `google_sign_in` package is used to implement Google Sign-In functionality in Flutter applications. ### What should you use instead of console prints to provide user feedback in a Flutter app? - [x] SnackBar or dialogs - [ ] Toasts - [ ] Alerts - [ ] Console logs > **Explanation:** `SnackBar` or dialogs are recommended for providing user feedback in a Flutter app, as they offer a better user experience than console prints. ### What is the recommended way to handle sensitive information in authentication error messages? - [x] Avoid logging sensitive information - [ ] Log everything for debugging - [ ] Use print statements - [ ] Display detailed error messages to users > **Explanation:** Avoid logging sensitive information to ensure security and privacy compliance. ### Which method is used to sign in a user with Google credentials in Firebase Authentication? - [x] signInWithCredential - [ ] signInWithGoogle - [ ] signInWithEmailAndPassword - [ ] signInWithToken > **Explanation:** The `signInWithCredential` method is used to authenticate a user with Google credentials in Firebase Authentication. ### What should be included in the `pubspec.yaml` file to use Firebase Authentication? - [x] firebase_auth: ^3.0.0 - [ ] firebase_core: ^1.0.0 - [ ] firebase_database: ^2.0.0 - [ ] firebase_storage: ^4.0.0 > **Explanation:** The `firebase_auth` dependency should be included in the `pubspec.yaml` file to use Firebase Authentication. ### True or False: It is important to test authentication flows on different devices and platforms. - [x] True - [ ] False > **Explanation:** Testing authentication flows on different devices and platforms ensures a seamless user experience and helps identify potential issues.