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!!!
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.
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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With