Skip to content

Commit

Permalink
feat: Googlr Login 구현 - 모듈화 필요, 서버 연결 필요 / #ARCH-22
Browse files Browse the repository at this point in the history
  • Loading branch information
comst19 committed Jan 11, 2024
1 parent c0fa937 commit fafeb9a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
6 changes: 5 additions & 1 deletion frontend/ARchive/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

buildConfigField "String", "KAKAO_NATIVE_APP_KEY", properties['kakao_native_app_key']
buildConfigField "String", "GOOGLE_LOGIN_CLIENT_ID", properties['google_login_client_id']

manifestPlaceholders["KAKAO_NATIVE_APP_KEY"] =
properties.getProperty("kakao_native_app_key_no_quotes")
Expand Down Expand Up @@ -116,8 +117,11 @@ dependencies {
implementation 'com.google.android.gms:play-services-auth:20.7.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:18.0.1'

// KaKao
// KaKao Login
implementation "com.kakao.sdk:v2-user:2.12.1"

// Goole Login
implementation 'com.google.android.gms:play-services-auth:20.7.0'
}
kapt {
correctErrorTypes true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ package com.droidblossom.archive.presentation.ui.auth
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.droidblossom.archive.BuildConfig
import com.droidblossom.archive.R
import com.droidblossom.archive.databinding.FragmentSignInBinding
import com.droidblossom.archive.presentation.base.BaseFragment
import com.droidblossom.archive.presentation.ui.MainActivity
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.kakao.sdk.auth.model.OAuthToken
import com.kakao.sdk.common.model.ClientError
import com.kakao.sdk.common.model.ClientErrorCause
Expand All @@ -34,21 +43,41 @@ class SignInFragment : BaseFragment<AuthViewModelImpl,FragmentSignInBinding>(R.l
if (error != null) {
Log.e("카카오", "로그인 실패 $error")
} else if (token != null) {
Log.e("카카오", "로그인 성공 ${token.accessToken}")
Log.e("카카오2", "로그인 성공 ${token.accessToken}")
}
}

private var googleLoginLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == -1) {
val data = result.data
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
getGoogleInfo(task)
}
}

private val googleSignInClient: GoogleSignInClient by lazy {
GoogleSignIn.getClient(requireContext(), googleSignInOptions)
}

private val googleSignInOptions: GoogleSignInOptions by lazy {
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

navController = Navigation.findNavController(view)

binding.kakaoLoginBtn.setOnClickListener {
kakaoLogin()
//viewModel.signInToSignUp()
kakaoSignIn()
}

binding.googleLoginBtn.setOnClickListener {
viewModel.signInToSignUp()
//viewModel.signInToSignUp()
googleSignIn()
}
}

Expand Down Expand Up @@ -78,10 +107,8 @@ class SignInFragment : BaseFragment<AuthViewModelImpl,FragmentSignInBinding>(R.l
}
}

private fun kakaoLogin(){
// 카카오톡 설치 확인
private fun kakaoSignIn(){
if (UserApiClient.instance.isKakaoTalkLoginAvailable(requireContext())) {
// 카카오톡 로그인
UserApiClient.instance.loginWithKakaoTalk(requireContext()) { token, error ->
// 로그인 실패 부분
if (error != null) {
Expand All @@ -99,11 +126,30 @@ class SignInFragment : BaseFragment<AuthViewModelImpl,FragmentSignInBinding>(R.l
// 로그인 성공 부분
else if (token != null) {
viewModel.SignInSuccess()
Log.e("카카오", "로그인 성공 ${token.accessToken}")
Log.e("카카오1", "로그인 성공 ${token.accessToken}")
}
}
} else {
UserApiClient.instance.loginWithKakaoAccount(requireContext(), callback = mCallback) // 카카오 이메일 로그인
}
}

private fun googleSignIn() {
val signInIntent = googleSignInClient.signInIntent
googleLoginLauncher.launch(signInIntent)
}
private fun getGoogleInfo(completedTask: Task<GoogleSignInAccount>) {
try {
val TAG = "구글 로그인 결과"
val account = completedTask.getResult(ApiException::class.java)
Log.d(TAG, account.id!!)
Log.d(TAG, account.familyName!!)
Log.d(TAG, account.givenName!!)
Log.d(TAG, account.email!!)
}
catch (e: ApiException) {
Log.w("구글", "signInResult:failed code=" + e.statusCode)
}
}

}

0 comments on commit fafeb9a

Please sign in to comment.