Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions Near/app/src/main/java/com/alarmy/near/data/di/DataSourceModule.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import kotlinx.coroutines.flow.Flow

interface AuthRepository {
// 소셜 로그인 수행
suspend fun performSocialLogin(providerType: ProviderType): Result<Unit>

/**
* 소셜 로그인 수행 (토큰 직접 전달)
*/
suspend fun socialLogin(
suspend fun performSocialLogin(
accessToken: String,
providerType: ProviderType,
): Result<Unit>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alarmy.near.data.repository

import com.alarmy.near.data.datasource.SocialLoginProcessor
import com.alarmy.near.model.ProviderType
import com.alarmy.near.network.auth.TokenManager
import com.alarmy.near.network.request.SocialLoginRequest
Expand All @@ -13,40 +12,15 @@ class AuthRepositoryImpl
@Inject
constructor(
private val authService: AuthService,
private val socialLoginProcessor: SocialLoginProcessor,
private val tokenManager: TokenManager,
) : AuthRepository {
override suspend fun performSocialLogin(providerType: ProviderType): Result<Unit> =
try {
val result = socialLoginProcessor.processLogin(providerType)

if (result.isSuccess) {
val accessToken = result.getOrThrow()
socialLogin(accessToken, providerType)
} else {
Result.failure(
createLoginException(
providerType = providerType,
errorMessage = result.exceptionOrNull()?.message,
defaultMessage = "로그인에 실패했습니다",
),
)
}
} catch (exception: Exception) {
Result.failure(
createLoginException(
providerType = providerType,
errorMessage = exception.message,
defaultMessage = "로그인 중 오류가 발생했습니다",
),
)
}

override suspend fun socialLogin(
override suspend fun performSocialLogin(
accessToken: String,
providerType: ProviderType,
): Result<Unit> =
Comment on lines +17 to 20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The performSocialLogin function now directly receives the access token. Consider adding error handling to this function to manage potential issues with the token itself, such as an invalid or expired token. This will make the function more robust.

try {
validateAccessToken(accessToken)

val request =
SocialLoginRequest(
accessToken = accessToken,
Expand All @@ -72,6 +46,15 @@ class AuthRepositoryImpl
Result.failure(Exception(errorMessage))
}

private fun validateAccessToken(accessToken: String) {
require(accessToken.isNotBlank()) {
"액세스 토큰이 비어있습니다"
}
require(accessToken.length >= MIN_TOKEN_LENGTH) {
"액세스 토큰 형식이 올바르지 않습니다"
}
}

override suspend fun logout() {
tokenManager.clearAllTokens()
}
Expand All @@ -94,15 +77,6 @@ class AuthRepositoryImpl

override suspend fun refreshToken(): Boolean = tokenManager.refreshToken()

private fun createLoginException(
providerType: ProviderType,
errorMessage: String?,
defaultMessage: String,
): Exception {
val finalMessage = errorMessage ?: "$providerType $defaultMessage"
return Exception(finalMessage)
}

// TODO 추후 에러 메시지 변경
private fun getHttpErrorMessage(httpCode: Int): String =
when (httpCode) {
Expand All @@ -112,4 +86,8 @@ class AuthRepositoryImpl
500 -> "서버에 문제가 발생했습니다"
else -> "로그인 중 오류가 발생했습니다"
}

companion object {
private const val MIN_TOKEN_LENGTH = 10
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ package com.alarmy.near.model

enum class ProviderType {
KAKAO,
ETC,
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
Expand All @@ -31,8 +32,10 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.alarmy.near.R
import com.alarmy.near.model.ProviderType
import com.alarmy.near.presentation.feature.login.auth.SocialLoginHandler
import com.alarmy.near.presentation.feature.login.model.TermType
import com.alarmy.near.presentation.ui.theme.NearTheme
import kotlinx.coroutines.launch

@Composable
internal fun LoginRoute(
Expand All @@ -42,9 +45,13 @@ internal fun LoginRoute(
viewModel: LoginViewModel = hiltViewModel(),
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val showPrivacyBottomSheet by viewModel.showPrivacyBottomSheet.collectAsStateWithLifecycle()
val requiresPrivacyConsent by viewModel.requiresPrivacyConsent.collectAsStateWithLifecycle()
val termsAgreementState by viewModel.termsAgreementState.collectAsStateWithLifecycle()
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current
val scope = rememberCoroutineScope()

val socialLoginHandler = remember { SocialLoginHandler(context) }

// 약관 제목을 미리 가져옴
val termsTitles =
Expand Down Expand Up @@ -83,13 +90,23 @@ internal fun LoginRoute(
LoginScreen(
uiState = uiState,
onLoginClick = { providerType ->
viewModel.performLogin(providerType)
scope.launch {
socialLoginHandler.performSocialLogin(
providerType = providerType,
onSuccess = { accessToken ->
viewModel.onSocialLoginSuccess(accessToken, providerType)
},
onFailure = { exception ->
viewModel.onSocialLoginFailure(exception)
},
)
}
},
)

// 개인정보 동의 바텀시트
PrivacyConsentBottomSheet(
isVisible = showPrivacyBottomSheet,
isVisible = requiresPrivacyConsent,
termsAgreementState = termsAgreementState,
onDismiss = {
viewModel.dismissPrivacyBottomSheet()
Expand Down Expand Up @@ -211,14 +228,3 @@ private object LoginScreenConstants {
const val DESCRIPTION_SPACING = 12
const val BOTTOM_SPACING = 96
}

@Preview(showBackground = true)
@Composable
fun LoginScreenPreview() {
NearTheme {
LoginScreen(
uiState = LoginUiState(),
onLoginClick = { },
)
}
}
Loading