Use your own backend
This guide will help you to install your REST API authentication with our app template.
Apparencekit app template architecture makes it easy to setup your own backend.
📄 TLDR
The only things you need to change are our*_api.dart
files.
These files are directly responsible for the the api calls.
How it works?
We basically have 4 layers
- The API layer
- The repository layer
- The domain layer
- The UI layer
Why split the API and the repository?
The API is the layer that will call the outside world. 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.
The API layer
Open the file
lib/modules/authentication/api/authentication_api.dart
You will see that we have a class HttpAuthenticationApi
that implements AuthenticationApi
.
The AuthenticationApi
is an abstract class that contains all the methods that we need to implement.
But you will see that you will be able to customize easily.
Api are our contract with the outside world. It's the only thing that our unit test don't cover.
Our dio client is already configured and will add the authentication token using the "Authentication Bearer token" header.
The repository layer
The repository layer is the layer that will call the API layer and return the data using our domain models. It is also responsible to store the data in a local database for example. It is important to note that the repository layer is the only layer that will be called by the UI layer.
How the authentication token is stored?
The authentication storage is handled by the AuthSecuredStorage api class. You will find it in the file
lib/core/security/secured_storage.dart
How to add a new authentication method?
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
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.
How to customize the Http authorization header?
Open the file
lib/core/data/api/http_client.dart
We use the dio client to make our API calls.
The dio client is already configured to add the authentication token using the "Authentication Bearer token" header.
Only one instance is created and used in all our api.
But you can customize it as you want.