Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass Auth bearer token Hilt DI? #1

Open
Neerav006 opened this issue Dec 15, 2020 · 1 comment
Open

How to pass Auth bearer token Hilt DI? #1

Neerav006 opened this issue Dec 15, 2020 · 1 comment

Comments

@Neerav006
Copy link

Hello , This is not issue , I'm new to mvvm structure, How to pass Auth bearer token in NetworkModule from token stored in Preference. Give some solution would be nice.

@saitej-janjirala
Copy link
Contributor

@Neerav006 you can create a custom interceptor for your HttpBuilder in that you can add like this.

val httpClientBuilder = OkHttpClient.Builder().apply {

                    addInterceptor(object :Interceptor{
                        override fun intercept(chain: Interceptor.Chain): Response {
                            val originalRequest = chain.request()

                            val newRequest = originalRequest.newBuilder().apply {

                                headers(Headers.Builder().apply {
                                    addAll(originalRequest.headers)
                                    val token = AppPreferences.getAccessToken()?.accessToken
                                    if(token != null) {
                                        add("Authorization", token)
                                        if(BuildConfig.DEBUG){
                                            Log.i("Authorization", "token : $token")
                                        }
                                    }
                                }.build())
                            }.build()
                            return chain.proceed(newRequest)
                        }

                    })
                }

tempInstance = Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClientBuilder.build())
.build()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants