onAuthStateChange() 是如何工作的,它是 kotlin 中的代码?
how does onAuthStateChange() works and it's code in kotlin?
我正在尝试在状态发生变化时记录消息,但显然它不起作用,我不确定哪里出错了。
我想在状态更改为用户已登录时更新 UI,但对于初学者来说,我只是想记录一条消息。
注册和注销我也搞不懂
这是我的登录 activity 的代码。
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 分配 non-null 值。这是唯一一次分配:
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 吗?
我正在尝试在状态发生变化时记录消息,但显然它不起作用,我不确定哪里出错了。
我想在状态更改为用户已登录时更新 UI,但对于初学者来说,我只是想记录一条消息。
注册和注销我也搞不懂
这是我的登录 activity 的代码。
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 分配 non-null 值。这是唯一一次分配:
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 吗?