Skip to content

Commit

Permalink
[feat]#107 googleLogin 성공
Browse files Browse the repository at this point in the history
logout은 아직
  • Loading branch information
cbj0010 committed Feb 8, 2023
1 parent dd07af0 commit 4b68501
Showing 1 changed file with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package kr.co.nottodo.presentation.login.view

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
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
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.kakao.sdk.auth.AuthApiClient
import com.kakao.sdk.auth.model.OAuthToken
import com.kakao.sdk.common.model.ClientError
import com.kakao.sdk.common.model.ClientErrorCause
import com.kakao.sdk.user.UserApiClient
import kr.co.nottodo.Application
import kr.co.nottodo.BuildConfig
import kr.co.nottodo.databinding.ActivityLoginBinding
import kr.co.nottodo.presentation.login.viewmodel.LoginViewModel
import timber.log.Timber

class LoginActivity : AppCompatActivity() {
lateinit var binding: ActivityLoginBinding
private val viewModel by viewModels<LoginViewModel>()

private lateinit var auth: FirebaseAuth
private lateinit var mGoogleSignInClient: GoogleSignInClient
private lateinit var startGoogleLoginForResult: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -26,6 +43,56 @@ class LoginActivity : AppCompatActivity() {
observeIsLoggedIn()
setKakaoLogin()
setKakaoLogout()
// googleInit()
auth = Firebase.auth
// binding.btnGoogleLogin.setOnClickListener {
// val signInIntent = mGoogleSignInClient.signInIntent
// startGoogleLoginForResult.launch(signInIntent)
// }
val requestLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
//구글 로그인 결과 처리...........................
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
try {
val account = task.getResult(ApiException::class.java)!!
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
Application.auth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
Application.email = account.email
Log.d("Login1", "signInWithCredential:success")
val user = auth.currentUser
updateUI(user)
// changeVisibility("login")
} else {
Log.w("Login2", "signInWithCredential:failure", task.exception)
// changeVisibility("logout")
}
}
} catch (e: ApiException) {
// changeVisibility("logout")
Log.e("Login3", "login E$e")
}
}
binding.btnGoogleLogin.setOnClickListener {
//구글 로그인....................
val gso = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(GOOGLE_CLIENT_ID)
.requestEmail()
.build()
val signInIntent = GoogleSignIn.getClient(this, gso).signInIntent
requestLauncher.launch(signInIntent)
}

binding.btnGoogleLogout.setOnClickListener {
//로그아웃...........
Application.auth.signOut()
Application.email = null
binding.tvGoogleNickname.text = ""
// changeVisibility("logout")
}
}

private fun observeIsLoggedIn() {
Expand Down Expand Up @@ -128,4 +195,67 @@ class LoginActivity : AppCompatActivity() {
}
}
}

private fun googleInit() {

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(GOOGLE_CLIENT_ID)
.requestEmail()
.requestProfile()
.requestId()
.build()

mGoogleSignInClient = GoogleSignIn.getClient(this, gso)

// startGoogleLoginForResult =
// registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
// if (result.resultCode == RESULT_OK) {
// result.data?.let { data ->
//
// val task = GoogleSignIn.getSignedInAccountFromIntent(data)
//
// try {
// // Google Sign In was successful, authenticate with Firebase
// val account = task.getResult(ApiException::class.java)!!
// Log.d("LoginActivity", "firebaseAuthWithGoogle:" + account.id)
// firebaseAuthWithGoogle(account.idToken!!)
// } catch (e: ApiException) {
// // Google Sign In failed, update UI appropriately
// Log.w("Login", "Google sign in failed", e)
// }
// }
// // Google Login Success
// } else {
// Log.e("Login", "Google Result Error ${result}")
// }
// }
}

// [START auth_with_google]
private fun firebaseAuthWithGoogle(idToken: String) {
val credential = GoogleAuthProvider.getCredential(idToken, null)
auth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d("Login", "signInWithCredential:success")
val user = auth.currentUser
updateUI(user)
} else {
// If sign in fails, display a message to the user.
Log.w("Login", "signInWithCredential:failure", task.exception)
updateUI(null)
}
}
}

private fun updateUI(user: FirebaseUser?) {
// FirebaseUser 데이터에 따른 UI 작업
binding.tvGoogleNickname.text = user?.displayName
}

companion object {
private const val GOOGLE_CLIENT_ID = BuildConfig.GOOGLE_CLIENT_ID
}

}

0 comments on commit 4b68501

Please sign in to comment.