firebase๋ก sns๋ง๋ค๊ธฐ ํ๋ก์ ํธ์์ ํ์ ๊ฐ์ ๊ธฐ๋ฅ์ ๋ง๋ค์ด๋ณด๋๋ก ํ์
android ํ๋ก์ ํธ firebase์ ์ฐ๋ํ๋ ๋ฐฉ๋ฒhttps://lakue.tistory.com/37
1. ์ ๋ ฅ๊ฐ ๋ฐ์์ค๊ธฐ
val name = binding.newName.text.toString()
val email = binding.newEmail.text.toString()
val id = binding.newId.text.toString()
val pw = binding.newPw.text.toString()
2. createuserEmailAndPassword๋ก ์ ์ ๋ง๋ค๊ธฐ
val auth = FirebaseAuth.getInstance()
๋จผ์ firebase auth๋ณ์๋ฅผ ๋ง๋ ๋ค
๊ทธ๋ค์ addOnCompleteListener๋ฅผ ์ด์ฉํด ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค.
auth.createUserWithEmailAndPassword(email,pw)
.addOnCompleteListener(this, object: OnCompleteListener<AuthResult>{
override fun onComplete(p0: Task<AuthResult>) {
. . .
}
}
addOnCompleteListener์์ ๊ธฐ๋ฅ์ ์์ฑํ๋ค.
1) ์์ธ ์ฒ๋ฆฌ๋ฅผ ๋จผ์ ํด์ค๋ค
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(email) || TextUtils.isEmpty(id) || TextUtils.isEmpty(pw)) {
binding.warning.visibility = View.VISIBLE
}
if (pw.length < 6) {
Toast.makeText(this@NewUserActivity, "๋น๋ฐ๋ฒํธ๋ 6์๋ฆฌ ์ด์์ด์ด์ผ ํฉ๋๋ค..", Toast.LENGTH_SHORT).show()
}
2) ๋ฐ์ดํฐ๋ฅผ hashMap์ ๋ฃ์ด์ค๋ค
val firebaseUser: FirebaseUser? = auth.currentUser
val userid = firebaseUser?.uid
val reference = FirebaseDatabase.getInstance().reference.child("Users").child(userid!!)
val hashMap: HashMap<String, Any> = HashMap()
hashMap["id"] = userid
hashMap["username"] = name.lowercase(Locale.getDefault())
hashMap["email"] = email
hashMap["pw"] = pw
hashMap["imageurl"] = "์ด๋ฏธ์ง ํ์ผ"
3) firebase ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ ๋ฅผ ์ถ๊ฐํ๋ค(setValue ์ด์ฉ).
reference.setValue(hashMap).addOnCompleteListener(object: OnCompleteListener<Void> {
override fun onComplete(p0: Task<Void>) {
if (p0.isSuccessful) {
Toast.makeText(this@NewUserActivity, "๊ฐ์
์ด ์๋ฃ๋์์ต๋๋ค.", Toast.LENGTH_SHORT).show()
val intent = Intent(this@NewUserActivity, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
})
4) ์ค๋ณต ์ด๋ฉ์ผ์ ๋ํ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ค๋ค.
try {
p0.result
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(this@NewUserActivity, "์ด๋ฏธ ์๋ ์ด๋ฉ์ผ ํ์์
๋๋ค.", Toast.LENGTH_SHORT).show()
}
์คํ ๊ฒฐ๊ณผ
์ด๋ ๊ฒ ๋ฐ์ดํฐ๊ฐ ์ ๋ฌ๋๋ฉด ์ฑ๊ณต!
728x90