GoogleSignIn (iOS) getTokensWithHandler doesn't call its closure after token has expired

Viewed 146

I am using Google Sign-In 5.0.2 with a Swift 4 iOS app. Here is my code to get a current id Token:

public static func getJwtToken(completion: @escaping (Result<String, Error>) -> Void) {
        assert(Config.isAuthEnabled())
                
        GIDSignIn.sharedInstance()?.currentUser?.authentication.getTokensWithHandler({ gidAuth, error in
            if let error = error {
                completion(.failure(error))
            } else if let token = gidAuth?.idToken {
                completion(.success(token))
            } else {
                assertionFailure("shouldn't have come here")
            }
        })
    }

This works fine when I launch the app. But if I leave the app running for an hour (to let the id token expire), then the next time getTokensWithHandler is called, it will not call my closure. Subsequent calls to getTokensWithHandler will once again call my closure.

I would like it to behave consistently so that my closure is always called, even if the token needs to be refreshed.

Anyone have any ideas on what I need to do to achieve this?

1 Answers

I figured out the answer to this problem after spending some more time on it.

My problem was I was trying to hack the Google SDK's closure to be synchronous instead of just letting it call the closure when it was ready. The function call that I claimed never called its closure would've called it if I had let the program flow naturally instead of trying to make it call the closure when I expected it to be called. I was using a semaphore somewhere in the call stack that was waiting until the closure got called and I just needed stop making assumptions about when Google's SDK would perform its work.

I'm still trying to get used to this async-first paradigm of Swift.

Related