Has anyone devised a good way to add a dynamic aut...
# apollo-kotlin
c
Has anyone devised a good way to add a dynamic auth header to a multiplatform apollo client? Seems like the recommended OkHttp interceptor would only work on Android, right?
m
Yup, all the docs are for the JVM runtime.
You could handle this in platform specific code and use an OkHttp interceptor for the JVM though as this is battle-tested
And on iOS, the NetworkTransport takes a DataTaskFactory so maybe you could inject the header there?
The only thing to be aware of is I think the DataTaskFactory gets called on the main thread
c
Thanks for the context! I think I'll just wrap my apollo client in a provider that is aware of the auth token and creates a new one when it is changed
πŸ‘ 1
r
Use an
ApolloRequestInterceptor
. The constructor of
ApolloClient
has an
interceptors
parameter that takes a list of these. I use it in common code to add bearer tokens to my requests. There is a built-in one called
BearerTokenInterceptor
that you can use as an example.
❀️ 1
πŸ‘ 1
@mbonnin You were the one to originally point this out to me!
m
Yup, that works too! Thanks for the reminder!
I actually wrote this
BearerTokenInterceptor
πŸ™ˆ
r
Lol, funny how we sometimes forget our own work.
I had to customize it for my situation to deal with token expiry. Its been working really well on both Android and iOS.
m
How do you handle the token refresh ? With ktor or do you wrote custom iOS/Android HTTP code?
r
I use Firebase Auth, which I'm accessing via GitLive's Firebase MPP library (https://github.com/GitLiveApp/firebase-kotlin-sdk). All my integration code is thus in commonMain!
m
Ooooho that's nice ✨
r
It is quite lovely πŸ™‚
m
I might have broken something πŸ™ˆ .
-dev
releases change quite fast these days...
p
I've had the debugger and some logs in, and the Interceptor methods are being called to retrieve the token from what I can see. But they don't actually end up in the request so I get a 401
m
Next
-dev
will have a HttpRequestInterceptor that should make adding a header easier: https://github.com/apollographql/apollo-android/pull/3151
p
Worth noting I was on Kotlin 1.4.2 (mt coroutines), so I bumped to 1.5.0 and 1.5.10 which made no difference either
m
I'm guessing this above PR is maybe what broke the headers stuff
p
Maybe I can roll back a few versions and see. Does that client building code look okay?
m
Looks good to me!
πŸ‘Œ 1
For next version, I'm looking at something like:
Copy code
return ApolloClient(
            networkTransport = ApolloHttpNetworkTransport(
                serverUrl = "<https://core-gql-sohj3qqawq-ew.a.run.app/graphql>",
                headers = mapOf(
                    "Accept" to "application/json",
                    "Content-Type" to "application/json"
                ),              
            ).withHttpInterceptor(BearerTokenInterceptor(tokenProvider)
        )
            .withCustomScalarAdapter(DateTimeAdapter.graphQlName, DateTimeAdapter())
            .withCustomScalarAdapter(DateAdapter.graphQlName, DateAdapter())
            .withStore(apolloStore)
I think it makes sense for the Authorization header to be part of a HttpInterceptor, like with OkHttp, and not at the GraphQLInterceptor level
p
Yeah I agree, that would be nice!
r
Yes, agreed. There was a lot of extra rigamarole in the interceptor to extract and modify the the http execution context from the ApolloRequest, and pass through the rest of it. Being able to work directly at the HTTP level will definitely simplify the logic.
p
@mbonnin need me to open an issue for this? or just wait for the next
dev
release?
m
It'll be in the next
dev
release
❀️ 1
Might even be
-alpha
actually 🀞
p
Don't get me excited πŸ˜ƒ
πŸ˜„ 1
m
Looks like @audriusk had the exact same issue 2 min ago : https://github.com/apollographql/apollo-android/issues/3160
p
If it helps I've gone back to
dev9
and it doesn't work there either.
FWIW, I love that the interface of the
BearerTokenInterceptor
takes care of the concurrency by expecting a
suspend
fun. I had so much trouble trying to use the Firebase library mentioned above until I found
BearerTokenInterceptor
m
Looking into the
BearerTokenInterceptor
thing right now. Maybe we can do a
-dev12
quickly.
p
Happy to try it out and report back if you do!! I'm having a tech debt week, so I've got time (dunno if thats a good thing or a bad thing πŸ˜† )
❀️ 1
πŸ˜„ 1
m
Sorry that took a while because we had a custom hook into the HTTP pipeline for Kotlin native that I had to refactor. PR is there: https://github.com/apollographql/apollo-android/pull/3161. I'll merge it once tests pass so that it's in the snapshots and make a version tomorrow
p
Really appreciate you pulling it together so quickly - I'll carve out some time tomorrow to help test it once the version drops πŸ™
@mbonnin just tried
dev12
, working perfectly thanks so much!
πŸŽ‰ 1
m
Ahah you're fast !! I wasn't sure the version was on central yet πŸ˜ƒ
Thanks for the feedback!
286 Views