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 to access their information the app then signs the user in and changes the SignInViewController to the TabBarController (where they can now interact accordingly).
When the user presses a Signout button they are directed to the login screen as one would expect. But the odd thing is, if the user presses the google button again they are automatically signed in with no further authentication at all and no option to remove their account. Is their a way to clear the google account credentials as to protect the users from accidental theft?
Sign in function:
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)
}
// ...
}
Sign out function:
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)")
}
}
Swift
try GIDSignIn.sharedInstance().signOut()
objective - c
[[GIDSignIn sharedInstance] signOut];
Yes, like @Rahul said following code would be the right way of going about it.
GIDSignIn.sharedInstance().signOut()
https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift#sign_out_the_user
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.
}
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)
}
}
Try my code..
@IBAction func onClickSignOut(_ sender: UIButton) {
GIDSignIn.sharedInstance()?.signOut()
// if GIDSignIn.sharedInstance()?.currentUser == nil {//Logged out
// self.navigationController?.popToRootViewController(animated: true)
// } else {//Not logged out
// //Your code here
// }
/* check for user's token */
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
//hasAuthInKeychain() : Checks whether the user has either currently signed in or has previous authentication saved in keychain.
//Not logged out
//Write your code here
//......
} else {
//Logged out
//Write logged out code here
//EX:
self.navigationController?.popToRootViewController(animated: true)
}
}