I am trying to retrieve data from an api using retrofit. The request am going to use needs an access token to pass in the header. The problem is that the token expires after 10 min and the refresh token request needs an unexpired token to create a new one! So what should i do to keep the token refreshed by it self before passing 10 min? I already tried Interceptor but it can't work with this type of problem because i need a valid token to get a new one
Asked
Active
Viewed 662 times
1
-
the way you will use an interceptor is by intercepting the first failed request from retrofit and then doing a refresh token method on the spot and replacing your old API request with a new one with the newly created token, and then you will do a retry, this all happens inside the interceptor and can be quite buggy and hard to maintain, take a look at my answer and let me know if you need further help – Mahmoud Omara Dec 09 '20 at 16:44
1 Answers
0
You can use a Worker
and set it to run every 30min or so and set it to save the renewed token in your SharedPreference
here's an example for the Worker
class UpdateTokenWorkManger(
val context: Context,
params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
LoginHandler.refreshTokenSilently()
// Indicate whether the work finished successfully with the Result
return Result.success()
}
companion object {
private const val TAG = "Token Refresh "
const val TOKEN_REFRESH_WORK_MANGER_ID = "automatic_renew_token_work_manger"
fun renewToken() {
val periodicRefreshRequest = PeriodicWorkRequest.Builder(
UpdateTokenWorkManger::class.java, // Your worker class
30, // repeating interval
TimeUnit.MINUTES
)
val periodicWorkRequest: PeriodicWorkRequest = periodicRefreshRequest
.build()
WorkManager.getInstance(App.getApplication()).enqueueUniquePeriodicWork(
TOKEN_REFRESH_WORK_MANGER_ID,
ExistingPeriodicWorkPolicy.REPLACE,
periodicWorkRequest
)
}
}
to use this component you will need these dependencies
implementation "androidx.work:work-runtime-ktx:2.4.0"
also note that LoginHandler
is the class that should be responsible for handling your login, refresh and logout scenarios.
and don't forget to add this line to your first Activity
after the login Activity
, for example: if you login in SplashActivity
and after succesful authentication you redirect to MainActivity
, then this line should be in MainActivity's
onCreate
function
UpdateTokenWorkManger.renewToken()

Mahmoud Omara
- 533
- 6
- 22
-
I tried this code but i don't know why in the log the doWork function works only one single time. and i want to ask you abou App.getApplication() is it normal if i pass a context in the renewToken function as an alternative? and thanks for the answer i really appreciate it – Montassar Selmi Dec 09 '20 at 21:24
-
oh the problem that i was testing it within 1 min w Result.success() every time so workmanager doesn't repeat the work – Montassar Selmi Dec 09 '20 at 21:40
-
you can set it at any time interval you would want, but the minimum is 15min, so you have to type 15 at least for testing – Mahmoud Omara Dec 10 '20 at 16:13
-
and if my answer has helped you, then i would appreciate if you can mark it as the solution – Mahmoud Omara Dec 10 '20 at 16:14
-
yea but the token refreshed every 10 min i need to renew it before thats the problem am facing i think maybe the alarmManager is better for my problem isn't? – Montassar Selmi Dec 10 '20 at 16:17
-
it is very weird seeing a token that needs a refresh every 10min, i think standard is like 30min-1hour, but for this maybe the 2nd approach of an interceptor that retries would be better – Mahmoud Omara Dec 10 '20 at 16:20
-
that's doesn't work either because the problem when the request first failed i'cant use interceptor because the old token has expired and i need an unexpired one to get a new token – Montassar Selmi Dec 10 '20 at 16:27
-
-
-
unless there's a security risk with making it refresh every 15-30min then i think u should request this edit from ur back-end team – Mahmoud Omara Dec 10 '20 at 16:48