I am trying to call a function that when triggered saves Firebase data based on the type of User that is logged in. In my case I have a Facebook User that when function is called successfully saves that data to the database. I am also trying to implement a method where the same function is called but when a Google User also saves that data to Firebase. Again, I am able to successfully save with Facebook user but am having trouble with saving data when a Google User is logged in...help and Thanks!
var user: GIDGoogleUser!
// user taps confirm button to send item to be saved in Firebase
func confirmAddPlace(place: GMSPlace!, _ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!) {
// save place to Facebook User
if let accessToken = FBSDKAccessToken.current() {
guard let accessTokenString = accessToken.tokenString else { return }
let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)
FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if error != nil {
print("Something went wrong with our FB user: ", error ?? "")
return
} else {
// save place to GoogleUser
// getting an error in the if let idToken...
if let idToken = user.authentication.idToken {
guard let idTokenString = idToken.tokenString else { return }
let credentials = FIRGoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if let err = error {
print("failled to create place for user with Google", err)
return
}
guard let uid = user?.uid else { return }
print("Successfully logged into Firebase with Google", uid)
self.delegate?.finishLoggingIn()
let ref = FIRDatabase.database().reference()
let usersReference = ref.child("users").child(uid)
let Places: String = "Google_User_Places"
let values = ["name": user!.displayName, "email": user!.email, "Places": Places]
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!)
return
}
print("place has been saved to Firebase database")
})
})
}
}
// create place in Firebase
let ref = FIRDatabase.database().reference().child("places")
let childRef = ref.childByAutoId()
let toId = user?.uid
// let timeStamp: NSNumber = Int(NSDate().timeIntervalSinceNow)
let values = ["place": self.placeNameLabel.text!, "toId": toId!]
childRef.updateChildValues(values)
})
animateOut()
}
}
Aucun commentaire:
Enregistrer un commentaire