13022

Firebase google auth (swift)

Question:

Following the firebase docs i added the authentication with google account and this is part of the code that i have in my app delegate

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) { if let error = error { print("Failed to log into Google: ", error) return } print("Successfully logged into Google", user) guard let idToken = user.authentication.idToken else { return } guard let accessToken = user.authentication.accessToken else { return } let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken) Auth.auth().signIn(with: credentials, completion: { (user, error) in if let err = error { print("Failed to create a Firebase User with Google account: ", err) return } guard let uid = user?.uid else { return } print("Successfully logged into Firebase with Google", uid) }) } func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) { // Perform any operations when the user disconnects from app here. // ... }

but the google button

fileprivate func setupGoogleButtons() { googleButton.frame = CGRect(x: 189, y: 532, width: 118, height: 41) view.addSubview(googleButton) GIDSignIn.sharedInstance().uiDelegate = self }

is obviously in a viewController, what i would like to do is an automatically self.performSegue(withIdentifier: "goToHome1", sender: self) as soon as the user logs in with his google account, because at the moment after login the user always finds on the same VC. How can i do this?

UPDATE

I solve my problem following this question <a href="https://stackoverflow.com/questions/42628879/google-and-facebook-firebase-auth-using-swift-3" rel="nofollow">Google and Facebook Firebase Auth using swift 3</a>

Answer1:

If there is no error then the user signed in successfully so you should segue. The button itself just calls the sign in function. Depending on whether the sign in fails or succeeds you alert or segue.

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) { if let error = error { print("Failed to log into Google: ", error) return } print("Successfully logged into Google", user) guard let idToken = user.authentication.idToken else { return } guard let accessToken = user.authentication.accessToken else { return } let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken) Auth.auth().signIn(with: credentials, completion: { (user, error) in if let err = error { print("Failed to create a Firebase User with Google account: ", err) return } guard let uid = user?.uid else { return } print("Successfully logged into Firebase with Google", uid) // segue here DispatchQueue.main.async { self.performSegue(withIdentifier: "goToHome1", sender: self) } }) }

Answer2:

The Auth class has a function addAuthStateDidChangeListener:, check it <a href="https://firebase.google.com/docs/reference/ios/firebaseauth/api/reference/Classes/FIRAuth?authuser=0#/c:objc(cs)FIRAuth(im)addAuthStateDidChangeListener:" rel="nofollow">here</a>.

It will trigger any time the user changes, particularly when the user logs in, the callback will return a non-nil user. That's the time you want to perform the segue.

Recommend

  • Posting multiple photos in a single tweet
  • Firebase Facebook login with redirect returning null email
  • Card payment Using PayPal android
  • promise.all not access in rejection function
  • Facebook SDK Android: OnSuccess called but OnCancel not called
  • Azure Active Directory Graph API - access token for signed in user
  • How to represent a form-data request using Akka HTTP?
  • Facebook token Android SDK refresh criteria
  • How to get new token after calling reconnect api of Intuit? [duplicate]
  • Multiple emails selected messageMetadata.accessToken
  • Sound Cloud returning 401 error
  • I want to find populate OR search, mongoose
  • How to authorize SignalR Core Hub method with JWT
  • React Native - async/await does not work
  • Batch requests in Node.js
  • Does this code create a circular memory reference in Ruby?
  • Is it possible to revoke AWS Cognito IdToken?
  • POSTMAN - Schema validation is passed even for bad response data
  • No permission to access invitable friends
  • Hiding contentful Space id and access token, client side javascript file
  • how to perform file upload using Volley in android?
  • Firebase Authentication Javascript: setCookie for pending Credential for redirect
  • Windows service showing a GUI when no user is logged in
  • Reading a file located in a jar as java.io.File object
  • Office365 authentication without login redirection
  • OAuth2 flow for mobile app
  • Zend Framework 2, Module Redirect
  • How to authenticate user name and password against Active Directory Federation Services (ADFS)?
  • New-PSSession in an Azure-runbook (ARM)
  • Django: DRY principle and UserPassesTestMixin
  • SmtpClient sends email to junk
  • Can't access Tomcat 7 Manager app when running from Eclipse
  • ASP.NET windows authentication should always ask for credentials
  • Django return user model id with L
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • sending mail using smtp is too slow