onAuthStateChange()如何工作,它是kotlin中的代码? [英] how does onAuthStateChange() works and it's code in kotlin?

查看:185
本文介绍了onAuthStateChange()如何工作,它是kotlin中的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当状态发生变化时,我正在尝试记录一条消息,但是显然它不起作用,我不确定我要去哪里.

I am trying to log a message when there is a change in state, but apparently it's not working, I am not sure where I am going wrong.

当状态更改为用户登录时,我想更新UI,但是对于初学者来说,我仅尝试记录消息.

I want to update the UI when the state is changed to user logged in, but for starters I am only trying to log a message.

我也不是很安静地了解它的注册和注销.

I also don't quiet understand the registering it and unregistering it.

这是我的登录活动的代码.

This is my login activity's code.

    val TAG = "LoginActivity"
    //private var mDatabaseReference: DatabaseReference? = null
    //private var mDatabase: FirebaseDatabase? = null

    // Firebase refferences for Authentication.
    private var mAuth: FirebaseAuth? = null
    private var mUser : FirebaseUser? = null
    private var mDatabase : DatabaseReference? = null
    private var mAuthListener : FirebaseAuth.AuthStateListener? = null

    //global variables
    private var emailString : String? = null
    private var passwordString : String? = null

    // ActivityState : ONCREATE.
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        // initializing firebase Auth and database Reference.
        mAuth = FirebaseAuth.getInstance()
        mDatabase = FirebaseDatabase.getInstance().reference
        // getting the currently logined user.
        mUser = mAuth?.currentUser

        //[START auth_state_listener]
        FirebaseAuth.AuthStateListener { firebaseAuth ->
            val cuser = firebaseAuth.currentUser
            if(mUser != null) {
                Log.d("WOWOWOWOWO : ", "you dont girl!")
            }
        }

    }
    //ActivityState : ONSTART.
    override fun onStart() {
        super.onStart()

        mAuth?.addAuthStateListener(mAuthListener!!)
    }
    //ActivityState : ONPAUSE.
    override fun onPause() {
        super.onPause()


    }

    // function for when the login button is clicked.
    // TODO : Login Activity : Function# 1.
     fun loginBtnClicked(view : View) {

         emailString = loginEmailTxt.text.toString()
         passwordString = loginPasswordtxt.text.toString()

         if(!emailString.isNullOrEmpty() && !passwordString.isNullOrEmpty()) {

             // Checking if the login cridentials are correct. and then changing the Auth State to logged in.
             //TODO : Login Activity : Function# 3.
             mAuth!!.signInWithEmailAndPassword(emailString!!, passwordString!!).addOnCompleteListener(this) { task ->

                 if(task.isSuccessful) {

                     // User ID token retrival  TODO: not sure what to do with the token yet.
                     mUser!!.getIdToken(true)
                             .addOnCompleteListener(OnCompleteListener {task: Task<GetTokenResult> ->
                                 if(task.isSuccessful) {
                                    var idToken = task.getResult().token
                                     Log.d(TAG, "signInWithEmail:success :" + idToken)
                                 } else {

                                 }
                             }) 
                 } else {

                     Log.e(TAG, "signInWithEmail:failure", task.exception)
                     Toast.makeText(this@LoginActivity, "Authentication failed. Make sure email and password are correct",
                             Toast.LENGTH_SHORT).show()
                 }
             } 

         } else {
             Toast.makeText(this, "Email or Password can not be empty.", Toast.LENGTH_LONG).show()
         } 
    } 

    // TODO : Login Activity : Function#2
     fun getHelpImgClicked(view : View) {
        val builder = AlertDialog.Builder(this)
        val dialogView = layoutInflater.inflate(R.layout.get_help_dialog, null)
        builder.setView(dialogView)
                .setNegativeButton("Close" ){ _, _ -> }.show()

        }

推荐答案

您永远不会为您的mAuthListener分配非null值.这是唯一分配的时间:

You're never assigning a non-null value to your mAuthListener. This is the only time it's assigned:

private var mAuthListener : FirebaseAuth.AuthStateListener? = null

这是您传递该null值以成为身份验证状态侦听器的地方:

And this is where you pass that null value to become an auth state listener:

override fun onStart() {
    super.onStart()
    mAuth?.addAuthStateListener(mAuthListener!!)
}

您还创建了一个身份验证状态侦听器,但是从不在任何地方分配该对象,因此它不会执行任何操作:

You also create an auth state listener, but never assign that object anywhere, so it doesn't do anything:

    //[START auth_state_listener]
    FirebaseAuth.AuthStateListener { firebaseAuth ->
        val cuser = firebaseAuth.currentUser
        if(mUser != null) {
            Log.d("WOWOWOWOWO : ", "you dont girl!")
        }
    }

您是要获取AuthStateListener对象并在onCreate()期间将其分配给mAuthListener吗?

Did you mean to take that AuthStateListener object and assign it to mAuthListener during onCreate()?

这篇关于onAuthStateChange()如何工作,它是kotlin中的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