GIDSignIn.sharedInstance.currentUser is always nil when app starts

686 Views Asked by At

I'm comming accross an issue similar to this one. Basically every time my app starts, I have to login with my Google Account.

Then, I have this property:

var isGoogleSessionOpen: Bool {
    return GIDSignIn.sharedInstance().currentUser != nil
}

which is called as soon as the app starts to check if I have to show the LoginViewController or not.

My problem is that this call is always nil in first place, so I have to login every time my app starts.

also, as it's mentioned here, I'm configuring the scope like this:

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

Any idea pls?

Regards

2

There are 2 best solutions below

0
Matthew Reilly On

You need to sign in when the app starts.

GIDSignIn.sharedInstance().signInSilently()

0
SM Abu Taher Asif On

If user already logged in the try this API: GIDSignIn.sharedInstance.restorePreviousSignIn , whenever user opens the app I use below method to check auth status. This will return user if he already logged in otherwise return error.

func checkAuthStatus() -> Observable<GIDGoogleUser> {
        if GIDSignIn.sharedInstance.hasPreviousSignIn() {
            return Future<GIDGoogleUser, Error> { promise in
                GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
                    if let error = error {
                        promise(.failure(error))
                    } else if let user = user {
                        promise(.success(user))
                    } else {
                        let unknownError = CommonError.authError // use your error type
                        promise(.failure(unknownError))
                    }
                }
            }
            .eraseToAnyPublisher()
        } else {
            return Fail(error: CommonError.noPreviousAccountFound).eraseToAnyPublisher() // use your error type
        }
    }