Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use Firebase (Authentication) with Kotlin Multiplatform?

I have a project which uses Firebase Authentication in Android. It works well and I want to port the same project to an iOS App using code sharing with Kotlin Multiplatform.

I initially thought I could simple create a

expect class FirebaseAuth

and

//AndroidMain
actual class FirebaseAuth

//iOSMain
actual class FirebaseAuth

But I don't really know how I could use the iOS version of FirebaseAuth in iOSMain? Can someone guide me here?

like image 537
Archie G. Quiñones Avatar asked Jan 07 '20 05:01

Archie G. Quiñones


3 Answers

From now on there is a new official library about Kotlin Multiplatform firebase products. Supported platforms are Android, iOS and JavaScript.

https://firebaseopensource.com/projects/gitliveapp/firebase-kotlin-sdk/

like image 53
Vladimir Petrovski Avatar answered Nov 09 '22 10:11

Vladimir Petrovski


Note: This is an old answer. Check out https://github.com/gitliveapp/firebase-kotlin-sdk/ for a reasonably maintained library.

I gave a talk on libraries for KMP and specifically built a wrapper around Firestore to go along with it.

https://github.com/touchlab/FirestoreKMP

https://vimeo.com/371460823

In that library I create extension functions to implement features. Your question is kind of broad, but I'd probably start with:

//common
expect fun FirebaseAuth.signIn(email:String, password:String):Task<AuthDataResult>

//ios main
actual fun FirebaseAuth.signIn(email:String, password:String):Task<AuthDataResult> = signInWithEmail(email, password) //Figure out async return value

The async return stuff can be a little tricky, but the firestore code will have examples. One here:

https://github.com/touchlab/FirestoreKMP/blob/master/firestore/src/commonMain/kotlin/co/touchlab/firebase/firestore/Query.kt#L17

like image 4
Kevin Galligan Avatar answered Nov 09 '22 08:11

Kevin Galligan


I am building and publishing Firebase Wrappers for Kotlin Multiplatform!

Keep in mind that only common API between JS, JVM and iOS are available.

Here it is: https://github.com/lamba92/firebase-multiplatform

like image 3
Lamberto Basti Avatar answered Nov 09 '22 10:11

Lamberto Basti