MOBILE APP DEVELOPMENT
Kotlin / Wasm — Auth part 1: Google Sign in
Back in my Kotlin/JS auth series I promised that if anyone needed a guide for integrating Firebase authentication with Kotlin/Wasm, I’d make time to create one. Well, a promise is a promise. And just like that series, this one exists because I expected it to be easy. It wasn’t. Not because Wasm is hard, but because the tooling hasn’t caught up yet. The catch is that the Firebase-Kotlin-SDK we used for JS doesn’t support the Wasm target. There’s an open issue from 2023 and even a prototype PR that was closed without merging. So this time there’s no wrapper. We’re going to talk to the Firebase JS SDK directly. The good news? It’s less scary than it sounds, and you’ll learn how Kotlin/Wasm JS interop actually works along the way. As before, this guide won’t cover the Firebase console setup. Creating a project, registering a Web app and enabling the Google provider is well documented here. Let’s start with the Gradle setup. We add the wasmJs target and, since there's no Kotlin wrapper, we pull the Firebase JS SDK straight from npm:
kotlin { @OptIn(ExperimentalWasmDsl::class) wasmJs { browser() binaries.executable() } sourceSets { wasmJsMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-browser:0.3") implementation(npm("firebase", "12.18.0")) } } } One thing to watch out for: if your project also has a js target using the Firebase-Kotlin-SDK, those dependencies need to move from commonMain to jsMain, because they won't resolve for Wasm. Now to the essential part. If you read my Google Sign-in article for JS, you might remember externals.auth.kt, the file in the GitLive library bridging the gap between Kotlin and external JavaScript modules. This time we write that bridge ourselves. It's surprisingly little code. Two files, one per Firebase module: @file:JsModule("firebase/app") package com.kmp.webauth.externals external interface FirebaseApp : JsAny external fun initializeApp(options: JsAny): FirebaseApp @file:JsModule("firebase/auth") package com.kmp.webauth.externals import kotlin.js.Promise external interface Auth : JsAny external interface User : JsAny { val uid: String val displayName: String? val email: String? } external interface UserCredential : JsAny { val user: User } external class GoogleAuthProvider() : JsAny external fun getAuth(app: FirebaseApp): Auth external fun signInWithPopup(auth: Auth, provider: GoogleAuthProvider): Promise<UserCredential> That’s the whole “SDK”. The @file:JsModule annotation tells the compiler these declarations come from the firebase/auth npm module, and webpack takes care of the rest. Notice that everything extends JsAny. In Kotlin/Wasm, JavaScript values live on the other side of a strict boundary, and JsAny is your passport across it. Next, initialization. In Kotlin/JS we’d just throw together a json() or use dynamic. Wasm has neither. The way to build a plain JS object is a js() function with an object literal: private fun firebaseOptions( apiKey: String, authDomain: String, projectId: String, appId: String, ): JsAny = js("({ apiKey: apiKey, authDomain: authDomain, projectId: projectId, appId: appId })") val firebaseAuth: Auth by lazy { val app = initializeApp( firebaseOptions( apiKey = "yourApiKey", authDomain = "your-project.firebaseapp.com", projectId = "your-project", appId = "yourAppId", ) ) getAuth(app) } The values come from the same place as always: Firebase project -> Settings -> Web application -> firebaseConfig. And finally, the sign-in function itself. If you’ve read the JS articles this will look very familiar: fun signInWithGoogle( onSuccess: (displayName: String) -> Unit, onFailure: (message: String) -> Unit, ) { signInWithPopup(firebaseAuth, GoogleAuthProvider()) .then { credential -> onSuccess(credential.user.displayName ?: credential.user.email ?: credential.user.uid) null } .catch { error -> onFailure(messageOf(error)) null } } private fun messageOf(error: JsAny?): String = js("String((error && error.message) || error)") Two Wasm quirks here. First, the null at the end of each lambda: Promise callbacks must return a JsAny?, and Unit doesn't qualify, so we hand back null explicitly. Second, the error arriving in catch is a JsAny, not a Throwable, so extracting the message goes through another tiny js() helper. If you’re sharing UI between targets, wrap it in an expect fun signInWithGoogle(...) in commonMain. The Wasm actual is the function above, and the JS actual can keep delegating to the Firebase-Kotlin-SDK. Call it from any composable button and voilà! One last tip before you test: Safari blocks the popup by default, and Firebase’s signInWithRedirect fallback doesn't play nicely with Safari's tracking prevention either. Test in Chrome first, then decide how much you care about Safari. Big shoutout to GitLive for the Firebase-Kotlin-SDK, their externals files were the blueprint for the declarations above. And if you'd like a guide for another provider on Wasm, drop a comment below and I'll make some time to create one for you! --- Kotlin / Wasm — Auth part 1: Google Sign in was originally published in ProAndroidDev on Medium, where people are continuing the conversation by highlighting and responding to this story.