Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/#58] auth #59

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kkkk.data.dataSource

import com.kkkk.data.dto.BaseResponse
import com.kkkk.data.dto.request.AuthRequestDto
import com.kkkk.data.dto.response.AuthTokenDto

interface AuthDataSource {
Expand All @@ -9,6 +10,10 @@ interface AuthDataSource {
): BaseResponse<AuthTokenDto>

suspend fun postLogin(
deviceTag: String,
auth: AuthRequestDto,
): BaseResponse<AuthTokenDto>

suspend fun postSignUp(
auth: AuthRequestDto,
): BaseResponse<AuthTokenDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kkkk.data.dataSourceImpl

import com.kkkk.data.dataSource.AuthDataSource
import com.kkkk.data.dto.BaseResponse
import com.kkkk.data.dto.request.AuthRequestDto
import com.kkkk.data.dto.response.AuthTokenDto
import com.kkkk.data.service.AuthService
import javax.inject.Inject
Expand All @@ -12,10 +13,13 @@ constructor(
private val authService: AuthService,
) : AuthDataSource {
override suspend fun postReissueTokens(
authorization: String
authorization: String,
): BaseResponse<AuthTokenDto> = authService.postReissueTokens(authorization)

override suspend fun postLogin(
deviceTag: String
): BaseResponse<AuthTokenDto> = authService.postLogin(deviceTag)
auth: AuthRequestDto,
): BaseResponse<AuthTokenDto> = authService.postLogin(auth)

override suspend fun postSignUp(auth: AuthRequestDto): BaseResponse<AuthTokenDto> =
authService.postSignUp(auth)
}
12 changes: 12 additions & 0 deletions data/src/main/java/com/kkkk/data/dto/request/AuthRequestDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kkkk.data.dto.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class AuthRequestDto(
@SerialName("deviceTag")
val deviceTag: String,
@SerialName("password")
val password: String
)
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package com.kkkk.data.repositoryImpl

import com.kkkk.data.dataSource.AuthDataSource
import com.kkkk.data.dto.request.AuthRequestDto
import com.kkkk.domain.entity.response.AuthTokenModel
import com.kkkk.domain.repository.AuthRepository
import javax.inject.Inject

class AuthRepositoryImpl
@Inject
constructor(
class AuthRepositoryImpl @Inject constructor(
private val authDataSource: AuthDataSource,
) : AuthRepository {
override suspend fun reissueTokens(
authorization: String
authorization: String,
): Result<AuthTokenModel> = runCatching {
authDataSource.postReissueTokens(
authorization,
).data.toModel()
}

override suspend fun login(
deviceTag: String
deviceTag: String,
): Result<AuthTokenModel> = runCatching {
authDataSource.postLogin(deviceTag).data.toModel()
authDataSource.postLogin(
AuthRequestDto(
deviceTag = deviceTag,
password = ""
)
).data.toModel()
}

override suspend fun signup(deviceTag: String): Result<AuthTokenModel> = runCatching {
authDataSource.postSignUp(
AuthRequestDto(
deviceTag = deviceTag,
password = ""
)
).data.toModel()
}
}
12 changes: 9 additions & 3 deletions data/src/main/java/com/kkkk/data/service/AuthService.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.kkkk.data.service

import com.kkkk.data.dto.BaseResponse
import com.kkkk.data.dto.request.AuthRequestDto
import com.kkkk.data.dto.request.TokenRequestDto
import com.kkkk.data.dto.response.AuthTokenDto
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.POST

interface AuthService {
@POST("api/v1/reissue")
@POST("api/v1/auth/reissue")
suspend fun postReissueTokens(
@Header("Authorization") authorization: String
): BaseResponse<AuthTokenDto>

@POST("api/v1/login")
@POST("api/v1/auth/login")
suspend fun postLogin(
@Header("Authorization") deviceTag: String,
@Body auth: AuthRequestDto,
): BaseResponse<AuthTokenDto>

@POST("api/v1/auth/register")
suspend fun postSignUp(
@Body auth: AuthRequestDto,
): BaseResponse<AuthTokenDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ interface AuthRepository {
suspend fun login(
deviceTag: String
): Result<AuthTokenModel>

suspend fun signup(
deviceTag: String
): Result<AuthTokenModel>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.kkkk.presentation.onboarding.onbarding

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import android.provider.Settings
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
Expand Down Expand Up @@ -56,7 +58,7 @@ class OnboardingActivity : BaseActivity<ActivityOnboardingBinding>(R.layout.acti

override fun onFinish() {
with(viewModel) {
setBpmLevel()
setBpmLevel(getDeviceTag())
setState(OnboardingState.END)
}
}
Expand All @@ -76,6 +78,10 @@ class OnboardingActivity : BaseActivity<ActivityOnboardingBinding>(R.layout.acti
}
}

@SuppressLint("HardwareIds")
private fun getDeviceTag(): String =
Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)

companion object {
private const val TIME = 60000L
private const val INTERVAL = 1000L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.kkkk.presentation.onboarding.onbarding

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kkkk.domain.repository.AuthRepository
import com.kkkk.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class OnboardingViewModel @Inject constructor(
private val userRepository: UserRepository
private val userRepository: UserRepository,
private val authRepository: AuthRepository,
) : ViewModel() {
private val _state = MutableStateFlow(OnboardingState.START)
val state: StateFlow<OnboardingState> = _state
Expand Down Expand Up @@ -38,9 +43,14 @@ class OnboardingViewModel @Inject constructor(
_state.value = newState
}

fun setBpmLevel() {
fun setBpmLevel(deviceTag: String) {
val bpm = _speed.value / (_stepCount.value / SPEED_CALC_INTERVAL)
userRepository.setBpm(bpm.toInt())

viewModelScope.launch {
authRepository.signup(deviceTag).onSuccess {
userRepository.setBpm(bpm.toInt())
}.onFailure(Timber::e)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_spl
private fun observeUserState() {
viewModel.userState.flowWithLifecycle(lifecycle).onEach { isSuccess ->
if (isSuccess) {
navigateToScreenClear<OnboardingActivity>()
navigateToScreenClear<MainActivity>()
} else {
toast(getString(R.string.error_msg))
navigateToScreenClear<OnboardingActivity>()
}
}.launchIn(lifecycleScope)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -39,7 +38,9 @@ class SplashViewModel @Inject constructor(
userRepository.setTokens(response.accessToken, response.refreshToken)
userRepository.setDeviceToken(deviceTag)
_userState.emit(true)
}.onFailure(Timber::e)
}.onFailure { error -> // 401일 때 회원가입으로 이동 하도록 구현 필요
_userState.emit(false)
}
}
}

Expand Down
Loading