Skip to content

Commit a93e2d3

Browse files
authored
Merge pull request #85 from YAPP-Github/refactor/#83-auto-login
[Refactor/#83] 자동 로그인 로직 변경
2 parents 5de90c6 + d22e0e4 commit a93e2d3

File tree

19 files changed

+130
-73
lines changed

19 files changed

+130
-73
lines changed

app/src/main/java/com/threegap/bitnagil/MainNavHost.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ fun MainNavHost(
3838
popUpTo<Route.Splash> { inclusive = true }
3939
}
4040
},
41+
navigateToTermsAgreement = {
42+
navigator.navController.navigate(Route.TermsAgreement) {
43+
popUpTo<Route.Splash> { inclusive = true }
44+
}
45+
},
46+
navigateToOnboarding = {
47+
navigator.navController.navigate(Route.OnBoarding()) {
48+
popUpTo<Route.Splash> { inclusive = true }
49+
}
50+
},
4151
navigateToHome = navigator::navigateToHomeAndClearStack,
4252
)
4353
}

core/datastore/src/main/java/com/threegap/bitnagil/datastore/auth/storage/AuthTokenDataStore.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ import kotlinx.coroutines.flow.Flow
55

66
interface AuthTokenDataStore {
77
val tokenFlow: Flow<AuthToken>
8-
9-
suspend fun hasToken(): Boolean
10-
118
suspend fun updateAuthToken(accessToken: String, refreshToken: String)
12-
139
suspend fun updateAccessToken(accessToken: String)
14-
1510
suspend fun updateRefreshToken(refreshToken: String)
16-
1711
suspend fun clearAuthToken()
1812
}

core/datastore/src/main/java/com/threegap/bitnagil/datastore/auth/storage/AuthTokenDataStoreImpl.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,12 @@ package com.threegap.bitnagil.datastore.auth.storage
33
import androidx.datastore.core.DataStore
44
import com.threegap.bitnagil.datastore.auth.model.AuthToken
55
import kotlinx.coroutines.flow.Flow
6-
import kotlinx.coroutines.flow.firstOrNull
76

