I am trying out firebase auth UI ins simple Jetpack project. I followed the instructions here.
But signed in UI flow is not show, instead it shows me the main activity which is an empty constraint layout.
My code is as follows
MainActivity.kt
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.firebase.ui.auth.AuthUI
import com.google.firebase.auth.FirebaseAuth
class MainActivity : AppCompatActivity() {
private lateinit var mFirebaseAuth: FirebaseAuth
private lateinit var mAuthStateListener: FirebaseAuth.AuthStateListener
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize Firebase Auth
mFirebaseAuth = FirebaseAuth.getInstance()
mAuthStateListener = FirebaseAuth.AuthStateListener { firebaseAuth -> // line 25
val user = firebaseAuth.currentUser
if (user != null){
Toast.makeText(applicationContext, "Already signed in", Toast.LENGTH_LONG).show()
}
else{
// Choose authentication providers
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build())
// Create and launch sign-in intent
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setAvailableProviders(providers)
.setTheme(R.style.AppTheme) // Set theme
.setAvailableProviders(providers)
.setTosAndPrivacyPolicyUrls(
"https://example.com/terms.html",
"https://example.com/privacy.html")
.build(),
RC_SIGN_IN)
}
}
}
public override fun onStart() {
super.onStart()
mFirebaseAuth.addAuthStateListener { mAuthStateListener }
}
override fun onPause() {
super.onPause()
mFirebaseAuth.removeAuthStateListener { mAuthStateListener }
}
companion object {
private const val RC_SIGN_IN = 123
}
}
Dependencies
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// firebase
implementation 'com.firebaseui:firebase-ui-auth:4.3.1'
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
}
For providers as you can see I am only using Email and Google signin. Those providers are enabled in the firebase console. I also have a fingerprint specified in the project(which was there by default).
I tried debugging by setting breakpoint at line 25, I saw that the AuthStateListener
lambda was not executed. I do not know why.
Please help!
UPDATE :
According to pointers given by Peter in his answer, dependencies were updated as mentioned here, but the issue still exists.
Actually after posting an issue on github repo, I came to know that the sign in flow appear when there is an internet connection (in my case it was keeping Wifi turned ON).
So this issue allowed one of members of the repo to identify a flaw in Firebase Auth UI (Android), he created a pull request that allows end users to pick auth method when they are offline.