Skip to content

Commit

Permalink
[#71] googleLoginLauncher 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
youngseo3 committed May 2, 2024
1 parent 4b75b76 commit 0243f47
Showing 1 changed file with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
package com.snowball.memetory.presentation.ui.auth

import android.app.Activity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.navigation.fragment.findNavController
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.common.api.ApiException
import com.snowball.memetory.R
import com.snowball.memetory.databinding.FragmentSignInBinding
import com.snowball.memetory.util.SocialLoginUtil

class SignInFragment : Fragment() {

private var _binding: FragmentSignInBinding? = null
private val binding get() = _binding!!


private lateinit var socialLoginUtil: SocialLoginUtil
private var googleLoginLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
Log.d("SignInFragment", "$result")
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)

try {
val account = task.getResult(ApiException::class.java)
// 로그인 성공, 서버로 로그인 정보 전송
Toast.makeText(context, "${account.idToken}", Toast.LENGTH_LONG).show()
Log.d("SocialLoginUtil","email =${account.email}, id=${account.id}\n token =${account.idToken}" +
"\nserverAuthCode=${account.serverAuthCode}")
// sendLoginDataToServer(account.idToken)
// googleSignOut()
} catch (e: ApiException) {
// 로그인 실패 콜백 호출
Toast.makeText(context, "${e.message}", Toast.LENGTH_LONG).show()
Log.e("SocialLoginUtil", "google login fail = ${e.message}")
}
//
// if (result.resultCode == Activity.RESULT_OK) {
// val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
// Log.d("SignInFragment", "${result.data} // $task")
// socialLoginUtil.handleGoogleSignInResult(task)
//
// } else if (result.resultCode == Activity.RESULT_CANCELED) {
//
// val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
// Log.d("SignInFragment", "GoogleSignIn.getSignedInAccountFromIntent(result.data): $task")
// result.data?.extras?.let { bundle ->
// for (key in bundle.keySet()) {
// Log.d("SignInFragment", "Extra [$key]: ${bundle.get(key)}")
// }
// } ?: Log.d("SignInFragment", "Login canceled with no extras")
// }
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -26,8 +69,22 @@ class SignInFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// SocialLoginUtil 초기화
socialLoginUtil = SocialLoginUtil(requireContext(), object : SocialLoginUtil.LoginCallback {
override fun onLoginFailure(error: Throwable) {
// 로그인 실패 처리
// TODO: 사용자에게 실패를 알리는 UI 작업
}
})

binding.googleLoginBtn.setOnClickListener {
findNavController().navigate(R.id.signUpFragment) // Navigate to another fragment
// SocialLoginUtil을 통해 로그인을 시도합니다.
socialLoginUtil.googleSignOut()
val signInIntent = socialLoginUtil.getGoogleSignInIntent()
googleLoginLauncher.launch(signInIntent)

// socialLoginUtil.loginGoogle(requireActivity())
// findNavController().navigate(R.id.signUpFragment) // Navigate to another fragment
}
}

Expand Down

0 comments on commit 0243f47

Please sign in to comment.