I've setup an app where users login through the phone number auth on Firebase and I'm trying to setup logic so a user can re-login via their phone number if they already setup an account. I'm struggling to match the inputted phone number vs a document from Firestore (I have a folder titled phoneNumbers where I capture phone setup specifically for this purpose).
FYI - the phone auth logic has been working, just need help on matching a phone number inputted to a phone number already in my database. thanks!
{
print ("there is a number")
let countryCodeText: String = " 1-"
let phoneNumSetup = phonenumberTextField.text ?? ""
let phoneNumber = countryCodeText phoneNumSetup
print("phone number given = \(phoneNumber)")
UserDefaults.standard.set(phoneNumber, forKey: "phoneNumber")
let db = Firestore.firestore()
db.collection(Constants.ProfileData.phoneNumbers).whereField(Constants.ProfileData.phone, isEqualTo: phoneNumber).getDocuments { (snapshot, error) in
guard error == nil, !(snapshot?.isEmpty ?? false) else { return }
let document = snapshot?.documents.first
if let userData = document?.data() {
let pN = userData[Constants.ProfileData.phone] as? String
let pNcount = pN?.count
if pNcount! > 0 {
print("phone number existed before")
PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil) { (verificationID, error) in
if let error = error {
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { _ in
alert.dismiss(animated: false)
}))
self.present(alert, animated: true, completion: nil)
print(error.localizedDescription)
return
}
self.performSegue(withIdentifier: "verificationSegue", sender: nil)
self.currentVerificationId = verificationID!
UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
}
}
else {
print("phone number is new")
print ("not in firebase, they need to signup!")
self.displayAlertToWelcome2()
}
}
}
```[![Firestore Setup][1]][1][![phone input view][2]][2]
[1]: https://i.stack.imgur.com/qe3Wj.jpg
[2]: https://i.stack.imgur.com/GHrmu.jpg
CodePudding user response:
Just figured it out, I'm trying to query the Firestore database without being logged in! My database is setup to only allow queries if an authorized user. I'll have to re-think my setup on this, won't be smart to allow un-auth pings to the database.
CodePudding user response:
func yourFunc() {
guard let phoneNumberInput = phonenumberTextField.text else {
return
}
let phoneNumber = " 1-" phoneNumberInput
UserDefaults.standard.set(phoneNumber, forKey: "myApp.phoneNumber") // good practice to prefix user defaults
Firestore.firestore().collection(Constants.ProfileData.phoneNumbers)
.whereField(Constants.ProfileData.phone, isEqualTo: phoneNumber)
.getDocuments { (snapshot, error) in
guard let doc = snapshot?.documents.first else {
if let error = error {
print(error)
}
return
}
if let pN = doc.get(Constants.ProfileData.phone) as? String,
pN.count > 0 {
print("phone number existed before")
} else {
print("phone number is new")
print ("not in firebase, they need to signup!")
self.displayAlertToWelcome2()
}
}
}