2016-11-15 68 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)。

你需要做的是創建另一個名爲「用戶名」的孩子。讓任何人都可以閱讀,而無需驗證。檢查用戶名是否被採用,如果沒有,請爲新用戶聲明。然後,進行身份驗證調用並在「用戶」子項中創建用戶。

0

對Firebase一無所知,「知道它在做什麼」的方式似乎在提醒或打印。所以如果沒有這樣做,我會發現至少有兩個執行路徑不會以警告或打印結束。

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äesepp您在firebase中存儲的用戶名是否與firebase用戶名相同?它通常會是相同的,但你可能有不同的用例。即當你創建一個用戶時,它是用他們的電子郵件地址完成的,然後通過你的代碼存儲在你的/用戶中。 – Jay

+0

我還在用戶節點下注冊了「username」:「usernameField.text」到用戶節點下的firebase數據庫中,並且還註冊了其他一些細節。那麼什麼是更好的規則? Firebase用戶名是什麼意思? –