-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Google OneTabSiginin 작업
- Loading branch information
Showing
5 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 99 additions & 2 deletions
101
app/src/main/java/com/moyerun/moyeorun_android/login/LoginActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,114 @@ | ||
package com.moyerun.moyeorun_android.login | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.content.Intent | ||
import android.content.IntentSender | ||
import android.os.Bundle | ||
import android.provider.Settings | ||
import androidx.activity.result.IntentSenderRequest | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.google.android.gms.auth.api.identity.BeginSignInRequest | ||
import com.google.android.gms.auth.api.identity.Identity | ||
import com.google.android.gms.auth.api.identity.SignInClient | ||
import com.google.android.gms.common.api.ApiException | ||
import com.google.android.gms.common.api.CommonStatusCodes | ||
import com.moyerun.moyeorun_android.BuildConfig | ||
import com.moyerun.moyeorun_android.R | ||
import com.moyerun.moyeorun_android.common.Lg | ||
import com.moyerun.moyeorun_android.common.extension.showNetworkErrorToast | ||
import com.moyerun.moyeorun_android.common.extension.toast | ||
import com.moyerun.moyeorun_android.databinding.ActivityLoginBinding | ||
|
||
|
||
class LoginActivity : AppCompatActivity() { | ||
|
||
private val oneTapClient: SignInClient by lazy { Identity.getSignInClient(this) } | ||
private val signInRequest: BeginSignInRequest by lazy { getBeginSignInRequest() } | ||
|
||
private val beginSignInResultLauncher = | ||
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> | ||
if (result != null) { | ||
try { | ||
val credential = oneTapClient.getSignInCredentialFromIntent(result.data) | ||
val idToken = credential.googleIdToken | ||
if (idToken != null) { | ||
//Todo: 서버에 보내서 인증 @winter223 | ||
// Todo: Firebase crashlytics userId 세팅 | ||
Lg.d("Success. token : $idToken") | ||
} else { | ||
showUnknownErrorToast() | ||
//Todo: #31 을 rebase 하고 주석 풀기 | ||
// Lg.fe("No ID token") | ||
} | ||
} catch (e: ApiException) { | ||
when (e.statusCode) { | ||
CommonStatusCodes.CANCELED -> { /*Doing nothing*/ } | ||
CommonStatusCodes.NETWORK_ERROR -> { | ||
showNetworkErrorToast() | ||
Lg.e("One-tap encountered a network error. $e") | ||
} | ||
else -> { | ||
showUnknownErrorToast() | ||
//Todo: #31 을 rebase 하고 주석 풀기 | ||
// Lg.fe("Couldn't get credential from result.", e) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val binding = ActivityLoginBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
binding.buttonLoginGoogle.setOnClickListener { | ||
//Todo: 구글 로그인 작업 @winter223 | ||
oneTapClient.beginSignIn(signInRequest) | ||
.addOnSuccessListener(this) { result -> | ||
try { | ||
val intentSenderRequest = | ||
IntentSenderRequest.Builder(result.pendingIntent.intentSender).build() | ||
beginSignInResultLauncher.launch(intentSenderRequest) | ||
} catch (e: IntentSender.SendIntentException) { | ||
showUnknownErrorToast() | ||
// Lg.fe("Couldn't start One Tab UI", e) | ||
} | ||
} | ||
.addOnFailureListener(this) { | ||
// 기기에 등록된 계정이 없는 경우 호출 | ||
startDeviceGoogleSignInActivity() | ||
//Todo: #31 을 rebase 하고 주석 풀기 | ||
// 간혹 등록된 계정이 있는데도 해당 콜백을 타는 경우가 있어서 로깅 | ||
// Lg.fe("No Google Accounts found", it) | ||
} | ||
} | ||
} | ||
|
||
private fun startDeviceGoogleSignInActivity() { | ||
startActivity(Intent(Settings.ACTION_ADD_ACCOUNT).apply { | ||
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION) | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK | ||
putExtra(Settings.EXTRA_ACCOUNT_TYPES, arrayOf("com.google")) | ||
}) | ||
} | ||
|
||
private fun getBeginSignInRequest(): BeginSignInRequest { | ||
return BeginSignInRequest.builder() | ||
.setGoogleIdTokenRequestOptions( | ||
BeginSignInRequest.GoogleIdTokenRequestOptions.builder() | ||
.setSupported(true) | ||
.setServerClientId(BuildConfig.WEB_CLIENT_ID) | ||
// false 로 설정해서 앱에 로그인한 적이 있는 계정뿐만 아니라 | ||
// 기기에 등록된 구글 계정을 모두 보여준다 | ||
.setFilterByAuthorizedAccounts(false) | ||
.build() | ||
) | ||
// 하나의 계정만 있다면 자동으로 선택 | ||
.setAutoSelectEnabled(true) | ||
.build() | ||
} | ||
|
||
private fun showUnknownErrorToast() { | ||
toast(getString(R.string.login_toast_unknown_error)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters