-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feature] Google 로그인 기능 구현
- Loading branch information
Showing
39 changed files
with
714 additions
and
261 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
64 changes: 64 additions & 0 deletions
64
app/src/main/java/com/squirtles/musicroad/account/AccountViewModel.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,64 @@ | ||
package com.squirtles.musicroad.account | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential | ||
import com.squirtles.domain.usecase.user.ClearUserUseCase | ||
import com.squirtles.domain.usecase.user.CreateGoogleIdUserUseCase | ||
import com.squirtles.domain.usecase.user.FetchUserUseCase | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class AccountViewModel @Inject constructor( | ||
private val fetchUserUseCase: FetchUserUseCase, | ||
private val createGoogleIdUserUseCase: CreateGoogleIdUserUseCase, | ||
private val clearUserUseCase: ClearUserUseCase, | ||
) : ViewModel() { | ||
|
||
private val _signInSuccess = MutableSharedFlow<Boolean>() | ||
val signInSuccess = _signInSuccess.asSharedFlow() | ||
|
||
private val _signOutSuccess = MutableSharedFlow<Boolean>() | ||
val signOutSuccess = _signOutSuccess.asSharedFlow() | ||
|
||
fun signIn(credential: GoogleIdTokenCredential) { | ||
viewModelScope.launch { | ||
fetchUserUseCase(credential.id) | ||
.onSuccess { | ||
Log.d("SignIn", "기존 계정 ${it.userId} 로그인") | ||
_signInSuccess.emit(true) | ||
} | ||
.onFailure { | ||
createGoogleIdUser(credential) | ||
} | ||
} | ||
} | ||
|
||
private fun createGoogleIdUser(credential: GoogleIdTokenCredential) { | ||
viewModelScope.launch { | ||
createGoogleIdUserUseCase( | ||
userId = credential.id, | ||
userName = credential.displayName, | ||
userProfileImage = credential.profilePictureUri.toString() | ||
).onSuccess { | ||
Log.d("SignIn", "새로운 계정 ${it.userId} 로그인") | ||
_signInSuccess.emit(true) | ||
}.onFailure { | ||
_signInSuccess.emit(false) | ||
} | ||
} | ||
} | ||
|
||
fun signOut() { | ||
viewModelScope.launch { | ||
clearUserUseCase() | ||
.onSuccess { _signOutSuccess.emit(true) } | ||
.onFailure { _signOutSuccess.emit(false) } | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
app/src/main/java/com/squirtles/musicroad/account/GoogleId.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,68 @@ | ||
package com.squirtles.musicroad.account | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.credentials.ClearCredentialStateRequest | ||
import androidx.credentials.CredentialManager | ||
import androidx.credentials.CustomCredential | ||
import androidx.credentials.GetCredentialRequest | ||
import androidx.credentials.GetCredentialResponse | ||
import androidx.credentials.exceptions.NoCredentialException | ||
import com.google.android.libraries.identity.googleid.GetGoogleIdOption | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential | ||
import com.squirtles.musicroad.BuildConfig | ||
import com.squirtles.musicroad.R | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
class GoogleId(private val context: Context) { | ||
private val credentialManager = CredentialManager.create(context) | ||
|
||
private val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder() | ||
.setFilterByAuthorizedAccounts(false) | ||
.setServerClientId(BuildConfig.GOOGLE_CLIENT_ID) | ||
.setAutoSelectEnabled(true) | ||
.build() | ||
|
||
private val request = GetCredentialRequest.Builder() | ||
.addCredentialOption(googleIdOption) | ||
.build() | ||
|
||
private fun handleSignIn(result: GetCredentialResponse, onSuccess: (GoogleIdTokenCredential) -> Unit) { | ||
when (val data = result.credential) { | ||
is CustomCredential -> { | ||
if (data.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { | ||
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(data.data) | ||
Log.d("GoogleId", "data.type : ${googleIdTokenCredential.id}") | ||
Log.d("GoogleId", "data.type : ${googleIdTokenCredential.displayName}") | ||
Log.d("GoogleId", "data.type : ${googleIdTokenCredential.profilePictureUri.toString()}") | ||
onSuccess(googleIdTokenCredential) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun signIn(onSuccess: (GoogleIdTokenCredential) -> Unit) { | ||
CoroutineScope(Dispatchers.Main).launch { | ||
runCatching { | ||
val result = credentialManager.getCredential(context, request) | ||
handleSignIn(result, onSuccess) | ||
}.onFailure { exception -> | ||
when (exception) { | ||
is NoCredentialException -> Toast.makeText(context, context.getString(R.string.google_id_no_credential_exception_message), Toast.LENGTH_SHORT).show() | ||
} | ||
Log.e("GoogleId", "Google SignIn Error : $exception") | ||
} | ||
} | ||
} | ||
|
||
fun signOut() { | ||
CoroutineScope(Dispatchers.Main).launch { | ||
credentialManager.clearCredentialState( | ||
request = ClearCredentialStateRequest() | ||
) | ||
} | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
app/src/main/java/com/squirtles/musicroad/common/SignInAlertDialog.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,135 @@ | ||
package com.squirtles.musicroad.common | ||
|
||
import android.content.res.Configuration.UI_MODE_NIGHT_YES | ||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.isSystemInDarkTheme | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.BasicAlertDialog | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.ButtonDefaults | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.squirtles.musicroad.R | ||
import com.squirtles.musicroad.ui.theme.Black | ||
import com.squirtles.musicroad.ui.theme.DarkGray | ||
import com.squirtles.musicroad.ui.theme.MusicRoadTheme | ||
import com.squirtles.musicroad.ui.theme.SignInButtonDarkBackground | ||
import com.squirtles.musicroad.ui.theme.SignInButtonDarkStroke | ||
import com.squirtles.musicroad.ui.theme.SignInButtonLightStroke | ||
import com.squirtles.musicroad.ui.theme.White | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
internal fun SignInAlertDialog( | ||
onDismissRequest: () -> Unit, | ||
onGoogleSignInClick: () -> Unit, | ||
description: String | ||
) { | ||
BasicAlertDialog( | ||
onDismissRequest = onDismissRequest, | ||
) { | ||
Surface( | ||
modifier = Modifier.fillMaxWidth(), | ||
shape = RoundedCornerShape(16.dp), | ||
color = MaterialTheme.colorScheme.surface | ||
) { | ||
Column( | ||
modifier = Modifier.padding(24.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center | ||
) { | ||
Text( | ||
text = description, | ||
color = MaterialTheme.colorScheme.onSurface, | ||
fontWeight = FontWeight.Bold, | ||
textAlign = TextAlign.Center, | ||
style = MaterialTheme.typography.bodyLarge | ||
) | ||
|
||
VerticalSpacer(height = 40) | ||
|
||
GoogleSignInButton(onClick = onGoogleSignInClick) | ||
|
||
VerticalSpacer(height = 20) | ||
|
||
Text( | ||
text = stringResource(R.string.sign_in_dialog_dismiss), | ||
modifier = Modifier.clickable(onClick = onDismissRequest), | ||
color = DarkGray, | ||
textAlign = TextAlign.Center, | ||
style = MaterialTheme.typography.bodyMedium | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun GoogleSignInButton( | ||
onClick: () -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
Button( | ||
onClick = onClick, | ||
modifier = modifier.height(40.dp), | ||
shape = RoundedCornerShape(50), | ||
colors = ButtonDefaults.buttonColors(containerColor = if (isSystemInDarkTheme()) SignInButtonDarkBackground else White), | ||
border = BorderStroke(1.dp, if (isSystemInDarkTheme()) SignInButtonDarkStroke else SignInButtonLightStroke), | ||
contentPadding = PaddingValues(vertical = 10.dp, horizontal = 12.dp) | ||
) { | ||
Row(verticalAlignment = Alignment.CenterVertically) { | ||
Image( | ||
painter = painterResource(id = R.drawable.img_google_logo), | ||
contentDescription = stringResource(id = R.string.profile_google_icon), | ||
modifier = Modifier.size(20.dp) | ||
) | ||
HorizontalSpacer(10) | ||
Text( | ||
stringResource( | ||
id = R.string.profile_sign_in_google | ||
), | ||
color = if (isSystemInDarkTheme()) White else Black | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Preview(name = "Light") | ||
@Preview(name = "Dark", uiMode = UI_MODE_NIGHT_YES) | ||
@Composable | ||
private fun LoginAlertDialogPreview() { | ||
MusicRoadTheme { | ||
SignInAlertDialog({}, {}, stringResource(id = R.string.sign_in_dialog_title_default)) | ||
} | ||
} | ||
|
||
|
||
@Preview(name = "Light") | ||
@Preview(name = "Dark", uiMode = UI_MODE_NIGHT_YES) | ||
@Composable | ||
private fun PreviewGoogleSignInButton() { | ||
MusicRoadTheme { | ||
GoogleSignInButton({}) | ||
} | ||
} |
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
Oops, something went wrong.