-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/#90 token #93
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
Changes from 6 commits
2f3ef03
99eb1e1
3ed9967
e1e79fd
1302575
ac0f701
b160844
1a01d63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.yapp.core.data.remote.model.request | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class ReissueTokenRequest( | ||
val accessToken : String, | ||
val refreshToken : String | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.yapp.core.data.remote.retrofit | ||
|
||
import com.yapp.core.data.local.SecurityPreferences | ||
import com.yapp.core.data.remote.api.AuthApi | ||
import com.yapp.core.data.remote.model.request.ReissueTokenRequest | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Authenticator | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.Route | ||
import javax.inject.Inject | ||
|
||
internal class TokenAuthenticator @Inject constructor( | ||
private val securityPreferences: SecurityPreferences, | ||
private val authApi: AuthApi, | ||
) : Authenticator { | ||
|
||
override fun authenticate(route: Route?, response: Response): Request? { | ||
return runBlocking { | ||
val accessToken = securityPreferences.flowAccessToken().firstOrNull().orEmpty() | ||
ashwon12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val refreshToken = securityPreferences.flowRefreshToken().firstOrNull().orEmpty() | ||
|
||
val request = ReissueTokenRequest( | ||
accessToken = accessToken, | ||
refreshToken = refreshToken | ||
) | ||
|
||
val (newAccessToken, newRefreshToken) = try { | ||
val response = authApi.reissueToken(request) | ||
securityPreferences.setAccessToken(response.accessToken) | ||
securityPreferences.setRefreshToken(response.refreshToken) | ||
|
||
response.accessToken to response.refreshToken | ||
} catch (e: Exception) { | ||
"" to "" | ||
} | ||
ashwon12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (newAccessToken.isBlank() || newRefreshToken.isBlank()) { | ||
resetToken() | ||
return@runBlocking null | ||
} | ||
|
||
response.request.newBuilder() | ||
.header("Authorization", "Bearer $newAccessToken") | ||
.build() | ||
} | ||
} | ||
|
||
private fun resetToken() { | ||
runBlocking { | ||
securityPreferences.setAccessToken("") | ||
securityPreferences.setRefreshToken("") | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐งน Nitpick (assertive) resetToken์์ runBlocking ์ฌ์ฉ ์์ ๋ ๋น๋๊ธฐ ์ ํ์ ๊ณ ๋ คํด ์ฃผ์ธ์. ํ ํฐ ๋ฆฌ์
์์ ์์ ์ญ์ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A: resetToken ๋ถ๋ถ์ suspend function์ผ๋ก ์ก์ผ๋ฉด ์ข๊ธด ํ๊ฒ ๊ตฐ์ |
||
} |
Uh oh!
There was an error while loading. Please reload this page.