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

[Android, Kotlin] firebase sns์•ฑ ๋งŒ๋“ค๊ธฐ 2(๋กœ๊ทธ์ธ)

by immgga 2022. 6. 23.

์ „์— ๋งŒ๋“ค์—ˆ๋˜ ํšŒ์›๊ฐ€์ž…์„ ์ด์šฉํ•ด ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด์ž.

 

1. ํด๋ฆญ ๋ฆฌ์Šค๋„ˆ ์„ค์ •

binding.loginButton.setOnClickListener(object: View.OnClickListener {
    override fun onClick(p0: View?) {
    	. . .
    }
}

 

2. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

์ด๋ฉ”์ผ ํŒจ์Šค์›Œ๋“œ๊ฐ€ ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์„ ๋•Œ์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ

val email:String = binding.email.text.toString()
val pw:String = binding.password.text.toString()

if (TextUtils.isEmpty(email) || TextUtils.isEmpty(pw)) {
    Toast.makeText(this@MainActivity, "์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ์‚ฌํ•ญ ์ž…๋‹ˆ๋‹ค.", Toast.LENGTH_SHORT).show()
    return
}

 

3. ๋กœ๊ทธ์ธ ๋กœ์ง ์ž‘์„ฑ(signInWithEmailAndPassword)

val auth = FirebaseAuth.getInstance()
auth.signInWithEmailAndPassword(email, pw).addOnCompleteListener(this@MainActivity,
    object : OnCompleteListener<AuthResult> {
    override fun onComplete(p0: Task<AuthResult>) {
        if (p0.isSuccessful) {
            Toast.makeText(this@MainActivity, "๋กœ๊ทธ์ธ ์„ฑ๊ณต", Toast.LENGTH_SHORT).show()
            val intent = Intent(this@MainActivity, MainPageActivity::class.java)
                .putExtra("userId", auth.uid)
            startActivity(intent)
            finish()
        } else {
            Toast.makeText(this@MainActivity, "์ด๋ฉ”์ผ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.", Toast.LENGTH_SHORT).show()
        }
    }
})

addOnCompleteListener๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์„ isSuccessful ์•ˆ์— ์ ์—ˆ๋‹ค.

signInWithEmailAndPassword์—์„œ ์ž์ฒด์ ์œผ๋กœ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ด์ค€๋‹ค.

 

Toast์— ๋กœ๊ทธ์ธ ์„ฑ๊ณต์ด ๋œจ๋ฉด ์„ฑ๊ณต!

728x90