87
class AuthTokenDataStoreImpl(
98
private val dataStore: DataStore<AuthToken>,
109
) : AuthTokenDataStore {
1110
override val tokenFlow: Flow<AuthToken> = dataStore.data
1211

13-
override suspend fun hasToken(): Boolean {
14-
return try {
15-
val currentToken = dataStore.data.firstOrNull()
16-
currentToken?.let {
17-
!it.accessToken.isNullOrEmpty() && !it.refreshToken.isNullOrEmpty()
18-
} ?: false
19-
} catch (e: Exception) {
20-
false
21-
}
22-
}
23-
2412
override suspend fun updateAuthToken(accessToken: String, refreshToken: String) {
2513
try {
2614
dataStore.updateData {
@@ -58,8 +46,4 @@ class AuthTokenDataStoreImpl(
5846
throw e
5947
}
6048
}
61-
62-
companion object {
63-
private const val TAG = "AuthTokenDataStore"
64-
}
6549
}

core/network/src/main/java/com/threegap/bitnagil/network/auth/TokenAuthenticator.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TokenAuthenticator(
1919
private val authMutex = Mutex()
2020

2121
override fun authenticate(route: Route?, response: Response): Request? {
22+
if (response.request.header(AUTO_LOGIN_HEADER) != null) return null
2223
if (!shouldRetry(response)) return null
2324

2425
val currentToken = runBlocking { tokenProvider.getAccessToken() }
@@ -82,6 +83,7 @@ class TokenAuthenticator(
8283
private fun buildRequestWithToken(originalRequest: Request, token: String): Request {
8384
return originalRequest.newBuilder()
8485
.header(AUTHORIZATION, "$TOKEN_PREFIX $token")
86+
.removeHeader(AUTO_LOGIN_HEADER)
8587
.build()
8688
}
8789

@@ -96,5 +98,6 @@ class TokenAuthenticator(
9698
private const val AUTHORIZATION = "Authorization"
9799
private const val TOKEN_PREFIX = "Bearer"
98100
private const val SUCCESS_CODE = "CO000"
101+
private const val AUTO_LOGIN_HEADER = "Auto-Login"
99102
}
100103
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.threegap.bitnagil.data.auth.datasource
22

33
interface AuthLocalDataSource {
4-
suspend fun hasToken(): Boolean
5-
4+
suspend fun getRefreshToken(): String?
65
suspend fun updateAuthToken(accessToken: String, refreshToken: String): Result<Unit>
7-
86
suspend fun clearAuthToken(): Result<Unit>
97
}

data/src/main/java/com/threegap/bitnagil/data/auth/datasource/AuthRemoteDataSource.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import com.threegap.bitnagil.data.auth.model.response.LoginResponseDto
66

77
interface AuthRemoteDataSource {
88
suspend fun login(socialAccessToken: String, loginRequestDto: LoginRequestDto): Result<LoginResponseDto>
9-
109
suspend fun submitAgreement(termsAgreementRequestDto: TermsAgreementRequestDto): Result<Unit>
11-
1210
suspend fun logout(): Result<Unit>
13-
1411
suspend fun withdrawal(): Result<Unit>
12+
suspend fun reissueToken(refreshToken: String): Result<LoginResponseDto>
1513
}

data/src/main/java/com/threegap/bitnagil/data/auth/datasourceimpl/AuthLocalDataSourceImpl.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package com.threegap.bitnagil.data.auth.datasourceimpl
22

33
import com.threegap.bitnagil.data.auth.datasource.AuthLocalDataSource
44
import com.threegap.bitnagil.datastore.auth.storage.AuthTokenDataStore
5+
import kotlinx.coroutines.flow.firstOrNull
56
import javax.inject.Inject
67

78
class AuthLocalDataSourceImpl @Inject constructor(
89
private val authTokenDataStore: AuthTokenDataStore,
910
) : AuthLocalDataSource {
1011

11-
override suspend fun hasToken(): Boolean = authTokenDataStore.hasToken()
12+
override suspend fun getRefreshToken(): String? =
13+
runCatching {
14+
authTokenDataStore.tokenFlow.firstOrNull()?.refreshToken
15+
}.getOrNull()
1216

1317
override suspend fun updateAuthToken(accessToken: String, refreshToken: String): Result<Unit> =
1418
runCatching {

data/src/main/java/com/threegap/bitnagil/data/auth/datasourceimpl/AuthRemoteDataSourceImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ class AuthRemoteDataSourceImpl @Inject constructor(
3131
safeUnitApiCall {
3232
authService.postWithdrawal()
3333
}
34+
35+
override suspend fun reissueToken(refreshToken: String): Result<LoginResponseDto> =
36+
safeApiCall {
37+
authService.postReissueToken(refreshToken)
38+
}
3439
}

data/src/main/java/com/threegap/bitnagil/data/auth/repositoryimpl/AuthRepositoryImpl.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ class AuthRepositoryImpl @Inject constructor(
1818
authRemoteDataSource.login(socialAccessToken, LoginRequestDto(socialType))
1919
.map { it.toDomain() }
2020

21-
override suspend fun hasToken(): Boolean = authLocalDataSource.hasToken()
22-
23-
override suspend fun updateAuthToken(accessToken: String, refreshToken: String): Result<Unit> =
24-
authLocalDataSource.updateAuthToken(accessToken, refreshToken)
25-
2621
override suspend fun submitAgreement(termsAgreement: TermsAgreement): Result<Unit> =
2722
authRemoteDataSource.submitAgreement(
2823
termsAgreement.toDto(),
@@ -39,4 +34,16 @@ class AuthRepositoryImpl @Inject constructor(
3934
if (it.isSuccess) authLocalDataSource.clearAuthToken()
4035
}
4136
}
37+
38+
override suspend fun reissueToken(refreshToken: String): Result<AuthSession> =
39+
authRemoteDataSource.reissueToken(refreshToken).map { it.toDomain() }
40+
41+
override suspend fun getRefreshToken(): String? =
42+
authLocalDataSource.getRefreshToken()
43+
44+
override suspend fun updateAuthToken(accessToken: String, refreshToken: String): Result<Unit> =
45+
authLocalDataSource.updateAuthToken(accessToken, refreshToken)
46+
47+
override suspend fun clearAuthToken(): Result<Unit> =
48+
authLocalDataSource.clearAuthToken()
4249
}

data/src/main/java/com/threegap/bitnagil/data/auth/service/AuthService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ interface AuthService {
2727

2828
@POST("/api/v1/auth/logout")
2929
suspend fun postLogout(): BaseResponse<Unit>
30+
31+
@POST("/api/v1/auth/token/reissue")
32+
@Headers("No-Service-Token: true", "Auto-Login: true")
33+
suspend fun postReissueToken(
34+
@Header("Refresh-Token") refreshToken: String,
35+
): BaseResponse<LoginResponseDto>
3036
}

0 commit comments

Comments
 (0)