'Firebase phoneAuth - how to check first auth?
My app has phone authentication. The user also has a shopping cart. I want to create an empty cart for new users only, but for secondary users, it should remain as before.
private func checkCodeAndAuth(verificationID: String, code: String){
let credetional = PhoneAuthProvider.provider().credential(withVerificationID: verificationID, verificationCode: code)
Auth.auth().signIn(with: credetional) { [weak self] (result,error) in
guard let self = self else { return }
if let error = error {
self.showErrorAlert(error: error, withAction: nil)
} else {
guard let result = result else { return }
let userID = result.user.uid
let userPhone = result.user.phoneNumber
let userData = ["Контакт":"\(userPhone ?? "-")","shoppingCart":[:]] as [String : Any]
Constants.db.collection("users").document("\(userID)").setData(userData) { (error) in
if let error = error {
self.showErrorAlert(error: error, withAction: nil)
}
}
self.dismiss(animated: true, completion: nil)
}
}
}
Is it possible to use the credential to identify the first entry before entering?
*** When logging in using mail, if the user does not find it, then error 17011 "FIRAuthErrorCodeUserNotFound" comes up and I can easily process it and offer to register. How do you do this phone authentication trick without resorting to creating an array of all created ID's in the database?
Solution 1:[1]
For Phone Authentication, you cannot register the accounts thus making it impossible to check the existence of the user before login. Every time when you login with Phone Number, you need to go through the OTP Code.
So there are two ways you can implement this for the shopping cart.
- Check your database if the user already has the Shopping Cart.
Constants.db.collection("users").document("\(userID)").getDocument { (document, error) in
if let document = document, document.exists {
print("User already has Shopping Cart")
} else {
print("New user: Create a shopping cart")
}
}
- Check the account Creation Time with the result (work-around)
let userID = result.user.uid
let userPhone = result.user.phoneNumber
let creationDate = result.user.metadata.creationDate
// Maybe check whether `createDate` is within 60 seconds or something.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shri Hari L |
