pls pls - 2 months ago 39
Swift Question

GIDSignIn.sharedInstance().currentUser nil when relaunching app

Initially I was having a problem with the check to see if we have saved data into the keychain. I found this post and followed the suggestion by BhavinBhadani:


Whenever you check if ([GIDSignIn sharedInstance].hasAuthInKeychain) ,
before that add your sign in scopes..


https://github.com/googlesamples/google-services/issues/27

This has solved my first problem of
GIDSignIn.sharedInstance().hasAuthInKeychain()
returning true after the user has logged in successfully before and then has killed app and re runs.

However now I am doing the following:

let signIn = GIDSignIn.sharedInstance()
signIn.scopes = [Constants.GOOGLE_CLIENT_SCOPE, Constants.GOOGLE_OIDC_SCOPE]

if GIDSignIn.sharedInstance().hasAuthInKeychain() {
print("We have saved in keychain")

if let u = GIDSignIn.sharedInstance().currentUser {
getData(dataApi)
}
else {
// THIS CODE IS BEING SUCCESSFULLY HIT
GIDSignIn.sharedInstance().signInSilently()
getApplicationData(dataApi)
}
}


However even after doing:
GIDSignIn.sharedInstance().signInSilently()
the
GIDSignIn.sharedInstance().currentUser
is nil.

Has anyone successfully used
signInSilently()
to populate the currentUser data again?

Answer

I make a point in that issue in git that you also need to add scopes before check hasAuthInKeychain like this .. this works for me and hope same for you

 let signIn = GIDSignIn.sharedInstance()
signIn.scopes = ["https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/plus.me"]

  if (signin.hasAuthInKeychain) {
        print("Signed in");
  } else {
        print("Not signed in");
  }

make sure after GIDSignIn.sharedInstance().signInSilently(), there is some delay to call getApplicationData(dataApi) to populate data.