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

Async request in willSend Request #834

Closed
rad182 opened this issue Oct 15, 2019 · 5 comments
Closed

Async request in willSend Request #834

rad182 opened this issue Oct 15, 2019 · 5 comments
Labels
question Issues that have a question which should be addressed

Comments

@rad182
Copy link

rad182 commented Oct 15, 2019

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

@cltnschlosser
Copy link
Contributor

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.)

@rad182
Copy link
Author

rad182 commented Oct 15, 2019

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

@cltnschlosser
Copy link
Contributor

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?

@designatednerd
Copy link
Contributor

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 designatednerd added the question Issues that have a question which should be addressed label Oct 15, 2019
@rad182
Copy link
Author

rad182 commented Oct 16, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

3 participants