๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ| Android/๐Ÿ”ฅ | Firebase

[Andorid, Kotlin] firebase sns ์•ฑ ๋งŒ๋“ค๊ธฐ 1(ํšŒ์› ๊ฐ€์ž…)

by immgga 2022. 6. 19.

firebase๋กœ sns๋งŒ๋“ค๊ธฐ ํ”„๋กœ์ ํŠธ์—์„œ ํšŒ์› ๊ฐ€์ž… ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜์ž

 

android ํ”„๋กœ์ ํŠธ firebase์— ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•https://lakue.tistory.com/37

 

[Android/์•ˆ๋“œ๋กœ์ด๋“œ] Android Studio Firebase ์—ฐ๋™ (1)

์ด๋ฒˆ ๊ธ€์—์„  ์•ˆ๋“œ๋กœ์ด๋“œ์™€ ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋ฅผ ์—ฐ๋™์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ˆ๋“œ๋กœ์ด๋“œ์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ํŒŒ์ด์–ด๋ฒ ์ด์Šค์™€ ์•ˆ๋“œ

lakue.tistory.com

 

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