I am accessing a REST API through Retrofit and a bearer token must be added to each request. The bearer token expires after an hour and at that point needs to be refreshed. To refresh the token, I have a Retrofit Authenticator that gets a new bearer token when a 401 is given and the API request continues normally. I also have an OkHttp Interceptor that adds the renewed token to every request. I would like to save the bearer token to SharedPreferences and have the Interceptor use it until a 401 is given, and the Authenticator is called again.
I am very new to MVVM and do not know where I should put the token saving logic. Since using a bearer token is so critical to accessing data, I think it might make sense for the Authenticator and Interceptor to handle saving/loading the token. The one downside of this is that this breaks the rule of separation of concerns. Would it make sense for the repository to handle this, if so how could I implement it?
Using MVVM, this is how I would like to manage data.
[ Retrofit ]-
- [Repository] - [ViewModel] - [View]
[SharedPreferences]-
Any advice is greatly appreciated!