Skip to content

Commit

Permalink
[feat] #8 로그인 뷰 - 로그인 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
HAJIEUN02 committed Nov 5, 2024
1 parent 4ca3b5e commit 04965b9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import org.sopt.and.data.model.request.RequestUserRegisterDto
import org.sopt.and.data.model.response.ResponseLoginTokenDto
import org.sopt.and.data.model.response.ResponseRegisterNumberDto
import org.sopt.and.presentation.util.BaseResponse
import org.sopt.and.presentation.util.NullableBaseRespone

interface AuthRemoteDataSource {
suspend fun register(requestUserRegisterDto: RequestUserRegisterDto): BaseResponse<ResponseRegisterNumberDto>

suspend fun login(requestUserLoginDto: RequestUserLoginDto): BaseResponse<ResponseLoginTokenDto>
suspend fun login(requestUserLoginDto: RequestUserLoginDto): NullableBaseRespone<ResponseLoginTokenDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.sopt.and.data.model.response.ResponseLoginTokenDto
import org.sopt.and.data.model.response.ResponseRegisterNumberDto
import org.sopt.and.data.service.AuthService
import org.sopt.and.presentation.util.BaseResponse
import org.sopt.and.presentation.util.NullableBaseRespone
import javax.inject.Inject

class AuthRemoteDataSourceImpl @Inject constructor(
Expand All @@ -15,6 +16,6 @@ class AuthRemoteDataSourceImpl @Inject constructor(
override suspend fun register(requestUserRegisterDto: RequestUserRegisterDto): BaseResponse<ResponseRegisterNumberDto> =
authService.postRegister(requestUserRegisterDto)

override suspend fun login(requestUserLoginDto: RequestUserLoginDto): BaseResponse<ResponseLoginTokenDto> =
override suspend fun login(requestUserLoginDto: RequestUserLoginDto): NullableBaseRespone<ResponseLoginTokenDto> =
authService.postLogin(requestUserLoginDto)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.sopt.and.data.repositoryImpl

import org.sopt.and.data.datasource.remote.AuthRemoteDataSource
import org.sopt.and.domain.model.UserEntity
import org.sopt.and.domain.model.UserIdEntity
import org.sopt.and.domain.model.UserLoginEntity
import org.sopt.and.domain.model.UserRegisterEntity
import org.sopt.and.domain.repository.AuthRepository
import javax.inject.Inject
Expand All @@ -13,4 +15,9 @@ class AuthRepositoryImpl @Inject constructor(
runCatching {
authRemoteDataSource.register(requestUserRegisterDto = userRegisterEntity.toRequestUserRegisterDto()).result.toUserIdEntity()
}

override suspend fun postLogin(userLoginEntity: UserLoginEntity): Result<UserEntity> =
runCatching {
authRemoteDataSource.login(requestUserLoginDto = userLoginEntity.toRequestUserLoginDto()).result.toUserEntity()
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/org/sopt/and/data/service/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.sopt.and.data.model.request.RequestUserRegisterDto
import org.sopt.and.data.model.response.ResponseLoginTokenDto
import org.sopt.and.data.model.response.ResponseRegisterNumberDto
import org.sopt.and.presentation.util.BaseResponse
import org.sopt.and.presentation.util.NullableBaseRespone
import retrofit2.http.Body
import retrofit2.http.POST

Expand All @@ -17,7 +18,7 @@ interface AuthService {
@POST("/$LOGIN")
suspend fun postLogin(
@Body body: RequestUserLoginDto
): BaseResponse<ResponseLoginTokenDto>
): NullableBaseRespone<ResponseLoginTokenDto>

companion object {
const val USER = "user"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/org/sopt/and/di/UseCaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.and.domain.repository.AuthRepository
import org.sopt.and.domain.usecase.PostUserLoginUseCase
import org.sopt.and.domain.usecase.PostUserRegisterUseCase
import javax.inject.Singleton

Expand All @@ -15,4 +16,9 @@ class UseCaseModule {
@Singleton
fun providesUserRegisterUseCase(authRepository: AuthRepository): PostUserRegisterUseCase =
PostUserRegisterUseCase(authRepository = authRepository)

@Provides
@Singleton
fun providesUserLoginUseCase(authRepository: AuthRepository): PostUserLoginUseCase =
PostUserLoginUseCase(authRepository = authRepository)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.sopt.and.domain.repository

import org.sopt.and.domain.model.UserEntity
import org.sopt.and.domain.model.UserIdEntity
import org.sopt.and.domain.model.UserLoginEntity
import org.sopt.and.domain.model.UserRegisterEntity

interface AuthRepository {
suspend fun postRegister(userRegisterEntity: UserRegisterEntity): Result<UserIdEntity>
suspend fun postLogin(userLoginEntity: UserLoginEntity): Result<UserEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.sopt.and.domain.usecase

import org.sopt.and.domain.model.UserEntity
import org.sopt.and.domain.model.UserLoginEntity
import org.sopt.and.domain.repository.AuthRepository

class PostUserLoginUseCase(
private val authRepository: AuthRepository
) {
suspend operator fun invoke(userLoginEntity: UserLoginEntity): Result<UserEntity> =
authRepository.postLogin(userLoginEntity = userLoginEntity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package org.sopt.and.presentation.ui.auth.login

import dagger.hilt.android.lifecycle.HiltViewModel
import org.sopt.and.data.datasource.local.WaveLocalDataSource
import org.sopt.and.domain.model.UserLoginEntity
import org.sopt.and.domain.usecase.PostUserLoginUseCase
import org.sopt.and.presentation.util.BaseViewModel
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(
private val localDataSource: WaveLocalDataSource
private val waveLocalDataStorage: WaveLocalDataSource,
private val postUserLoginUseCase: PostUserLoginUseCase
) : BaseViewModel<LoginContract.LoginEvent, LoginContract.LoginState, LoginContract.LoginEffect>() {

override fun createInitialState(): LoginContract.LoginState {
Expand All @@ -20,8 +24,8 @@ class LoginViewModel @Inject constructor(

override suspend fun handleEvent(event: LoginContract.LoginEvent) {
when (event) {
is LoginContract.LoginEvent.EmailChanged -> {
setState(currentUiState.copy(email = event.email))
is LoginContract.LoginEvent.UsernameChanged -> {
setState(currentUiState.copy(username = event.username))
}

is LoginContract.LoginEvent.PasswordChanged -> {
Expand All @@ -33,19 +37,29 @@ class LoginViewModel @Inject constructor(
}

is LoginContract.LoginEvent.OnLoginBtnClicked -> {
if (checkIsUserEmail() && checkIsUserPassword()) {
postUserLoginUseCase(
userLoginEntity = UserLoginEntity(
username = currentUiState.username,
userPassword = currentUiState.password
)
).onSuccess { user ->
Timber.d("[로그인] 성공 -> $user")
setEffect(LoginContract.LoginEffect.ShowSuccessSnackBar(successMessage = event.successMessage))
setAccessToken(user.accessToken)
setState(currentUiState.copy(loginStatus = LoginContract.LoginStatus.Success))
} else {
}.onFailure {
Timber.d("[로그인] 실패 -> $it")
setEffect(LoginContract.LoginEffect.ShowFailSnackBar(failMessage = event.failMessage))
setState(currentUiState.copy(loginStatus = LoginContract.LoginStatus.Fail))
}
}
}
}

private fun checkIsUserEmail(): Boolean = (currentUiState.email == localDataSource.userEmail)

private fun checkIsUserPassword(): Boolean =
(currentUiState.password == localDataSource.userPassword)
private fun setAccessToken(token: String) {
with(waveLocalDataStorage) {
accessToken = token
isLogin = true
}
}
}

0 comments on commit 04965b9

Please sign in to comment.