web-dev-qa-db-ja.com

認証された後にGoogleからサインアウトする方法

したがって、私のアプリにはGoogleでサインインするオプションがあります。 Googleが提供するボタンをクリックすると、Webビューが開き、ユーザーが資格情報を入力します。アプリが自分の情報にアクセスできるようにした後、アプリはユーザーをサインインし、SignInViewControllerをTabBarControllerに変更します(TabBarControllerが適切に対話できるようになりました)。

ユーザーがサインアウトボタンを押すと、期待どおりにログイン画面が表示されます。しかし、奇妙なことに、ユーザーが再度googleボタンを押すと、ユーザーは自動的にサインインされ、それ以上の認証は一切行われず、アカウントを削除するオプションもありません。ユーザーを偶発的な盗難から保護するために、Googleアカウントの資格情報を消去する方法はありますか?

サインイン機能:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
    if let error = error {
        print(error.localizedDescription)
        return
    }
    let authentication = user.authentication
    let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken, accessToken: authentication.accessToken)
    FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
        // ...
        SignInViewController().signedIn(user)
    }
    // ...
}

ログアウト関数:

func signOutOverride() {
    do {
        try! FIRAuth.auth()!.signOut()
        CredentialState.sharedInstance.signedIn = false
        // Set the view to the login screen after signing out
        let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
        let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = loginVC
    } catch let signOutError as NSError {
        print ("Error signing out: \(signOutError)")
    }
}
26
About7Deaths

スイフト

GIDSignIn.sharedInstance().signOut()を試してください

目的-c

[[GIDSignIn sharedInstance] signOut];
48
Rahul Patel

はい、@ Rahulが言ったように、次のコードが正しい方法になるでしょう。

GIDSignIn.sharedInstance().signOut()

https://developers.google.com/identity/sign-in/ios/sign-in?ver=Swift#sign_out_the_user

12
Lazy

GoogleSignIn SDKで遊んだ後、以前の回答について少し詳しく説明したかった。

signOut()メソッドとdisconnect()メソッドを見て、違いが何であるか疑問に思っていました。

signOut()は同期呼び出しです。

_// 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()を使用すると、ユーザーはアプリへのアクセスを取り消すことができますさらにログアウトします。これは、再度ログインすることを選択した場合に、アプリへのアクセス許可を再付与する必要があることを意味します。

Googleのデベロッパードキュメント によると、ユーザーがアプリからdisconnectを選択した場合、アプリに保存されているユーザーのGoogleデータを削除する必要があります。

また、disconnect()は非同期です。切断呼び出しの結果は、GIDSignInDelegate.sign(_:didDisconnectWith:withError:)メソッドに返されます。

_// 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.
}
_
1
orangemako
  public func logOut(on:UIViewController){

    let firebaseAuth = Auth.auth()
    do {
        try  firebaseAuth.signOut()
            GIDSignIn.sharedInstance().signOut()
            GIDSignIn.sharedInstance().disconnect()

        if let url = NSURL(string:  "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://google.com"){
            UIApplication.shared.open(url as URL, options: [:]) { (true) in
                let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
                appDel.window?.rootViewController = LoginViewController()
            }
        }
    } catch let signOutError as NSError {
        Help.shared.Snack(messageString: "Error signing out: \(signOutError)" 
)
        print ("Error signing out: %@", signOutError)
    }
}
1