Skip to content

Commit

Permalink
Implement refresh token functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
axiel7 committed May 15, 2023
1 parent c84d2ac commit 6dd1860
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/axiel7/moelist/data/network/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6dd1860

Please sign in to comment.