SDPCoachMe/SDP-2023

View on GitHub
app/src/main/java/com/github/sdpcoachme/auth/GoogleAuthenticator.kt

Summary

Maintainability
A
3 hrs
Test Coverage
F
53%
package com.github.sdpcoachme.auth

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import com.firebase.ui.auth.AuthUI
import com.firebase.ui.auth.AuthUI.IdpConfig.GoogleBuilder
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult
import com.google.firebase.auth.FirebaseAuth
import java.util.function.Consumer

/**
 * Class that handles the Google sign in process
 */
class GoogleAuthenticator : Authenticator {

    /**
     * Creates a sign in intent and launches it using the given launcher
     *
     * @param signInLauncher the launcher to use
     */
    private fun createSignInIntent(signInLauncher: ActivityResultLauncher<Intent>) {
        // Choose authentication providers
        val providers = listOf(
            GoogleBuilder().build()
        )
        val signInIntent = AuthUI.getInstance()
            .createSignInIntentBuilder()
            .setAvailableProviders(providers)
            .build()

        signInLauncher.launch(signInIntent)
    }

    override fun onSignInResult(
        result: FirebaseAuthUIAuthenticationResult?,
        onSuccess: Consumer<String?>?,
        onFailure: Consumer<String?>?
    ) {
        if (result == null) {
            onFailure!!.accept("login error")
        } else if (result.resultCode == Activity.RESULT_OK) {
            // Successfully signed in
            println("Successfully signed in")
            val user = FirebaseAuth.getInstance().currentUser
                ?: throw IllegalStateException("User is null")
            onSuccess!!.accept(user.email)
        } else if (result.resultCode == Activity.RESULT_CANCELED) {
            // Sign in was cancelled by the user
            onFailure!!.accept("User cancelled sign in")
        } else {
            val error = result.idpResponse!!.error
            // Handle the error here
            // ...
            onFailure!!.accept("login error: $error")
        }
    }

    override fun signIn(signInLauncher: ActivityResultLauncher<Intent>) {
        createSignInIntent(signInLauncher)
    }

    override fun delete(context: Context?, onComplete: Runnable?) {
        AuthUI.getInstance()
            .delete(context!!)
            .addOnCompleteListener { onComplete!!.run() }
    }

    override fun signOut(context: Context?, onComplete: Runnable?) {
        AuthUI.getInstance()
            .signOut(context!!)
            .addOnCompleteListener { onComplete!!.run() }
    }

    override fun isSignedIn(): Boolean {
        return FirebaseAuth.getInstance().currentUser != null
    }
}