๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์ต๋ช… ๋กœ๊ทธ์ธ

hyihyiยท2024๋…„ 2์›” 29์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
56/70
post-thumbnail

1. ํŒŒ์ด์–ด๋ฒ ์ด์Šค์— ์•ฑ ๋“ฑ๋ก

๋‹ค์šด๋ฐ›์€ google-services.json ํŒŒ์ผ์„ ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ถ”๊ฐ€

2. ์ข…์†์„ฑ ์ถ”๊ฐ€(ํ”„๋กœ์ ํŠธ ์ˆ˜์ค€)

build.gradle.kts(ํ”„๋กœ์ ํŠธ ์ˆ˜์ค€) ํŒŒ์ผ์— ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ข…์† ํ•ญ๋ชฉ์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

id("com.google.gms.google-services") version "4.4.1" apply false

3. ์ข…์†์„ฑ ์ถ”๊ฐ€(์•ฑ ์ˆ˜์ค€)

build.gradle.kts(์•ฑ ์ˆ˜์ค€) ํŒŒ์ผ์—์„œ google-services ํ”Œ๋Ÿฌ๊ทธ์ธ๊ณผ ์•ฑ์—์„œ ์‚ฌ์šฉํ•  Firebase SDK๋ฅผ ๋ชจ๋‘ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

id("com.google.gms.google-services")
 // ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ ๋ฒ„์ „ ์ž๋™ ๊ด€๋ฆฌ
implementation(platform("com.google.firebase:firebase-bom:32.7.3"))

// ์• ๋„๋ฆฌํ‹ฑ์Šค ๋ชจ๋“ˆ๋กœ ์•ฑ์˜ ์‚ฌ์šฉ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘, ๋ถ„์„
implementation("com.google.firebase:firebase-analytics") 

์ต๋ช…๋กœ๊ทธ์ธ/๊ตฌ๊ธ€๋กœ๊ทธ์ธ ์ข…์†์„ฑ ์ถ”๊ฐ€
ktx๋Š” ์ฝ”ํ‹€๋ฆฐ ํ™•์žฅ์„ ์˜๋ฏธ

// ํŒŒ์ด์–ด๋ฒ ์ด์Šค์˜ ๋‹ค์–‘ํ•œ ์ธ์ฆ ๋ฐฉ๋ฒ•(๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ, ์ด๋ฉ”์ผ/ํŒจ์Šค์›Œ๋“œ, ์ต๋ช… ๋กœ๊ทธ์ธ ๋“ฑ)์„ ์‚ฌ์šฉ
implementation("com.google.firebase:firebase-auth-ktx") 

// ๊ตฌ๊ธ€ ์ธ์ฆ ์„œ๋น„์Šค ์‚ฌ์šฉ
implementation("com.google.android.gms:play-services-auth:20.7.0") 


4. ํŒŒ์ด์–ด๋ฒ ์ด์Šค์—์„œ ์ต๋ช… ๋กœ๊ทธ์ธ ์„ค์ •



5. ์ฝ”๋“œ

1) FirebaseAuth ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ

class StartFragment : Fragment() {
    private lateinit var auth: FirebaseAuth
    ...

2) onCreate ๋ฉ”์„œ๋“œ์—์„œ FirebaseAuth ๊ฐ์ฒด์˜ ๊ณต์œ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”

override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   auth = Firebase.auth
}

์ด ๊ณผ์ •์ด ์—†์œผ๋ฉด ์•„๋ž˜์˜ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธด๋‹ค.

3) ํŠน์ • ๋™์ž‘์ด ์žˆ์„ ๋•Œ ๋กœ๊ทธ์ธ ์‹คํ–‰

private fun setUpClickListener() {
        binding.btnStartTrip.setOnClickListener{
            performAnonymousLogin()
        }
    }

    private fun performAnonymousLogin() {
        auth.signInAnonymously()
            .addOnCompleteListener(activity as Activity) { task ->
                if (task.isSuccessful) {
                    //val user = auth.currentUser
                    showSnackBar(R.string.login_success)
                    findNavController().navigate(R.id.action_navigation_start_to_navigation_random_option)
                } else {
                    showSnackBar(R.string.login_fail)
                }
            }
    }

์ง€๊ธˆ์€ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด์— ์ ‘๊ทผํ•  ํ•„์š”๊ฐ€ ์—†๊ณ  ๋กœ๊ทธ์ธ์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋งŒ ์ค‘์š”ํ•˜๋ฏ€๋กœ
val user = auth.currentUser๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
ํ•˜์ง€๋งŒ ๋กœ๊ทธ์ธ ํ›„ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ฑฐ๋‚˜, ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ถ”๊ฐ€์ ์ธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž ์ •๋ณด์— ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€