From 4b75b76214efa0ee1c08cf7e7ae57597a9e73421 Mon Sep 17 00:00:00 2001 From: youngseo3 Date: Thu, 2 May 2024 13:57:41 +0900 Subject: [PATCH] =?UTF-8?q?[#71]=20SocialLoginUtil=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 구글로그인을 위한 util코드 작성 --- .../snowball/memetory/util/SocialLoginUtil.kt | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Android/app/src/main/java/com/snowball/memetory/util/SocialLoginUtil.kt diff --git a/Android/app/src/main/java/com/snowball/memetory/util/SocialLoginUtil.kt b/Android/app/src/main/java/com/snowball/memetory/util/SocialLoginUtil.kt new file mode 100644 index 00000000..b67e1f81 --- /dev/null +++ b/Android/app/src/main/java/com/snowball/memetory/util/SocialLoginUtil.kt @@ -0,0 +1,130 @@ +package com.snowball.memetory.util + +import android.app.Activity +import android.content.Context +import android.content.Intent +import android.util.Log +import android.widget.Toast +import com.google.android.gms.auth.api.signin.GoogleSignIn +import com.google.android.gms.auth.api.signin.GoogleSignInAccount +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.android.gms.common.api.Scope +import com.google.android.gms.tasks.Task +import com.snowball.memetory.BuildConfig + +class SocialLoginUtil (private val context: Context, private val callback: LoginCallback) { + + private var clientId = BuildConfig.GOOGLE_LOGIN_CLIENT_ID + + private val googleSignInClient: GoogleSignInClient by lazy { + val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) + .requestServerAuthCode(clientId) + .requestIdToken(clientId) + .requestEmail() + .build() + GoogleSignIn.getClient(context, gso) + } + + + // 아직. +// private var googleLoginClient: GoogleSignInClient? = null + + private val REQ_GOOGLE_LOGIN = 1001 + interface LoginCallback { + // fun onLoginSuccess(memberStatusCheckData : CheckStatus, signUpData : SignUp) + fun onLoginFailure(error: Throwable) + } + + + fun loginGoogle(activity: Activity) { + +// var signInOptions = +// GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) +// .requestServerAuthCode(clientId) +// .requestEmail().build() +// +// googleLoginClient = GoogleSignIn.getClient(activity, signInOptions) + + googleSignInClient?.let { client -> + client.signOut().addOnCompleteListener { + // 로그아웃 후 진행 + activity.startActivityForResult( + client.signInIntent, + REQ_GOOGLE_LOGIN + ) + } + } + } + + fun getGoogleSignInIntent(): Intent { + // Google 로그인 인텐트 반환 + Toast.makeText(context, "$googleSignInClient", Toast.LENGTH_LONG).show() + Log.d("SocialLoginUtil", "getGoogleSignInIntent: $googleSignInClient // ${googleSignInClient.signInIntent} // $clientId") + return googleSignInClient.signInIntent + } + fun googleSignOut() { + Toast.makeText(context, "로그아웃", Toast.LENGTH_LONG).show() + googleSignInClient.signOut().addOnCompleteListener { task -> + if (task.isSuccessful) { + // 로그아웃이 성공적으로 완료되었을 때의 로직 + Log.d("SocialLoginUtil", "Sign out successful") + } else { + // 로그아웃이 실패했을 때의 로직 + Log.e("SocialLoginUtil", "Sign out failed", task.exception) + } + } + } + + fun handleGoogleSignInResult(task: Task) { + 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}, token =${account.idToken}") +// sendLoginDataToServer(account.idToken) +// googleSignOut() + } catch (e: ApiException) { + // 로그인 실패 콜백 호출 + Toast.makeText(context, "${e.message}", Toast.LENGTH_LONG).show() + Log.e("SocialLoginUtil", "google login fail = ${e.message}") + callback.onLoginFailure(e) + } + } + + private fun sendLoginDataToServer(idToken: String?) { + // TODO: 서버로 idToken과 로그인 타입 전송 로직 구현 + + } + + fun handleGoogleActivityResult( + requestCode: Int, + resultCode: Int, + data: Intent?, + activity: Activity){ + + if(requestCode == REQ_GOOGLE_LOGIN){ + // 유저 로그인 + val task = GoogleSignIn.getSignedInAccountFromIntent(data) + try { + val account = task.getResult(ApiException::class.java) + if (account != null && account.id != null && account.email != null) { + + Log.d("SocialLoginUtil","email =${account.email}, id=${account.id}, token =${account.idToken}") +// account.email, +// account.id, +// account.displayName, +// account.photoUrl?.path ?: "" + + } + + } catch (e: ApiException) { + Log.d("SocialLoginUtil", "google login fail = ${e.message}") + if (e.statusCode == 12501) { + // 사용자 취소 + } + } + } + } +}