-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature/refresh-token] 토큰 자동 갱신 로직 추가 및 자동 로그인 기능 추가 (#390)
* 토큰 자동 갱신 로직 추가 * Qualifier 수정하고 자동로그인 로직 추가 * remove useless expression * remove useless expressions and apply ktlint
- Loading branch information
Showing
11 changed files
with
112 additions
and
11 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
app/src/main/java/org/sopt/official/data/authenticator/SoptAuthenticator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.sopt.official.data.authenticator | ||
|
||
import android.content.Context | ||
import com.jakewharton.processphoenix.ProcessPhoenix | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Authenticator | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.Route | ||
import org.sopt.official.common.di.Auth | ||
import org.sopt.official.data.model.request.RefreshRequest | ||
import org.sopt.official.data.persistence.SoptDataStore | ||
import org.sopt.official.data.service.AuthService | ||
import org.sopt.official.feature.auth.AuthActivity | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class SoptAuthenticator @Inject constructor( | ||
private val dataStore: SoptDataStore, | ||
@Auth(false) private val service: AuthService, | ||
@ApplicationContext private val context: Context | ||
) : Authenticator { | ||
override fun authenticate(route: Route?, response: Response): Request? { | ||
if (response.code == 401) { | ||
val refreshToken = dataStore.refreshToken | ||
val newTokens = runCatching { | ||
runBlocking { | ||
service.refresh(RefreshRequest(refreshToken)) | ||
} | ||
}.onSuccess { | ||
dataStore.refreshToken = it.refreshToken | ||
dataStore.accessToken = it.accessToken | ||
dataStore.playgroundToken = it.playgroundToken | ||
}.onFailure { | ||
dataStore.clear() | ||
Timber.e(it) | ||
ProcessPhoenix.triggerRebirth(context, AuthActivity.newInstance(context)) | ||
}.getOrThrow() | ||
|
||
return response.request.newBuilder() | ||
.header("Authorization", newTokens.accessToken) | ||
.build() | ||
} | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters