How to Sign Out of Google After Being Authenticated

后端 未结 6 970
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 23:54

So my app has the option to sign in with Google. Upon clicking the button that Google provides, a web view opens and has the user input their credentials. After allowing the app

6条回答
  •  误落风尘
    2021-02-07 00:03

    Wanted to elaborate a bit on the previous answers after playing with the GoogleSignIn SDK.

    I saw the signOut() and disconnect() methods and was wondering what the differences were.

    signOut() is a synchronous call:

    // Immediately sets GIDSignIn.sharedInstance()?.currentUser to nil. 
    // For example, if the user is already signed in:
    
    print(GIDSignIn.sharedInstance()?.currentUser != nil) // true - signed in
    GIDSignIn.sharedInstance()?.signOut()
    print(GIDSignIn.sharedInstance()?.currentUser != nil) // false - signed out
    

    disconnect() allows a user to revoke access to the app in addition to logging out. I assume this means they'll need to re-grant any permissions to your app if they choose to log in again.

    According to Google's Developer Docs if a user chooses to disconnect from your app, then you'll need to remove any of the user's Google data that has been stored in your app.

    Also, disconnect() is asynchronous. The result of the disconnect call will be returned to the GIDSignInDelegate.sign(_:didDisconnectWith:withError:) method.

    // Also sets GIDSignIn.sharedInstance()?.currentUser to nil. 
    // Asynchronous call. If for example the user was already signed in:
    
    print(GIDSignIn.sharedInstance()?.currentUser != nil) // true - signed in
    GIDSignIn.sharedInstance()?.disconnect()
    print(GIDSignIn.sharedInstance()?.currentUser != nil) // true - still signed in
    
    // MARK: - GIDSignInDelegate
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        print(GIDSignIn.sharedInstance()?.currentUser != nil) // false - signed out
    
        // Remove any data saved to your app obtained from Google's APIs for this user.
    }
    

提交回复
热议问题