【问题标题】:Using Kotlin and Firebase to trigger a sendEmailVerification with onAuthStateChanged callback使用 Kotlin 和 Firebase 通过 onAuthStateChanged 回调触发 sendEmailVerification
【发布时间】:2017-12-17 17:08:47
【问题描述】:

我正在开始一个新的 Android 项目并决定在其中使用 KotlinFirebase,现在我可以在我的电脑上使用 createUserWithEmailAndPassword 成功创建用户完成 createUserWithEmailAndPassword 后,SignupActivity 和我的用户都成功登录。

现在我正在尝试使用onAuthStateChanged(FirebaseAuth auth)FirebaseAuth.AuthStateListener 上触发的回调事件来进一步获取它,但是我在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

        }
        // ...
    }
};

我的 Kotlin 代码

    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();
        }
    }

我出于调试目的放置了一些日志和 toast 元素,但它们都没有被触发,我认为 FirebaseAuth.AuthStateListener 中缺少 onAuthStateChanged 但我不知道如何解决它。

如果有人能给我一些关于我做错了什么的建议,我将不胜感激。

提前致谢。

【问题讨论】:

  • 不用问,您是否将AuthStateListener 添加到 FirebaseAuth 对象?
  • 你能详细说明一下吗?我使用AuthStateListener的唯一部分是我发布的sn-p,我需要把它放在我活动的其他部分吗?谢谢@chandil03
  • 您刚刚初始化了AuthStateListener 对象,现在您必须将其分配给 FirebaseAuth 对象,以告知这是身份验证状态更改时要联系的人。
  • 谢谢@chandil03,我该如何做这个作业?当我使用 createUserWithEmailAndPassword 时,它非常简单(只需声明 FirebaseAuth 对象并调用该方法)但我不知道侦听器上缺少哪个分配。
  • 检查 firebaseAuth 对象上的 add..stateListener() 方法,由 FirebaseAuth.getInstance() 方法创建。

标签: android firebase firebase-authentication listener kotlin


【解决方案1】:

这对我有帮助,在调用 addAuthStateListener 时请注意括号 - 作为 kotlin 的新手,我使用了 { } 卷曲的:

    public override fun onStart() {
      super.onStart()
      firebaseAuth.addAuthStateListener(authStateListener)
    }

    public override fun onPause() {
       super.onPause()
       firebaseAuth.removeAuthStateListener(authStateListener)
    }

【讨论】:

  • 是的@khusrav,这就是我将AuthStateListener 分配给FirebaseAuth 对象以告诉它在身份验证状态更改时联系的方式,唯一的是我使用了 onStart() 和 onStop( ) 解决这个问题时。
  • 我将此提交用作基础,来自 Google 的 @samtstern 向我解释了为什么他们从 Google Firebase 示例中删除了 AuthStateListener,我希望它对某人有所帮助:github.com/firebase/quickstart-android/commit/…
  • 他们不建议再使用它了吗?
  • 哦,好的,所以它仍然可以像以前一样使用。但是看看提交中的代码,没有它看起来更干净。
  • 确实有,但早些时候他们遇到了问题,因为 AuthStateListener 的行为不同,开发人员感到困惑,为了解决这个问题,他们使用旧行为制作了一些名为 IdTokenListener 的新 AuthListener,请查看我发布的链接并转到 cmets 部分。很高兴知道我认为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2016-10-07
  • 2019-03-30
  • 2017-04-10
  • 1970-01-01
  • 2014-01-26
  • 2017-01-06
相关资源
最近更新 更多