2016-11-15 8 views
-1

ユーザの登録中にユーザ名の一意性をチェックしようとしています。私はそこにたくさんの質問があることを知っていますが、私はすべての問題を抱えていますが、何もしないということは、ユーザー名が存在し、ユーザー名に登録されていないことを印刷しません。SwiftのFirebaseユーザ名一意性

私は間違っていますか?私は答えを見つけることができないようだが、間違っている可能性があるのはネスティングの場合だけである。

これは構造体である:

AppName: 
    users 
    1v2mRJTvrBQ7dMQohUU3rnn7ypI3: //UID 
    username: John 

そして、それはコードです:

func registerUser(){ 
     guard let username = usernameField.text, let email = emailField.text, let password = passwordField.text else{ 
      print("Successfully registered") 
      return 
     } 
     if connectedToNetwork() == true{ 
      if passwordField.text == confirmPasswordField.text{ 
       let usersRef = FIRDatabase.database().reference().child("users") 
       usersRef.queryOrdered(byChild: "username").queryEqual(toValue: "\(username)") 
       .observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in 
        if snapshot.value is NSNull{ 

       AuthProvider.Instance.register(withEmail: email, password: password, username: username, loginHandler: { (errMessage) in 
        if errMessage != nil{ 

        }else{ 
         let user = FIRAuth.auth()?.currentUser 
         guard let uid = user?.uid else{ 
          return 
         } 
         user?.sendEmailVerification() { error in 
          if let error = error { 
           print(error.localizedDescription) 
          } else { 
           print("Email has been sent to you!") 
          } 
         } 
         //User logged in and redirect 

        } 
       }) 
        }else{ 
        print("Username already exists")// It doesn't execute this 
        } 
       }) 
      }else{ 
       Util.errorAlert(message: "Passwords do not match!") 
      } 
     }else{ 
      Util.errorAlert(message: "The internet connection appears to be offline.") 
     } 
    } 

私もさまざまなセキュリティルールを試みたが、それらはすべての違いを作ることが、私は使用しているものではないはずです。

{ 
"rules": { 
".read": "auth != null", 
".write": "auth != null", 
"Snuses": { 
".indexOn": "Brand", 
    "$username": { 
     ".validate": "!root.child(users').hasChild($username)" 
    } 
} 
} 
} 

私が間違っていることは何ですか?

+0

と思われます。どうした?あなたはどんなエラーを出していますか? – GJZ

+0

何もない、エラーはない、何もない。 –

+0

すべての開閉括弧を確認しましたか?コードの一部が実行されていない可能性があります。 – GJZ

答えて

1

はあなたの問題はあなたのセキュリティルールです。ユーザーが認証される前にクエリを実行しています。この問合せは、データベースに問合せを行うためにユーザーを認証する必要があるとルールに記述されているため失敗します(auth!= null)。

"usernames"という別の子を作成するだけです。これを認証なしで誰かが読めるようにする。ユーザー名が使用されているかどうかを確認し、そうでない場合は新しいユーザーのために要求します。次に、あなたの認証呼び出しを行い、 "ユーザー"の子でユーザーを作成します。

0

Firebaseについて知りませんが、「何かしていることを知る」というあなたのやり方は、警告または印刷されているようです。だから、それをやっていないなら、私は警告や印刷で終わらない少なくとも2つの実行パスを見ています。

if errMessage != nil{

guard let uid = user?.uid else{

また、これはelseを逃すことができるが、私はS.O.に答えるに十分な努力のレベルでそれを見つけることができません質問:

if snapshot.value is NSNull{

純粋に理論的には、あなたのUtil.errorAlertはあまりにも壊れていてもよいです。

0

Firebaseは重複したユーザ(名前)を許可しないため、ユーザを作成しようとするとエラーが表示されます。

FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in 
    // ... 
} 

返すことができた4個のエラーがあります:

FIRAuthErrorCodeInvalidEmail 
    Indicates the email address is malformed. 

FIRAuthErrorCodeEmailAlreadyInUse (this is the one to address you question) 
    Indicates the email used to attempt sign up already exists. Call 
    fetchProvidersForEmail to check which sign-in mechanisms such 
    user used, and prompt the user to sign in with one of those. 

FIRAuthErrorCodeOperationNotAllowed 
    Indicates that email and password accounts are not enabled. Enable them in the 
    Authentication section of the Firebase console. 

FIRAuthErrorCodeWeakPassword 
    Indicates an attempt to set a password that is considered too weak. The 
    NSLocalizedFailureReasonErrorKey field in the NSError.userInfo 
    dictionary object will contain more detailed explanation that can 
    be shown to the user. 
+0

ユーザー名..電子メールの問題は機能していますが、Firebaseデータベースに保存されているユーザ名も確認する必要があります。 –

+0

@TarvoMäeseppfirebaseに保存しているユーザ名はfirebaseユーザ名と同じですか?通常は同じですが、異なるユースケースがあるかもしれません。つまり、ユーザーを作成すると、そのユーザーのメールアドレスが入力された後、コードを介して/ユーザーに保存されます。 – Jay

+0

私は "username": "usernameField.text"をfirebaseデータベースにユーザノードの下にいくつかの他の詳細とともに登録しています。その後、より良いルールは何ですか? firebaseのユーザ名はどういう意味ですか? –