brew-vino-voyage / wine-route

와인의 다양성과 풍미를 탐험하는 여정을 담은 플랫폼, 와인루트(Wine Route)
Apache License 2.0
0 stars 0 forks source link

feat: 구글 로그인 구현하기 #1

Closed vmkmym closed 5 months ago

vmkmym commented 5 months ago

Google로 로그인으로 사용자 인증

구현 순서

  1. Google API 콘솔 프로젝트 설정
  2. 모듈의 build.gradle 파일에서 최신 버전의 Credential Manager를 사용하여 종속성을 선언한다
  3. Google 로그인 요청 인스턴스화한다.
    // GetGoogleIdOption은 사용자의 Google ID 토큰을 검색하는 데 사용
    val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
    .setFilterByAuthorizedAccounts(true)
    .setServerClientId(WEB_CLIENT_ID)
    .setAutoSelectEnabled(true)
    .setNonce(<nonce string to use when generating a Google ID token>)
    .build()
  4. 복귀 사용자를 위한 자동 로그인 활성화(권장)
    val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
    .setFilterByAuthorizedAccounts(true)
    .setServerClientId(WEB_CLIENT_ID)
    .setAutoSelectEnabled(true)
    .setNonce(<nonce string to use when generating a Google ID token>)
    .build()
  5. 보안 강화를 위해 nonce 설정
  6. Google로 로그인 흐름 만들기
    
    val request: GetCredentialRequest = Builder()
    .addCredentialOption(googleIdOption)
    .build()

coroutineScope.launch { try { val result = credentialManager.getCredential( request = request, context = activityContext, ) handleSignIn(result) } catch (e: GetCredentialException) { handleFailure(e) } }

fun handleSignIn(result: GetCredentialResponse) { // Handle the successfully returned credential. val credential = result.credential

when (credential) {

// Passkey credential
is PublicKeyCredential -> {
  // Share responseJson such as a GetCredentialResponse on your server to
  // validate and authenticate
  responseJson = credential.authenticationResponseJson
}

// Password credential
is PasswordCredential -> {
  // Send ID and password to your server to validate and authenticate.
  val username = credential.id
  val password = credential.password
}

// GoogleIdToken credential
is CustomCredential -> {
  if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
    try {
      // Use googleIdTokenCredential and extract id to validate and
      // authenticate on your server.
      val googleIdTokenCredential = GoogleIdTokenCredential
        .createFrom(credential.data)
    } catch (e: GoogleIdTokenParsingException) {
      Log.e(TAG, "Received an invalid google id token response", e)
    }
  } else {
    // Catch any unrecognized custom credential type here.
    Log.e(TAG, "Unexpected type of credential")
  }
}

else -> {
  // Catch any unrecognized credential type here.
  Log.e(TAG, "Unexpected type of credential")
}

} }

7. [Google로 로그인 버튼 흐름 트리거](https://developer.android.com/identity/sign-in/credential-manager-siwg#trigger-siwg)
```Kotlin
val signInWithGoogleOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption.Builder()
  .setServerClientId(WEB_CLIENT_ID)
  .setNonce(<nonce string to use when generating a Google ID token>)
  .build()
fun handleSignIn(result: GetCredentialResponse) {
  // Handle the successfully returned credential.
  val credential = result.credential

  when (credential) {
    is CustomCredential -> {
      if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
        try {
          // Use googleIdTokenCredential and extract id to validate and
          // authenticate on your server.
          val googleIdTokenCredential = GoogleIdTokenCredential
            .createFrom(credential.data)
        } catch (e: GoogleIdTokenParsingException) {
          Log.e(TAG, "Received an invalid google id token response", e)
        }
      }
      else -> {
        // Catch any unrecognized credential type here.
        Log.e(TAG, "Unexpected type of credential")
      }
    }

    else -> {
      // Catch any unrecognized credential type here.
      Log.e(TAG, "Unexpected type of credential")
    }
  }
}
  1. 신규 사용자 가입 활성화(권장)
  2. 로그아웃 처리