設置 FireBase Authentication
Step 1 點選 Tool 會看到 FireBase
Step 2 點選 Authentication
Step 3 需要去連接 FireBase帳號
Step 4 建立一個 新的 FireBase 專案 或 選擇舊的 FireBase 專案
Step 5 import FireBase Authentication 套件
功能
登入
val auth = FirebaseAuth.getInstance()
auth.signInWithEmailAndPassword(acount, password).addOnCompleteListener(this, OnCompleteListener { task ->
if(task.isSuccessful) {
Toast.makeText(this, "Successfully Logged In", Toast.LENGTH_LONG).show()
}
else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show()
}
})
登出
FirebaseAuth.getInstance().signOut()
註冊
val auth = FirebaseAuth.getInstance()
auth.createUserWithEmailAndPassword(acount, password).addOnCompleteListener(this, OnCompleteListener{ task ->
if(task.isSuccessful){
Toast.makeText(this, "Registration Successed", Toast.LENGTH_LONG).show()
}else {
// If sign in fails, display a message to the user.
Log.d("TAG", "createUserWithEmail:failure", task.exception)
Toast.makeText(this, "Registration Failed", Toast.LENGTH_LONG).show()
}
})
重設密碼
val auth = FirebaseAuth.getInstance()
auth.sendPasswordResetEmail(email)
.addOnCompleteListener(this, OnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(this, "Reset link sent to your email", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Unable to send reset mail", Toast.LENGTH_LONG).show()
}
})
FireBase 要記得設定認證選項
在FireBase 可以看到 使用者資料