would like to prevent the account creation when an email address is already registered in the database, so here is my code :
final class DatabaseManager {
static let shared = DatabaseManager()
private let database = Database.database().reference()
}
// MARK: - Account Management
extension DatabaseManager {
public func userExists(with email: String,
completion: @escaping ((Bool) -> Void)) {
var safeEmail = email.replacingOccurrences(of: ".", with: "-")
safeEmail = safeEmail.replacingOccurrences(of: "@", with: "-")
database.child(safeEmail).observeSingleEvent(of: .value, with: { snapshot in
guard snapshot.value as? String != nil else {
completion(false)
return
}
completion(true)
})
}
And then I call it like that :
//FireBase Log In
DatabaseManager.shared.userExists(with: email, completion: { [weak self] exists in
guard let strongSelf = self else {
return
}
guard !exists else {
strongSelf.alertInvalidEmail()
print("already exists")
return
}
print("doesnt exist")
self?.presenter(vc, animated: false, pushing: true, completion: nil) // success, present next vc
})
}
Here email
is the email address that needs to be checked.
But the thing is that it always prints "doesnt exist" even if it does, no matter what I try. Is it because the account creation happens on the second view controller, so not at the same time as the email address check? Im really stuck thanks