使用Kotlin和Firebase通过onAuthStateChanged回调触发sendEmailVerification
我开始一个新的Android项目,并决定使用Kotlin和Firebase ,现在我可以在SignupActivity上使用createUserWithEmailAndPassword
成功创建用户,并且当createUserWithEmailAndPassword完成时,我的用户已成功登录。
现在我试图进一步使用在FirebaseAuth.AuthStateListener上使用onAuthStateChanged(FirebaseAuth auth)
触发的回调事件,但是在我的onCreate(savedInstanceState: Bundle?)
函数中创建的侦听器没有被触发,我缺乏将Java代码转换为Kotlin的经验并不能帮助我找出根本问题。
我有一些基于Java的示例代码是这样的:
Java示例
onCreate(...// mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in // NOTE: this Activity should get onpen only when the user is not signed in, otherwise // the user will receive another verification email. sendVerificationEmail(); } else { // User is signed out } // ... } };
我的科特林代码
FirebaseAuth.AuthStateListener { auth -> val user = auth.currentUser if(user != null){ // User is signed in Log.d(TAG, "Signed in") Toast.makeText(this, "User", Toast.LENGTH_LONG).show(); sendVerificationEmail() }else{ // User is signed out Log.d(TAG, "Signed out") Toast.makeText(this, "Null", Toast.LENGTH_LONG).show(); } }
我把一些日志和吐司元素进行调试的目的,但他们都没有得到触发,我想在FirebaseAuth.AuthStateListener内缺少onAuthStateChanged
,但我不知道如何解决它。
如果任何人都可以给我一些建议,我在做什么错,这将是非常感激。
提前致谢。
这是什么帮助我,在调用addAuthStateListener
时注意括号 – 对于kotlin是新的我使用{ }
卷曲的:
public override fun onStart() { super.onStart() firebaseAuth.addAuthStateListener(authStateListener) } public override fun onPause() { super.onPause() firebaseAuth.removeAuthStateListener(authStateListener) }