본문 바로가기
App Programming/Kotlin

[Kotlin] 구글 자동 로그인

by goatlab 2023. 1. 15.
728x90
반응형
SMALL

구글 자동 로그인

 

https://ko.wikipedia.org/wiki/%ED%8C%8C%EC%9D%B4%EC%96%B4%EB%B2%A0%EC%9D%B4%EC%8A%A4

 

클라이언트 (파이어베이스)에 저장되어있는 해당 유저의 idToken과 현재 사용자 정보를 비교하여 참이면 바로 로그인을 실행한다.

 

import android.content.Intent
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider

class intro : AppCompatActivity() {
    lateinit var googleSignInClient: GoogleSignInClient
    lateinit var auth : FirebaseAuth
    lateinit var imageview : ImageView
    val binding by lazy {ActivityIntroBinding.inflate(layoutInflater)}
    
    var googleLoginReult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
        result ->

        var data = result.data
        var task = GoogleSignIn.getSignedInAccountFromIntent(data)
        val account = task.getResult(ApiException::class.java)
        firebaseAuthWithGoogle(account.idToken)
    }

    fun firebaseAuthWithGoogle(idToken: String?){
        var credential = GoogleAuthProvider.getCredential(idToken, null)

        auth.signInWithCredential((credential)).addOnCompleteListener{
            task ->
            if(task.isSuccessful){
                moveMainPage(task.result?.user)
            }
        }
    }
    
    override fun onStart() {
        super.onStart()

        val user = auth.currentUser

        if (user != null) {
            user.getIdToken(true).addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val idToken = task.result.token
                    val homeMove_intent = Intent(applicationContext, MainActivity::class.java)
                    startActivity(homeMove_intent)
                }
            }
        }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        auth = FirebaseAuth.getInstance()

        binding.btnSignIn.setOnClickListener{
            signIn()
        }

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .requestProfile()
            .build()
            
        googleSignInClient = GoogleSignIn.getClient(this, gso)
        
    }
    
    private fun signIn() {
        val i = googleSignInClient.signInIntent
        googleLoginReult.launch(i)
    }

    //로그인 activity에서 Main으로
    fun moveMainPage(user: FirebaseUser?){
        if (user != null){
            startActivity(Intent(this,MainActivity::class.java))
        }
    }
}
728x90
반응형
LIST

'App Programming > Kotlin' 카테고리의 다른 글

[Kotlin] Material CalendarView  (0) 2023.01.19
[Kotlin] timePicker  (0) 2023.01.17
[Kotlin] 파이어베이스 구글 로그인  (0) 2023.01.15
[Kotlin] 파이어베이스 (Firebase)  (0) 2023.01.15
[Kotlin] ImageView  (0) 2023.01.12