onAuthStateChange()如何工作以及它在kotlin中的代码?

时间:2018-02-20 13:23:01

标签: firebase kotlin firebase-authentication kotlin-android-extensions

我正在尝试在状态发生变化时记录消息,但显然它不起作用,我不确定我哪里出错了。

我希望在状态更改为用户登录时更新UI,但对于初学者,我只是尝试记录消息。

我也不会安静地理解注册并取消注册。

这是我登录活动的代码。



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

        }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您永远不会为mAuthListener分配非空值。这是唯一一次分配:

private var mAuthListener : FirebaseAuth.AuthStateListener? = null

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

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

您还可以创建一个身份验证状态侦听器,但永远不会将该对象分配到任何位置,因此它不会执行任何操作:

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

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