2017-02-02 30 views
0

私はこれを動作させることはできません。データベース部分が機能し、データベース内の意図どおりのユーザー情報を取得していますが、Firebase Authでユーザーを作成していません。次のコードでは、「can not register」という文字が表示されます。Firebase認証ユーザーの作成

私が間違っていることを教えてもらえますか?

import UIKit 
import Firebase 
import FirebaseAuth 

class AddUserTableViewController: UITableViewController, UITextFieldDelegate { 


@IBOutlet weak var firstNameTextField: UITextField! 
@IBOutlet weak var emailTextField: UITextField! 


@IBAction func saveUserButton(_ sender: Any) { 
    let ref: FIRDatabaseReference! 
    ref = FIRDatabase.database().reference() 
    FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: "pass", completion: { (user, error) in 
     if error != nil { 
      print ("Can't Register") 
     } 
     else { 
      print ("I don't know what this means") 
     } 
    }) 

    ref?.child("Users").childByAutoId().setValue(["First Name": self.firstNameTextField.text, "Email": self.emailTextField.text]) 

} 
+0

「error」の値を印刷すると、おそらくユーザーが登録できない理由が伝えられます。 –

答えて

0

Firebaseを含めるだけで、FirebaseAuthも各ページに含める必要はありません。

FireBaseログイン用のコードです。私は数週間前にYoutubeチュートリアルでこれを行いました。

import UIKit 
import Firebase 

class LoginController: UIViewController { 
    @IBOutlet weak var menuButton:UIBarButtonItem! 
    @IBOutlet weak var signinSelector: UISegmentedControl! 
    @IBOutlet weak var signinLabel: UILabel! 
    @IBOutlet weak var emailTextField: UITextField! 
    @IBOutlet weak var passwordTextField: UITextField! 
    @IBOutlet weak var signinButton: UIButton! 

    var isSignIn:Bool = true 


    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    @IBAction func signinSelectorChanged(_ sender: UISegmentedControl) { 
     //Flip the boolean true to false 
     isSignIn = !isSignIn 

     //Check the boolean and set the buttons and labels 
     if isSignIn { 
       signinLabel.text = "Sign In" 
       signinButton.setTitle("Sign In", for: .normal) 
     } 
     else { 
       signinLabel.text = "Register" 
       signinButton.setTitle("Register", for: .normal) 
     } 
    } 

    @IBAction func signinButtonTapped(_ sender: UIButton) { 

     //Do some form validation on email and password 
     if let email = emailTextField.text, let pass = passwordTextField.text 
     { 

     //Check if it's signed or register 

     if isSignIn { 
      //Sign in the user with Firebase 
      Auth.auth().signIn(withEmail: email, password: pass, completion: { (user, error) in 

       //Check that user isn't nil 
       if let u = user { 
         //User is found, goto home screen 
        self.performSegue(withIdentifier: "goToHome", sender: self) 
       } 
       else{ 
        //Error: Check error and show message 
       } 
      }) 
     } 

     else { 
      //Register the user with Firebase 

      Auth.auth().createUser(withEmail: email, password: pass, completion: { (user, error) in 

       //Check that user isn't NIL 
       if let u = user { 
        //User is found, goto home screen 
        self.performSegue(withIdentifier: "goToHome", sender: self) 
       } 
       else { 
        //Check error and show message 
       } 

      }) 



      } 
    } 


} 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     //Dismiss the keyboard when the view is tapped on 
     emailTextField.resignFirstResponder() 
     passwordTextField.resignFirstResponder() 
    } 


}