Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phone authentication using cloud firestore in ios swift?

firebase database image here

phone authentication login user image here

firebase database user phone number repeated with same user uid image here

In my app i am using phone authentication for login and storing user logged in user phone number and uid in database. when login with existing user phone number, it creates new document id with same uid and phone number you can look out the image 1 and 3.

Here is the code i used to login and if user exist mean it should check and navigate to home controller else sign user detail controller.

     @IBAction func loginbtn(_ sender: UIButton) {

    let defaults = UserDefaults.standard
    let credential: PhoneAuthCredential = PhoneAuthProvider.provider().credential(withVerificationID: defaults.string(forKey: "authVID")!,
                                                                                  verificationCode: otpText.text!)
    Auth.auth().signIn(with: credential)
    {
        (user, error) in
        if error != nil
        {
            print("error: \(String(describing: error?.localizedDescription))")
        }
        else if user != nil
        {


            print("Phone number: \(String(describing: user?.phoneNumber))")
            let userInfo = user?.providerData[0]
            print("Provider ID: \(String(describing: userInfo?.providerID))")

            let currentUser = Auth.auth().currentUser?.uid
            var ref: DocumentReference? = nil


            ref = self.db.collection("Users").addDocument(data: [
                                    "phoneNumber"   : user?.phoneNumber as Any,
                                    "UID"           : user?.uid as Any

                                ]) { err in
                                    if let err = err {

                                        print("Error adding document: \(err)")

                                    } else {

                                        print("Document added with ID: \(ref!.documentID)")

                                    }
                                }

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "HomeViewController")
            self.present(controller, animated: true, completion: nil)


        } else {




            }


        }

    }

can anyone help me out of this, i am nearly struggling with past one day!!!

like image 965
PvDev Avatar asked Mar 14 '18 03:03

PvDev


People also ask

How do I use firestore in Swift?

To add the to-do tasks, head over to the Firebase console and click Firestore Database. Click the Create database button and select the Start in test mode radio button. With this done, we can start adding to-do tasks into our database. Click the Start collection link and add “todos” as the collection ID.


2 Answers

You are calling the function "add" which creates a new document assigning an auto generated ID as mentioned here. Instead you'd need to be calling the function "set" using the user's uid as key, like follows:

db.collection("users").doc(uid).set... 
like image 73
Gerardo Avatar answered Oct 31 '22 05:10

Gerardo


You can also go with the firebase generated UID. You already have that line, but haven't used it:

let currentUser = Auth.auth().currentUser?.uid

So try to replace "UID": user?.uid as Any with "UID": currentUser as Any

like image 3
Moritz Gruber Avatar answered Oct 31 '22 04:10

Moritz Gruber