Apollo-ios: Async request in willSend Request

Created on 15 Oct 2019  路  5Comments  路  Source: apollographql/apollo-ios

Hello I'm using firebase authentication and in order to get current user's token is I need to call an async function.

image

Could anyone shed a light on how I should do this?TIA

question

All 5 comments

You would need to create a blocking version of getIDTokenForcingRefresh that returns idToken. It's not safe to modify an inout variable after the function has returned which is why the the compiler is complaining. Make sure blocking is actually safe here though (this isn't happening on the main thread, etc.)

@cltnschlosser thanks for the response, however I'm not entirely sure about creating a blocking version. Are you referring to something like async await?

I don't actually know that it's a good idea in this case without some more information, but this is what you would have to do inorder to get that to compile. You would need to do something like: https://stackoverflow.com/questions/47041491/swift-wait-for-closure-thread-to-finish

You will need to make a temporary result var that ends up containing the idToken and then updating the request after the semaphore wait.

Given the information I have this seems like the best approach, but waiting for an asynchronous function here isn't great. Is there no synchronous method for getting a (cached, maybe) idToken?

Supporting this sort of configuration as an async request is something we're planning on, since auth tokens are generally stored in the keychain.

However, I made a gist that shows an example of a way to take an async API and turn it into a sync one so you can use it with the delegate method in question: https://gist.github.com/designatednerd/93405c0751b0c62c3c575d687a409d45 - just make sure you don't call that on the main thread since it blocks.

I would also double check whether it's worth forcing refresh of the token on every single request you make - it seems like that shouldn't be necessary and you might be able to get a token you can store in-memory and then check if it's expired before forcing a refresh.

@designatednerd and @cltnschlosser thanks for this! I will take a look.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

designatednerd picture designatednerd  路  3Comments

wnagrodzki picture wnagrodzki  路  4Comments

Robuske picture Robuske  路  3Comments

uericw picture uericw  路  4Comments

maxsz picture maxsz  路  4Comments