2013-03-12 81 views
4

我對此很感興趣,所以請耐心等待。我正嘗試使用電子郵件和密碼的簡單登錄。我有工作與此:無法將用戶數據寫入Firebase

var authClient = new FirebaseAuthClient(fireRef, function(error, user) { 
      if (error) { 
      // an error occurred while attempting login 

       if(error.code === "INVALID_EMAIL"){ 
        $('#log_email_error').hide(); 
        $('#log_pass_error').hide(); 
        $('#log_email_error').html("Invalid email specified.").fadeIn(); 
        $('.login_button').removeClass('login_button_success').attr('value','Log in');   
       } 
       if (error.code === "INVALID_PASSWORD") { 
        $('#log_email_error').hide(); 
        $('#log_pass_error').hide(); 
        $('#log_pass_error').html("The specified password is incorrect..").fadeIn(); 
        $('.login_button').removeClass('login_button_success').attr('value','Log in'); 
       } 


      console.log(error); 
      } else if (user) { 
      // user authenticated with Firebase 
      hideLogin(); 
      $('.userInfo_cont').show(); 
      $('.userInfo').html('<div> '+ user.email + ' <span class="grey">| </span> </div>'); 
      $('.logout').on('click',function(){ 
       authClient.logout(); 
      }); 

      console.log('User ID: ' + user.id + ', Provider: ' + user.provider); 

      } else { 
      // user is logged out 
      $('.userInfo_cont').hide(); 
      showLogin(); 
      } 
    }); 

但是,當用戶註冊我要存儲在一個火力點/用戶區域 一些額外的信息,我可以在註冊這個做:

$('#submit_reg').on('click',function(){ 
      var firstName = $('#regFirstname').val(); 
      var lastName = $('#regLastname').val(); 
      var email = $('#regUsername').val(); 
      var password = $('#regPassword').val(); 

      authClient.createUser(email, password, function(error, user) { 
       if (!error) { 
        console.log('User Id: ' + user.id + ', Email: ' + user.email); 

         authClient.login('password', { 
              email: email, 
              password: password, 
              rememberMe: false 
             }); 


         hideLogin(); 


         userInfo = { 

          userId : user.id, 
          firstName : firstName, 
          lastName : lastName, 
          email : user.email 

         } 

         var url = USERS_LOCATION + "/" + user.id; 
         var userRef = new Firebase(url); 

         console.log(userInfo); 

         userRef.set(userInfo); 



        }else{ 
        //display error 

        alert(error); 

        } 
      }); 



     }); 

我的問題是,當我實現讀寫規則像文檔有:

{ 
    "rules": { 
    "users":{ 
    "$userid": { 
     ".read": "auth.id == $userid", 
     ".write": "auth.id == $userid" 
     } 
    } 
    } 
} 

我得到否認當用戶註冊權限。所以它註冊用戶就好了,但不會將其他數據寫入/ users/id區域。

任何幫助將不勝感激。

克雷格

+0

哪一行具體? userInfo對象內的user.id?這不會工作,因爲它不會寫入userInfo對象。或者你的意思是在authclient.createUser? – 2013-03-12 21:22:03

+0

這是如此令人沮喪,當我在權限中使用它:「.read」:「''+ auth.id == $ userid」, 「.write」:「''+ auth.id == $ userid」和還添加了以下代碼:var newid = user.id.toString(); \t \t \t \t \t \t var url = USERS_LOCATION +「/」+ newid;它工作了一秒鐘,然後停止工作。 – 2013-03-12 21:38:39

回答

5

在上面的代碼片段,你打電話login(),然後立即調用set()之後。這是有問題的,因爲login()方法是異步的,並且您幾乎總是保證在返回登錄嘗試方法之前調用set()方法,因爲login()是非阻塞的,並且對Firebase服務器進行網絡調用。

這意味着即使您使用正確的電子郵件和密碼呼叫login(),您仍然試圖在驗證過程完成之前設置數據。

我會建議將您的set()邏輯爲,當你確信用戶已經通過身份驗證,比如在你打電話時new FirebaseAuthClient()經過回調登錄的用戶將只執行和檢測塊。