I want to take the user to the login screen whenever I encounter a 401 response from the server.
I am currently handling 401 like this:
public abstract class BaseCallback<T> implements Callback<T> {
private final Context context;
public BaseCallback(Context context) {
this.context = context;
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (response.code() == 401) {
// launch login activity using `this.context`
} else {
onSuccess(response.body());
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
}
abstract void onSuccess(T response);
}
Courtesy of https://stackoverflow.com/a/49789543/6341943
But this way, I have to pass context from my ViewModel(my ViewModel is inherited from AndroidViewModel) whenever I make an API call.
Another way I found was to use an interceptor.
I feel like there should be a better way to handle this but I couldn't find something better than this.
How does Google handle this? I couldn't find any such sample apps. I tried ioshed but couldn't find anything like this.
Please point me in the right direction.