Skip to content

Commit

Permalink
Merge pull request #169 from boostcampwm-2024/feature/#163-google-login
Browse files Browse the repository at this point in the history
[feature] Google 로그인 기능 구현
  • Loading branch information
yuni-ju authored Feb 1, 2025
2 parents c450522 + ccc3d4c commit 3476794
Show file tree
Hide file tree
Showing 39 changed files with 714 additions and 261 deletions.
11 changes: 11 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ android {
}

addManifestPlaceholders(mapOf("NAVERMAP_CLIENT_ID" to properties.getProperty("NAVERMAP_CLIENT_ID")))

buildConfigField(
"String",
"GOOGLE_CLIENT_ID",
"\"${properties.getProperty("GOOGLE_CLIENT_ID")}\""
)
}

signingConfigs {
Expand Down Expand Up @@ -150,4 +156,9 @@ dependencies {
// Paging
implementation(libs.androidx.paging.runtime)
implementation(libs.androidx.paging.compose.android)

// Credentials
implementation(libs.androidx.credentials)
implementation(libs.androidx.credentials.play.services.auth)
implementation(libs.googleid)
}
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 app/src/main/java/com/squirtles/musicroad/account/GoogleId.kt
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 app/src/main/java/com/squirtles/musicroad/common/SignInAlertDialog.kt
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({})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,29 @@ class CreatePickViewModel @Inject constructor(
val musicVideo = fetchMusicVideoUseCase(song)

/* 등록 결과 - pick ID 담긴 Result */
val user = getCurrentUserUseCase()
val createResult = createPickUseCase(
Pick(
id = "",
song = song,
comment = _comment.value,
createdAt = "",
createdBy = Creator(
userId = user.userId,
userName = user.userName
),
location = LocationPoint(lastLocation!!.latitude, lastLocation!!.longitude),
musicVideoUrl = musicVideo?.previewUrl ?: "",
musicVideoThumbnailUrl = musicVideo?.thumbnailUrl ?: ""
getCurrentUserUseCase()?.let { user ->
val createResult = createPickUseCase(
Pick(
id = "",
song = song,
comment = _comment.value,
createdAt = "",
createdBy = Creator(
userId = user.userId,
userName = user.userName
),
location = LocationPoint(lastLocation!!.latitude, lastLocation!!.longitude),
musicVideoUrl = musicVideo?.previewUrl ?: "",
musicVideoThumbnailUrl = musicVideo?.thumbnailUrl ?: ""
)
)
)

createResult.onSuccess { pickId ->
_createPickUiState.emit(CreateUiState.Success(pickId))
}.onFailure {
/* TODO: Firestore 등록 실패처리 */
_createPickUiState.emit(CreateUiState.Error)
Log.d("CreatePickViewModel", createResult.exceptionOrNull()?.message.toString())
createResult.onSuccess { pickId ->
_createPickUiState.emit(CreateUiState.Success(pickId))
}.onFailure {
/* TODO: Firestore 등록 실패처리 */
_createPickUiState.emit(CreateUiState.Error)
Log.d("CreatePickViewModel", createResult.exceptionOrNull()?.message.toString())
}
}
}
}
Expand Down
Loading

0 comments on commit 3476794

Please sign in to comment.