2

私は新しいFirebaseに慣れていません。新しいユーザーを作成するにはどうすればよいですか?以下のコードは私がサインアップし、auth新しいユーザーです。 Firebase Databaseの「得意先」でこの新しいユーザーを作成する必要がある場合、追加するコードは何ですか?ありがとう! Firebase iOS - Save DataFirebaseユーザーの作成方法は?

答えて

3
FIRAuth.auth()?.createUserWithEmail(email, password: password, completion: { (user, err) in 

if err != nil { 

    self.showAlert("Can't Register", msg: "Please enter email and password") 

    } else { 

     NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: "uid") 

    FIRDatabase.database().reference().child("Customers").setValue([user!.uid : "true"]) 

    //Or if you want to save the users Details too:- 
    /* 

     FIRDatabase.database().reference().child("Customers").setValue([user!.uid : ["email" : email,"password" : password]]) 

    */ 
    self.performSegueWithIdentifier("toSecondVC", sender: self) 
    } 
}) 

はまた、私はこれを読んで示唆する

var values = [Any : Any] // This is a dictionary where you can store user details 
values["userUID"] = // user's uid 
values["usersName"] = // user's name 
// etc. 

let customerRef = databaseReference.child("Customers") // You could also add a child with the user's UID in order to identify each user 

customerRef.updateChildValues(values, withCompletionBlock: { (error, ref) in 

    if error != nil { 

     // display error.code and error.localizedDescription to user if needed 

    } else if ref != [] { 

     // Success! 
     // If needed, save the user to NSUserDefaults and perfrom a segue to another ViewController 

    } else { 

     // There was an error, but not sure what happened. Let the user know how they can fix the problem (maybe add the details to the database later) 

    } 
}) 
+0

こんにちはドラヴィダを、ありがとうございました!うまくいきます!ハッピー・フライデー! – developermike

0

答えは上記正しいですが、私はそれをこのようにやってお勧めします:

FIRAuth.auth()?.createUserWithEmail(email, password: password, completion: { (user, err) in 

    if err != nil { 

     self.showAlert("Can't Register", msg: "Please enter email and password") 

     } else { 

      NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: "uid") 

      FIRAuth.auth()?.signInWithEmail(email, password: password, completion: { (user, error) in 

     }) 

     self.performSegueWithIdentifier("toSecondVC", sender: self) 
    } 

    }) 
関連する問題