Use Firebase authentication

This guide will help you understand how to change the authentication on ApparenceKit.
Apparencekit app template architecture when using firebase or not is the same.

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.

👉 You must have run the ApparenceKit CLI setup before using this guide.

Methods available in the template

  • Email and password sign in
  • Email and password sign up
  • Recover password
  • Sign in with Google
  • Sign in with Apple
  • Sign in with Facebook
  • Sign in with Twitter
  • Anonymous sign in
  • Link anonymous account to any provider (email, google, apple, etc...)
  • Logout

Activate Firebase Authentication

You must activate the authentication methods you want to use in your project before using them.

  1. Go to the Firebase Console and click on your project.
  2. Go to the Authentication section and enable the desired authentication methods.
  3. Regenerate your firebase configuration in the lib/firebase_options_[env].dart file.

How to add a new authentication method?

We wrap the FirebaseAuth instance in a provider so that we can fake or mock it in our tests. (FirebaseAuth is a singleton and cannot be mocked directly)

1. Add a new API method

Open the file

lib/modules/authentication/api/authentication_api.dart

Then you add for example a new method signInWithPhoneNumber:

2. Add a new repository method

Once you have added the API method, you need to add a new method in the repository.
You will find the repository in

lib/modules/authentication/repository/authentication_repository.Dart

Why split the API and the repository?

The API is the layer that will call the firebase SDK. The repository is the layer that will call multiple API methods to perform a specific action. Also it allows you to store the user data in a local database for example. A repository is responsible to handle the data sources and return it using our domain models. An API is reponsible to call a remote or local source and return the data directly.

Update your tests

You can now update the tests in :

test/modules/authentication/repositories/authentication_repository_test.dart

Check the code and comments in the files to understand how it works.

👌 I encourage you to regularly run your tests to make sure everything is working as expected. Even better is to write your tests before coding anything. As this is not a course I won't go more on the subject here.