# Firebase support

This documentation outlines the usage of Firebase wrapper in a multi-platform project that supports both Android and iOS platforms. The Firebase module is defined using Koin and provides a set of authentication functions through the KickstarterAuth interface, which can be called by both platforms. Additionally, the KickstarterFirebaseFirestore class provides methods to perform CRUD operations on data transfer objects.

### Initialization <a href="#initialization" id="initialization"></a>

Firebase is set up as the default initialization in AppKickstarter. However, the project is designed to allow for the possibility of plugging in other backends like Supabase, Ktor, and others.

To initialize Firebase in the Android project, Koin is used along with the initFirebase function:

```
initFirebase(this)

initKoin {
    modules(
        listOf(
            module { androidContext(this@KickstarterApplication) },
            androidAppModule,
            firebaseModule
        )
    )
}
```

The firebaseModule is defined as:

```
val firebaseModule = module {
    single { Firebase.firestore }
    single { Firebase.auth }
    singleOf(::KickstarterFirebaseAuth).bind(KickstarterAuth::class)
    singleOf(::KickstarterFirebaseFirestore).bind(KickstarterRemoteDB::class)
}
```

### Authentication <a href="#authentication" id="authentication"></a>

The KickstarterAuth interface defines all the possible actions of the backend responsible for authentication. These actions include :

```
suspend fun signout()
suspend fun getCurrentUser(): CurrentUser?
suspend fun observeAuthStateChanged(): Flow<CurrentUser?>
suspend fun observeSessionStatus(): Flow<SessionStatus>
suspend fun signInAnonymously(): CurrentUser?
suspend fun resetPassword(email: String)
suspend fun updateProfile(displayName: String?, photoUrl: String?)

@Throws(
    BadEmailFormatException::class,
    PasswordInvalidException::class,
    EmptyOrNullValueException::class,
    EmailAlreadyUsedException::class,
    CancellationException::class
)
suspend fun createUserWithEmailAndPassword(email: String, password: String): CurrentUser?

@Throws(
    BadEmailFormatException::class,
    PasswordInvalidException::class,
    EmptyOrNullValueException::class,
    CancellationException::class
)
suspend fun signInWithEmailAndPassword(email: String, password: String): CurrentUser?
```

### Data transfer objects <a href="#data-transfer-objects" id="data-transfer-objects"></a>

The KickstarterFirebaseFirestore class provides methods to perform CRUD operations on data transfer objects. For example, the uploadPost(post: Post) function allows creating or updating a post. It uses the PostDto class to handle data transfer objects. The getAllPosts() function fetches all the posts from the Firestore database.

```
class KickstarterFirebaseFirestore(
    private val firestore: FirebaseFirestore,
    private val auth: KickstarterAuth,
) : KickstarterRemoteDB {

    override suspend fun uploadPost(post: Post): String {
        val postDto = post.toDto() 
        return if (postDto.remoteId == null) {
            create(postDto) 
        } else {
            update(postDto) 
        }
    }

    override suspend fun getAllPosts(): List<PostDto> {
        return getAll("posts")
            .map {
                it.data(strategy = PostDto.serializer()).toPost()
            }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.appkickstarter.com/features/firebase-support.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
