1
votes

I'm new to firebase, singing with gmail on main view. after user singing, I'm saving the userinfo based on documentId, if the user already existed(already logged with gmail) person no need to save the information just update information of existed. So I tried some of the code, but I will not achieved what I'm expecting.

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {

        if let error = error{
            print(error.localizedDescription)
            return
        }
        guard let authentication = user.authentication else { return }

        let credential = GoogleAuthProvider.credential(withIDToken: (authentication.idToken)!, accessToken: (authentication.accessToken)!)
        Auth.auth().signIn(with: credential, completion: { (user, error) in
            if let error = error {
                print("Login erro  \(error.localizedDescription)")
                return
            }

Here I check all document id in firestore

            self.db.collection("pruser").getDocuments() { (querySnapshot, err) in
                       if let err = err {
                           print("Error getting documents: \(err)")
                       } else {
                           for document in querySnapshot!.documents {
//                               print("\(document.documentID) => \(document.data())")
                            print("\(document.documentID)")

Here I have to check with existed Document id's and current user Document id. If already existed update info, if not set the values on Document Id but when try with this code

            self.ref = self.db.collection("pruser").document()
            let doc = self.ref?.documentID
            print("printdoucment id ",doc!)
                        }
                       }
                   }
}

userid: vnRsxNjJePfwRcAWOLy1iByhV352
Doucment id: PKKLapD9xE9kNNvntI8X

userId: vnRsxNjJePfwRcAWOLy1iByhV352
Doucment id: pxjiVXEgbZgFke3Mijk5

when I logged with existed email also documentid coming different, every login time DocumentId is coming different how achieve this issue? enter image description here

2
Have you thought about using the Realtime Database instead? I found it much more beginner friendly and clearer to use. - Hubert Rzeminski
@HubertRzeminski thank u for reply my requirement with cloud store. - naga

2 Answers

1
votes

document() with no parameters generates a new document ID every time you call it. You have to pass it a parameter to specify the document ID, if that's what you want.

self.db.collection("pruser").document(user.uid)
0
votes

Try adding this to viewDidLoad():

GIDSignIn.sharedInstance()?.presentingViewController    = self
GIDSignIn.sharedInstance()?.delegate = self

And replace the 7-11 lines with:

Auth.auth().signIn(with: credentials) {
            (authResult, error) in
            if let error = error {
                print(error.localizedDescription)
            }else{
                print("Log in successful")

This should sign in the user.

As for DocumentID, I don't think that in your code you've linked each user to each DocumentID. You need to change the or read the DocumentID based on the userid.