-
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.
Merge pull request #64 from Nexters/refactor/login
LoginViewModel - Context 참조 제거
- Loading branch information
Showing
3 changed files
with
96 additions
and
52 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
feature/login/src/main/java/com/goalpanzi/mission_mate/feature/login/LoginManager.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.goalpanzi.mission_mate.feature.login | ||
|
||
import android.content.Context | ||
import androidx.credentials.CredentialManager | ||
import androidx.credentials.CustomCredential | ||
import androidx.credentials.GetCredentialRequest | ||
import androidx.credentials.GetCredentialResponse | ||
import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
sealed class LoginData { | ||
data class Success(val email : String) : LoginData() | ||
data class Failed(val exception: Exception) : LoginData() | ||
} | ||
|
||
class LoginManager ( | ||
private val context : Context | ||
) { | ||
suspend fun request() : LoginData = withContext(Dispatchers.IO) { | ||
val credentialManager = CredentialManager.create(context) | ||
val signInWithGoogleOption: GetSignInWithGoogleOption = | ||
GetSignInWithGoogleOption.Builder(BuildConfig.CREDENTIAL_WEB_CLIENT_ID) | ||
.build() | ||
|
||
val request: GetCredentialRequest = GetCredentialRequest.Builder() | ||
.addCredentialOption(signInWithGoogleOption) | ||
.build() | ||
|
||
return@withContext try { | ||
val result = credentialManager.getCredential(context, request) | ||
handleSignIn(result) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
LoginData.Failed(e) | ||
} | ||
} | ||
|
||
private fun handleSignIn(response: GetCredentialResponse) : LoginData { | ||
return when (val credential = response.credential) { | ||
is CustomCredential -> { | ||
if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { | ||
try { | ||
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data) | ||
LoginData.Success(googleIdTokenCredential.id) | ||
} catch (e: GoogleIdTokenParsingException) { | ||
LoginData.Failed(e) | ||
} | ||
}else { | ||
LoginData.Failed(RuntimeException("NoDataException")) | ||
} | ||
} | ||
|
||
else -> { | ||
LoginData.Failed(IllegalStateException()) | ||
} | ||
} | ||
} | ||
} |
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