Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FilipeLipan/6a2dde783ac33b5188ba5c653ec3f860 to your computer and use it in GitHub Desktop.
Save FilipeLipan/6a2dde783ac33b5188ba5c653ec3f860 to your computer and use it in GitHub Desktop.
authenticator
package br.com.avon.common.repository.remote.util;
import java.io.IOException;
import javax.annotation.Nullable;
import br.com.avon.common.BuildConfig;
import br.com.avon.common.entity.response.mobile_commerce.RenewTokenResponse;
import br.com.avon.common.infrastructure.MyApplication;
import br.com.avon.common.repository.remote.RestApi;
import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import retrofit2.Call;
public class TokenAuthenticator implements Authenticator {
@Nullable
@Override
public Request authenticate(Route route, Response response) throws IOException {
if (response.code() == 401) {
Call<RenewTokenResponse> refreshCall = MyApplication.getRestApi().getRenewToken()
retrofit2.Response<RenewTokenResponse> refreshResponse = refreshCall.execute();
if (refreshResponse != null && refreshResponse.code() == 200 && refreshResponse.body() != null) {
MyApplication.getLocalProperties().setRefreshToken(refreshResponse.body().getToken());
MyApplication.getLocalProperties().setToken(refreshResponse.body().getToken());
return response.request().newBuilder()
.removeHeader(RestApi.TOKEN)
.addHeader(RestApi.TOKEN, refreshResponse.body().getToken())
.build();
} else {
return null;
}
}else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment