restorePreviousSignIn() does not fetch a new idToken on expiring with GIDSignIn-iOS SDK V6.0.2

163 Views Asked by At

I have implemented a Google Sign in flow to my iOS. First login works perfectly but after 1 hour when the idToken provided by Google expires, the restorePreviousSignIn() method is called but returns the same idToken. Can someone post an implementation of the restorePreviousSignIn() method

My restorePreviousSignIn() method looks as below-

GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in

    if error != nil || user == nil {
       print("Could not sign in user previously.\nError: \(String(describing: error))")
    }
    else {
    
    }
}
1

There are 1 best solutions below

2
Rod On

Try this:

GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
    if error != nil || user == nil {
        // Prompt the user to sign in again
        GIDSignIn.sharedInstance.signIn()
    } else {
        if let auth = user.authentication {
            if auth.accessTokenExpirationDate.timeIntervalSinceNow < 0 {
                // Access token has expired, refresh the token
                auth.refreshToken { refreshedAuth, error in
                    if error != nil || refreshedAuth == nil {
                        // Refresh failed, prompt the user to sign in again
                        GIDSignIn.sharedInstance.signIn()
                    } else {
                        // Refresh succeeded, update the user's authentication object
                        user.authentication = refreshedAuth
                        // Use the refreshed idToken to authenticate the user
                        // Your code here
                    }
                }
            } else {
                // Access token is still valid, use the existing idToken to authenticate the user
                // Your code here
            }
        } else {
            // Prompt the user to sign in again
            GIDSignIn.sharedInstance.signIn()
        }
    }
}