2014-10-28 42 views
0

我想在我的Firebase中創建一個新用戶。這是我正在使用的代碼:註冊時的Firebase設置用戶數據

function createUser(email, password, username) { 
     ref.createUser({ 
      email: email, 
      password: password 
     }, function(error) { 
      if (error === null) { 
       $activityIndicator.stopAnimating(); 
       $scope.padding_error = null; 
       $scope.error = null; 
       logUserIn(email, password); 
      } else { 
       $activityIndicator.stopAnimating(); 
       console.log(error.code); 
       switch (error.code) { 
        case "INVALID_EMAIL": 
         $scope.padding_error = "10"; 
         $scope.error = "Falsche E-Mail Adresse oder falsches Passwort"; 
         $scope.$apply(); 
        case "INVALID_PASSWORD": 
         $scope.padding_error = "10"; 
         $scope.error = "Falsche E-Mail Adresse oder falsches Passwort"; 
         $scope.$apply(); 
        case "EMAIL_TAKEN": 
         $scope.padding_error = "10"; 
         $scope.error = "Diese E-Mail Adresse ist schon registriert"; 
         $scope.$apply(); 
       } 
      } 
     }); 
    } 

我想除了電子郵件和密碼之外還在我的firebase中存儲用戶名。用戶註冊後如何立即執行此操作?

+0

一個良好的開端將是對認證引導,其包括[存儲用戶數據(https://www.firebase.com/docs/web/guide/user-auth的簡短實例。 HTML#部儲存)。 – Kato 2014-10-30 05:02:53

回答

1

如果註冊成功,您可以簡單地將電子郵件和密碼變量推送到firebase。見下面的代碼。

function createUser(email, password, username) { 
     ref.createUser({ 
      email: email, 
      password: password 
     }, function(error) { 
      if (error === null) { 
       ... Registration successful 
       $activityIndicator.stopAnimating(); 
       $scope.padding_error = null; 
       $scope.error = null; 
       ##NEWCODE HERE## 
       emailRef = new Firebase("<YOURFIREBASEURL>/accounts/"+username+"/email") 
       passRef = new Firebase("<YOURFIREBASEURL>/accounts/"+username+"/password") 
       emailRef.set(email) 
       passRef.set(password) 
       logUserIn(email, password); 
      } else { 
       ... Something went wrong at registration 
       } 
      } 
     }); 
    } 
+0

但最有可能的是,您希望在登錄後創建用戶記錄。安全規則可能會存在(如果它們不是,它們應該是)以防止匿名寫入。 [AngularFire-seed](https://github.com/firebase/angularFire-seed)有這個策略的一些很好的例子。 – Kato 2014-10-30 05:05:34