From 6dd18607cd27e49cc876d10d09b0d97a90a82ed0 Mon Sep 17 00:00:00 2001 From: Axel Date: Mon, 15 May 2023 12:47:31 +0900 Subject: [PATCH] Implement refresh token functionality --- .../data/datastore/PreferencesDataStore.kt | 1 + .../com/axiel7/moelist/data/network/Api.kt | 5 ++-- .../moelist/data/repository/BaseRepository.kt | 27 +++++++++++++++++-- .../moelist/uicompose/login/LoginViewModel.kt | 2 ++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/axiel7/moelist/data/datastore/PreferencesDataStore.kt b/app/src/main/java/com/axiel7/moelist/data/datastore/PreferencesDataStore.kt index 590368d0f..024e38297 100644 --- a/app/src/main/java/com/axiel7/moelist/data/datastore/PreferencesDataStore.kt +++ b/app/src/main/java/com/axiel7/moelist/data/datastore/PreferencesDataStore.kt @@ -22,6 +22,7 @@ import kotlinx.coroutines.runBlocking object PreferencesDataStore { val ACCESS_TOKEN_PREFERENCE_KEY = stringPreferencesKey("access_token") + val REFRESH_TOKEN_PREFERENCE_KEY = stringPreferencesKey("refresh_token") val NSFW_PREFERENCE_KEY = booleanPreferencesKey("nsfw") val LANG_PREFERENCE_KEY = stringPreferencesKey("lang") val THEME_PREFERENCE_KEY = stringPreferencesKey("theme") diff --git a/app/src/main/java/com/axiel7/moelist/data/network/Api.kt b/app/src/main/java/com/axiel7/moelist/data/network/Api.kt index 8914a6791..f41c6dbca 100644 --- a/app/src/main/java/com/axiel7/moelist/data/network/Api.kt +++ b/app/src/main/java/com/axiel7/moelist/data/network/Api.kt @@ -50,13 +50,12 @@ class Api(private val client: HttpClient) { suspend fun getAccessToken( clientId: String, - refreshToken: String, - grantType: String + refreshToken: String ): AccessToken = client.post("${MAL_OAUTH2_URL}token") { setBody(FormDataContent(Parameters.build { append("client_id", clientId) append("refresh_token", refreshToken) - append("grant_type", grantType) + append("grant_type", "refresh_token") })) }.body() diff --git a/app/src/main/java/com/axiel7/moelist/data/repository/BaseRepository.kt b/app/src/main/java/com/axiel7/moelist/data/repository/BaseRepository.kt index 3e0ab9acf..2a5505152 100644 --- a/app/src/main/java/com/axiel7/moelist/data/repository/BaseRepository.kt +++ b/app/src/main/java/com/axiel7/moelist/data/repository/BaseRepository.kt @@ -3,15 +3,38 @@ package com.axiel7.moelist.data.repository import androidx.datastore.preferences.core.edit import com.axiel7.moelist.App import com.axiel7.moelist.data.datastore.PreferencesDataStore.ACCESS_TOKEN_PREFERENCE_KEY +import com.axiel7.moelist.data.datastore.PreferencesDataStore.REFRESH_TOKEN_PREFERENCE_KEY +import com.axiel7.moelist.data.datastore.PreferencesDataStore.getValueSync +import com.axiel7.moelist.private.ClientId object BaseRepository { suspend fun handleResponseError(error: String) { when (error) { "invalid_token" -> { - App.dataStore?.edit { - it.remove(ACCESS_TOKEN_PREFERENCE_KEY) + val refreshToken = App.dataStore?.getValueSync(REFRESH_TOKEN_PREFERENCE_KEY) + if (refreshToken != null) { + try { + val newToken = App.api.getAccessToken( + clientId = ClientId.CLIENT_ID, + refreshToken = refreshToken + ) + App.dataStore?.edit { + it[ACCESS_TOKEN_PREFERENCE_KEY] = newToken.accessToken!! + it[REFRESH_TOKEN_PREFERENCE_KEY] = newToken.refreshToken!! + } + } catch (e: Exception) { + deleteAccessToken() + } + } else { + deleteAccessToken() } } } } + + private suspend fun deleteAccessToken() { + App.dataStore?.edit { + it.remove(ACCESS_TOKEN_PREFERENCE_KEY) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/axiel7/moelist/uicompose/login/LoginViewModel.kt b/app/src/main/java/com/axiel7/moelist/uicompose/login/LoginViewModel.kt index 5096cb670..c48089cbd 100644 --- a/app/src/main/java/com/axiel7/moelist/uicompose/login/LoginViewModel.kt +++ b/app/src/main/java/com/axiel7/moelist/uicompose/login/LoginViewModel.kt @@ -7,6 +7,7 @@ import androidx.datastore.preferences.core.edit import androidx.lifecycle.viewModelScope import com.axiel7.moelist.App import com.axiel7.moelist.data.datastore.PreferencesDataStore.ACCESS_TOKEN_PREFERENCE_KEY +import com.axiel7.moelist.data.datastore.PreferencesDataStore.REFRESH_TOKEN_PREFERENCE_KEY import com.axiel7.moelist.data.model.AccessToken import com.axiel7.moelist.data.network.Api import com.axiel7.moelist.data.network.KtorClient @@ -45,6 +46,7 @@ class LoginViewModel: BaseViewModel() { else { App.dataStore?.edit { it[ACCESS_TOKEN_PREFERENCE_KEY] = accessToken!!.accessToken!! + it[REFRESH_TOKEN_PREFERENCE_KEY] = accessToken!!.refreshToken!! } App.createKtorClient(accessToken = accessToken!!.accessToken!!) loginWasOk = true