<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eliza Camber</title>
    <description>The latest articles on DEV Community by Eliza Camber (@elizacamber).</description>
    <link>https://dev.to/elizacamber</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1024208%2F90124cbb-1c58-4734-b708-d0fe2ca36351.jpeg</url>
      <title>DEV Community: Eliza Camber</title>
      <link>https://dev.to/elizacamber</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elizacamber"/>
    <language>en</language>
    <item>
      <title>Login flow with Google Identity Services and Firebase</title>
      <dc:creator>Eliza Camber</dc:creator>
      <pubDate>Tue, 27 Jun 2023 15:59:13 +0000</pubDate>
      <link>https://dev.to/elizacamber/login-flow-with-google-identity-services-and-firebase-f1f</link>
      <guid>https://dev.to/elizacamber/login-flow-with-google-identity-services-and-firebase-f1f</guid>
      <description>&lt;p&gt;Most Android apps have some kind of authentication. For this post, we will see how this flow works using Google’s One Tap sign-in, Firebase and Amity.&lt;/p&gt;

&lt;p&gt;The tech stack we will be using is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kotlin script (KTS) for our Gradle&lt;/li&gt;
&lt;li&gt;Jetpack Compose for our UI&lt;/li&gt;
&lt;li&gt;MVVM architecture&lt;/li&gt;
&lt;li&gt;Hilt for dependency injection&lt;/li&gt;
&lt;li&gt;Amity’s Social SDK&lt;/li&gt;
&lt;li&gt;Google’s OneTap authentication&lt;/li&gt;
&lt;li&gt;Firebase authentication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The flow is as follows: first, we’ll check if Amity’s session is valid; if it is we’ll continue to our main flow, if not we will redirect our users to our log-in screen. There, we will first try to log our users in using Google; if they’ve logged in to our app before, this will succeed and then we can log in using Firebase. If not, we first need to sign up our users to our app, and then continue with Firebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;p&gt;Alright, let’s begin! First, to configure our project we’ll use the official guide. Since we don’t use Groovy, the dependencies are added as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins {
    ...
    id("com.google.gms.google-services") version "4.3.14"
}

dependencies {
    ...
    implementation("com.google.android.gms:play-services-auth:20.4.0")
}

apply(plugin = "com.google.gms.google-services")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*While you should use the provided libs.versions file (versions catalogue), for the sake of code readability in this post we’ll add the versions like this.&lt;/p&gt;

&lt;p&gt;Since we’re here, we’ll also add the Amity &amp;amp; Firebase dependencies that we’ll need later. Unfortunately, adding Firebase dependencies in the template cannot be done yet via the Android Studio’s Assistant without throwing exceptions, so we’ll add those manually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies {   
    ... 

    // Amity
    implementation("com.github.AmityCo.Amity-Social-Cloud-SDK-Android:amity-sdk:5.33.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.6.4")

    // Firebase
    implementation(platform("com.google.firebase:firebase-bom:31.1.1"))
    implementation("com.google.firebase:firebase-analytics-ktx")
    implementation("com.google.firebase:firebase-auth-ktx")
    implementation("com.google.android.gms:play-services-auth:20.4.0")
    implementation("com.google.firebase:firebase-firestore-ktx")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our last step is to configure our Firebase console. Per the official documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To use an authentication provider, you need to enable it in the &lt;a href="https://console.firebase.google.com/"&gt;Firebase console&lt;/a&gt;. Go to the Sign-in Method page in the Firebase Authentication section to enable Email/Password sign-in and any other identity providers you want for your app.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We’ll of course enable the Google sign-in. Then we’ll navigate to the project settings and add our SHA certificate fingerprint.&lt;/p&gt;

&lt;p&gt;Now we’re ready to start adding our login code! We first create the files we’ll need: &lt;code&gt;MainNavigation&lt;/code&gt; , &lt;code&gt;MainViewModel&lt;/code&gt; , &lt;code&gt;LoginScreen&lt;/code&gt; , &lt;code&gt;LoginViewModel&lt;/code&gt; , &lt;code&gt;AuthRepository&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Authentication status observation
&lt;/h2&gt;

&lt;p&gt;Our first Composable to be called is the &lt;code&gt;MainNavigation&lt;/code&gt;. To constantly check &lt;a href="https://docs.amity.co/core-concepts/session-state?utm_source=medium&amp;amp;utm_medium=blogposts&amp;amp;utm_campaign=docs-android"&gt;Amity’s session validity&lt;/a&gt; this is where we’ll monitor its state. Since we want our app’s state to reflect the session’s state we will map it to a &lt;code&gt;StateFlow&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/"&gt;&lt;code&gt;StateFlow&lt;/code&gt;&lt;/a&gt; is a state-holder observable flow that emits the current and new state updates to its collectors. The current state &lt;code&gt;value&lt;/code&gt; can also be read through its value property. In Android, &lt;code&gt;StateFlow&lt;/code&gt; is a great fit for classes that need to maintain an observable mutable state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We initiate and get the &lt;code&gt;SessionState&lt;/code&gt; in the &lt;code&gt;AuthRepository&lt;/code&gt;, then in our &lt;code&gt;MainViewModel&lt;/code&gt; we convert our flow to a &lt;code&gt;StateFlow&lt;/code&gt;, and finally in our &lt;code&gt;MainNavigation&lt;/code&gt; we observe it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override val amitySession = flow {
        emit(AmityCoreClient.currentSessionState)
        AmityCoreClient.observeSessionState().asFlow()
    }
val uiState: StateFlow&amp;lt;MainUiState&amp;gt; = authRepository
    .amitySession.map {
        when(it) {
            SessionState.NotLoggedIn,
            SessionState.Establishing -&amp;gt; MainUiState.LoggedOut
            SessionState.Established,
            SessionState.TokenExpired -&amp;gt; MainUiState.LoggedIn
            is SessionState.Terminated -&amp;gt; MainUiState.Banned
        }
    }
    .catch { Error(it) }
    .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), MainUiState.Loading)LaunchedEffect(lifecycleOwner) {
    viewModel.uiState.collect { state -&amp;gt;
        when(state) {
            MainUiState.Banned -&amp;gt; {} //TODO snackbar
            is MainUiState.Error -&amp;gt; {}  //TODO snackbar
            MainUiState.Loading -&amp;gt; { /* no-op */ }
            MainUiState.LoggedIn -&amp;gt; navController.navigate("main") { popUpTo(0) }
            MainUiState.LoggedOut -&amp;gt; navController.navigate("login") { popUpTo(0) }
        }
    }
}
LaunchedEffect(lifecycleOwner) {
    viewModel.uiState.collect { state -&amp;gt;
        when(state) {
            MainUiState.Banned -&amp;gt; showSnackbar(scope, snackbarHostState, userBannedText)
            is MainUiState.Error -&amp;gt; showSnackbar(scope, snackbarHostState, userErrorText)
            MainUiState.Loading -&amp;gt; { /* no-op */ }
            MainUiState.LoggedIn -&amp;gt; navController.navigate(Route.UsersList.route) { popUpTo(0) }
            MainUiState.LoggedOut -&amp;gt; navController.navigate(Route.Login.route) { popUpTo(0) }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool, now we can observe our users' state! 🎉🎉🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Google OneTap sign-in
&lt;/h2&gt;

&lt;p&gt;Our users now see our ✨shiny✨ login page which really for now is just the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Composable
fun LoginScreen(
    modifier: Modifier = Modifier,
    navigateToUsers: () -&amp;gt; Unit,
    viewModel: LoginViewModel = hiltViewModel()
) {
    Column(modifier) {
        Button(onClick = { /* TODO */ }) {
            Text(text = stringResource(R.string.login_google_bt))
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fPjFUVXh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wxlryiityycdnreu05b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fPjFUVXh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wxlryiityycdnreu05b.png" alt="Our login page" width="295" height="640"&gt;&lt;/a&gt;&lt;br&gt;
(Our login page)&lt;/p&gt;

&lt;p&gt;Not that shiny after all 😛&lt;/p&gt;

&lt;p&gt;To begin, we need our sign-in call in our &lt;code&gt;AuthRepository&lt;/code&gt;. *&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’re using an interface to encapsulate our &lt;code&gt;AuthRepository&lt;/code&gt;‘s functions. This will be specifically useful for creating a mock &lt;code&gt;AuthRepository&lt;/code&gt; for our tests in the future.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;signInRequest&lt;/code&gt;, &lt;code&gt;SignUpRequest&lt;/code&gt; as well as the Firebase and the Google clients are provided to our &lt;code&gt;AuthRepository&lt;/code&gt; implementation during the dependency injection as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module
@InstallIn(SingletonComponent::class)
class AuthModule {
    private val signInRequest = BeginSignInRequest.builder()
        .setGoogleIdTokenRequestOptions(
            BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
                .setSupported(true)
                .setServerClientId(BuildConfig.SERVER_CLIENT_ID)
                // Only show accounts previously used to sign in.
                .setFilterByAuthorizedAccounts(true)
                .build())
        // Automatically sign in when exactly one credential is retrieved.
        .setAutoSelectEnabled(true)
        .build()

    private val signUpRequest = BeginSignInRequest.builder()
        .setGoogleIdTokenRequestOptions(
            BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
                .setSupported(true)
                .setServerClientId(BuildConfig.SERVER_CLIENT_ID)
                .setFilterByAuthorizedAccounts(false)
                .build())
        .build()

    @Provides
    @Singleton
    fun provideAuthRepository(@ApplicationContext appContext: Context) : AuthRepository {
        return AuthRepositoryImp(
            Identity.getSignInClient(appContext),
            signInRequest,
            signUpRequest,
            Firebase.auth,
            Firebase.firestore
        )
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to our sign-in flow! As mentioned above, if it’s the first time our user’s trying to sign-in with this account, this will throw an exception. To avoid showing false error messages to the user, when we get an exception in our sign-in method, we’ll try to sign up. If that’s successful we move on, if not we then handle the error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//AuthRepositoryImpl.kt

override suspend fun signInWithGoogle(): OneTapResponse {
    return try {
        val result = oneTapClient.beginSignIn(signInRequest).await()
        ApiResponse.Success(result)
    } catch (e: Exception) {
        ApiResponse.Failure(e)
    }
}

override suspend fun signUpWithGoogle(): OneTapResponse {
    return try {
        val result = oneTapClient.beginSignIn(signUpRequest).await()
        ApiResponse.Success(result)
    } catch (e: Exception) {
        ApiResponse.Failure(e)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//LoginViewModel

suspend fun googleSignIn(launcher: ManagedActivityResultLauncher&amp;lt;IntentSenderRequest, ActivityResult&amp;gt;) {
    when (val oneTapResponse: ApiResponse&amp;lt;BeginSignInResult&amp;gt; =
        authRepository.signInWithGoogle()) {
        is ApiResponse.Success -&amp;gt; {
            val result = oneTapResponse.data!!
            val intent = IntentSenderRequest.Builder(result.pendingIntent.intentSender).build()
            launcher.launch(intent)
        }
        is ApiResponse.Loading -&amp;gt; { /* no-op */ }
        else -&amp;gt; {
            // No saved credentials found. Launch the One Tap sign-up flow
            googleSignUp(launcher)
        }
    }
}

private suspend fun googleSignUp(launcher: ManagedActivityResultLauncher&amp;lt;IntentSenderRequest, ActivityResult&amp;gt;) {
    when(val oneTapResponse: ApiResponse&amp;lt;BeginSignInResult&amp;gt; = authRepository.signUpWithGoogle())  {
        is ApiResponse.Success -&amp;gt; {
            val result = oneTapResponse.data!!
            val intent = IntentSenderRequest.Builder(result.pendingIntent.intentSender).build()
            launcher.launch(intent)
        }
        is ApiResponse.Loading -&amp;gt; { /* no-op */ }
        else -&amp;gt; handleSignUpError()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we were using Views instead of Compose, we would handle the result of the intent in our &lt;code&gt;onActivityForResult&lt;/code&gt; method. Instead, we will use the ManagedActivityResultLauncher in our &lt;code&gt;LoginScreen&lt;/code&gt;. If our launcher comes back with a positive result, we’ll then get our user’s ID and move on with the Firebase sign-in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Composable
fun LoginScreen(
    modifier: Modifier = Modifier,
    viewModel: LoginViewModel = hiltViewModel()
) {

    val launcher = rememberFirebaseAuthLauncher(viewModel = viewModel)
    val scope = rememberCoroutineScope()

    Column(modifier) {
        Button(onClick = { scope.launch { viewModel.googleSignIn(launcher) } }) {
            Text(text = stringResource(R.string.login_google_bt))
        }
    }
}

@Composable
private fun rememberFirebaseAuthLauncher(viewModel: LoginViewModel): ManagedActivityResultLauncher&amp;lt;IntentSenderRequest, ActivityResult&amp;gt; {
    val scope = rememberCoroutineScope()
    val context = LocalContext.current

    return rememberLauncherForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -&amp;gt;
        result.data.let {
            try {
                scope.launch {
                    val credentials =
                        Identity.getSignInClient(context).getSignInCredentialFromIntent(result.data)
                    val googleIdToken = credentials.googleIdToken
                    val googleCredentials = getCredential(googleIdToken, null)
                    // TO-DO sign-in to Firebase
                }
            } catch (e: Exception) {
                Log.e("LOG", e.message.toString())
                // TO-DO show error to user.
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Firebase and Amity sign-in
&lt;/h2&gt;

&lt;p&gt;Oof, almost done!!!&lt;/p&gt;

&lt;p&gt;Back to our &lt;code&gt;AuthRepository&lt;/code&gt; first, we add our calls for Firebase and Amity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override suspend fun firebaseSignInWithGoogle(googleCredential: AuthCredential): SignInToFirebaseResponse {
    return try {
        val authResult = auth.signInWithCredential(googleCredential).await()
        val isNewUser = authResult.additionalUserInfo?.isNewUser ?: false
        if (isNewUser) {
            addUserToFirestore()
        }
        ApiResponse.Success(true)
    } catch (e: Exception) {
        ApiResponse.Failure(e)
    }
}

override suspend fun amityLogIn() = login(userId = currentUserId)
    .build()
    .submit()
    .toSuspend()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the user is new we’ll also add it to Firestore as it’s needed for our chat app, but this is not mandatory. Then in our &lt;code&gt;LoginViewModel&lt;/code&gt; we call our suspend functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun firebaseSignIn(authCredential: AuthCredential) {
    viewModelScope.launch {
        authRepository.firebaseSignInWithGoogle(authCredential)
            .also { authRepository.amityLogIn() }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and replace our TO-DO with this call in our launcher.&lt;/p&gt;

&lt;h2&gt;
  
  
  Navigating away our login
&lt;/h2&gt;

&lt;p&gt;…or else observing our sign-in process state. Our last step, I promise!&lt;/p&gt;

&lt;p&gt;We’ll start from our &lt;code&gt;AuthRepository&lt;/code&gt; again, and add our final call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override val isSignedIn = callbackFlow {
    val authStateListener = FirebaseAuth.AuthStateListener { auth -&amp;gt;
        trySend(auth.currentUser != null)
    }
    auth.addAuthStateListener(authStateListener)
    awaitClose {
        auth.removeAuthStateListener(authStateListener)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since this is a callback, it means that every time the auth.currentUser value changes, we will get notified. Then in our &lt;code&gt;LoginViewModel&lt;/code&gt; we map it to a UI state, and of course observe it to our &lt;code&gt;LoginScreen&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// LoginViewModel
val uiState: StateFlow&amp;lt;LoginUiState&amp;gt; = authRepository
    .isSignedIn.map {if (it) LoginUiState.Authorized else LoginUiState.Unauthorized()}
    .catch { LoginUiState.Unauthorized(it) }
    .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), LoginUiState.Loading)
// LoginScreen
val authStatus by produceState&amp;lt;LoginUiState&amp;gt;(
    initialValue = LoginUiState.Unauthorized(),
    key1 = lifecycle,
    key2 = viewModel.uiState
) {
    lifecycle.repeatOnLifecycle(state = Lifecycle.State.STARTED) {
        viewModel.uiState.collect { value = it }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vJReb8mh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2rqrezjlabudr0gnwf9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJReb8mh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2rqrezjlabudr0gnwf9.png" alt="our happy, and …not that happy paths. Handling the errors during sign-in is very important!" width="590" height="640"&gt;&lt;/a&gt;&lt;br&gt;
(our happy, and …not that happy paths. Handling the errors during sign-in is very important!)&lt;/p&gt;

&lt;p&gt;WOOHOOOOOO! That's all folks!&lt;/p&gt;

&lt;p&gt;If you’re interested in how to use this code for a chat app, make sure to check our next blog post!&lt;/p&gt;

&lt;p&gt;The full code can be found &lt;a href="https://github.com/AmityCo/AmityFirebaseAuthenticationSample"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>firebase</category>
      <category>tutorial</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
