0

I am creating an iPhone app in swift 4.0. I'm connecting over to Firestore, which currently works. The problem that I'm having is that a random character is document is created each time a user is authenticated. I want to change this so the email address is the registered document in the DB so the next time I login with the same credentials its uses the same document.

let databaseRef = Database.database().reference(fromURL: “*****************”)
let storage = Storage.storage().reference()
let db = Firestore.firestore()
var ref: DocumentReference? = nil
var handle: AuthStateDidChangeListenerHandle?
var checkBtnTitle:Bool = true


    override func viewDidLoad() {
    super.viewDidLoad()
        if Auth.auth().currentUser != nil {
            let currentUser = Auth.auth().currentUser
            btnLogOut.title = "LogOut"
            btnLogin.title = "Login"
            var ref: DocumentReference? = nil
            ref = db.collection("Solicitor").addDocument(data: [
                "userID": currentUser?.email! as Any,
                "Name": currentUser?.displayName as Any
            ]) { err in
                if let err = err {
                    print("Error adding document: \(err)")
                } else {
                    print("Document added with ID: \(ref!.documentID)")
                }
            }
        } else {
            btnLogOut.title = "Login"
            btnLogin.title = ""
        }

      // google signin
       GIDSignIn.sharedInstance()?.uiDelegate = self

Image of DB and random generated character

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Erinballs
  • 3
  • 5

2 Answers2

1

When you call addDocument() Firestore create a new document with an auto-generated ID. If you want to control the ID of the document, you should use the document() and setData() methods. For example:

ref = db.collection("Solicitor").document(currentUser?.email!).setDate(data: [
    "userID": currentUser?.email! as Any,
    "Name": currentUser?.displayName as Any
])

For more on this, see the Firebase documentation on adding documents.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you Frank, that makes sense.... I've tried to add this but im receiving the following error 'Value of optional type 'String?' must be unwrapped to a value of type 'String'' any thoughts? I tried looking at the support documentation too. – Erinballs Feb 07 '19 at 03:50
  • Did you search for that error message? I expect that the `currentUser?.email!` causes that, but am never completely sure about the correct Swift incantation. – Frank van Puffelen Feb 07 '19 at 03:51
  • I did mate, im still searching for the right syntax. been struggling with this one for a bit now. any other suggestions? – Erinballs Mar 12 '19 at 05:24
  • According to https://stackoverflow.com/questions/32892462/value-of-optional-type-string-not-unwrapped-did-you-mean-to-use-or it should be `document(String(currentUser?.email!))` – Frank van Puffelen Mar 12 '19 at 13:10
  • Hi Frank, thank you for your assistance on this one. I finally was able to get it to save... – Erinballs Mar 14 '19 at 05:21
0

thank you for your assistance on this one. I finally was able to get it to save... I didn't need the 'ref =' part in the DB.

 let currentUser = Auth.auth().currentUser
 let businessEmail = Auth.auth().currentUser?.email!
 db.collection("Solicitor").document(businessEmail!).setData([

On a side note, im still getting the auto-generated ID as well as the document ID (email address) of the user. Does anyone have any suggestions on how to stop this behaviour?

Erinballs
  • 3
  • 5