Подскажите, почему не добавляется пользователь в базу? вход пользователя из уже созданной учетной записи производиться
правило в firebase
{
"rules": {
".read": true,
".write": true
}
}
код программы
//
// AuthViewController.swift
// motoRider
//
// Created by Николай on 13.10.2020.
// Copyright © 2020 Nikolay Fokin. All rights reserved.
//
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
class AuthViewController: UIViewController {
var signup:Bool = true{
willSet{
if newValue{
titleLabel.text = "Регистрация"
nameField.isHidden = false
enterButton.setTitle("Войти", for: .normal)
}else{
titleLabel.text = "Вход"
nameField.isHidden = true
enterButton.setTitle("Регистрация", for: .normal)
}
}
}
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var enterButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
nameField.delegate = self
emailField.delegate = self
passwordField.delegate = self
}
@IBAction func switchLogin(_ sender: UIButton) {
signup = !signup
}
func showAlert(){
let alert = UIAlertController(title: "Ошибка", message: "Заполните все поля!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
}
extension AuthViewController:UITextFieldDelegate{
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let name = nameField.text!
let email = emailField.text!
let password = passwordField.text!
if (signup){
if (!name.isEmpty && !email.isEmpty && !password.isEmpty){
Auth.auth().createUser(withEmail: email, password: password) { result, error in
if error == nil{
if let result = result{
print(result.user.uid)
let ref = Database.database().reference().child("users")
ref.child(result.user.uid).updateChildValues(["name" : name,"email" : email])
self.dismiss(animated: true, completion: nil)
self.navigationController?.popViewController(animated: true)
}
}
}
}else{
self.showAlert()
print("2")
}
}else{
if (!email.isEmpty && !password.isEmpty){
Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
if error == nil{
self.dismiss(animated: true, completion: nil)
}
}
} else{
self.showAlert()
}
}
return true
}
}