Last active
October 11, 2024 10:11
-
-
Save EmmanuelGuther/fa73a1139900abed33c0750fe7cd44e9 to your computer and use it in GitHub Desktop.
Get rest api with Auth - token and using okhttp Authenticator to manage the refresh token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object RetrofitGetAPIWithAuth { | |
private val BASE_URL = BuildConfig.API_BASE | |
private var retrofit: Retrofit? = null | |
private val tokenLoginModel: TokenLoginModel? = obtainTokenLoginModel() | |
private val accessToken = tokenLoginModel?.access_token | |
class HeaderInterceptor : Interceptor { | |
@Throws(IOException::class) | |
override fun intercept(chain: Interceptor.Chain): Response { | |
var request = chain.request() | |
request = request.newBuilder() | |
.addHeader("Authorization", "Bearer " + accessToken) | |
.build() | |
return chain.proceed(request) | |
} | |
} | |
private val okHttpClient = OkHttpClient().newBuilder() | |
.addInterceptor(HeaderInterceptor()).authenticator(TokenAuthenticator()) | |
.build() | |
val retrofitClient: Retrofit? | |
get() { | |
if (retrofit == null) { | |
retrofit = Retrofit.Builder() | |
.client(okHttpClient) | |
.baseUrl(BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
return retrofit | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TokenAuthenticator : Authenticator { | |
override fun authenticate(route: Route?, response: Response?): Request? { | |
val tokenLoginModel: TokenLoginModel? = obtainTokenLoginModel() | |
val apiService = RetrofitLogin.client!!.create(ApiInterface::class.java) | |
val newAccessToken: String? = apiService.refreshToken(BuildConfig.GRANT_TYPE_LOGIN, BuildConfig.CLIENT_ID_LOGIN, | |
tokenLoginModel?.access_token, tokenLoginModel?.refresh_token, 20.toString()).execute().body()?.access_token.toString() | |
// Add new header to rejected request and retry it | |
return response?.request()?.newBuilder()?.header("Authorization", newAccessToken)?.build() | |
} | |
} | |
fun obtainTokenLoginModel(): TokenLoginModel? { | |
val fooRawTk: Any? = SharedPreferencesHelper().load(Constants.SHARED_PREFS_AUTH_TOKEN) | |
return CryptoHelp().decrypt(fooRawTk.toString()) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment