1

I use angular2 + firebase. There's something wrong in my code. When user is logged in, functions createUser and removeUser doesn't work without errors. Without logging in it works properly. I got parts of code from firebase tutorial. The same thing when i'm trying to use google auth service.

app.controller("AuthCtrl", ["$scope", "$firebaseAuth",
  function($scope, $firebaseAuth) {
    var ref = new Firebase("...");
    var Auth = $firebaseAuth(ref)

    $scope.createUser = function() {
      $scope.message = null;
      $scope.error = null;

      Auth.$createUser({
        email: $scope.email,
        password: $scope.password
      }).then(function(userData) {
        $scope.message = "User created with uid: " + userData.uid;
        $scope.email = "";
        $scope.password = "";
      }).catch(function(error) {
        $scope.error = error;
      });
    };

    $scope.removeUser = function() {
      $scope.message = null;
      $scope.error = null;

      Auth.$removeUser({
        email: $scope.email,
        password: $scope.password
      }).then(function() {
        $scope.message = "User removed";
        $scope.email = "";
        $scope.password = "";
      }).catch(function(error) {
        $scope.error = error;
      });
    };

    $scope.login = function(){
      Auth.$authWithPassword({
        email: $scope.emaillog,
        password: $scope.passwordlog
      }).then(function(authData) {
        $scope.authData = Auth.$getAuth();
        console.log("Logged in as:", authData.uid);
      }).catch(function(error) {
        console.error("Authentication failed:", error);
      });
    }
  }
]);
<div ng-app="App">
    <div ng-controller="AuthCtrl">
      <div>
        <h2>Login</h2>
        <form>
          Email: <input ng-model="emaillog" type="text" placeholder="email">
          Password: <input ng-model="passwordlog" type="text" placeholder="password">
          <br><br>
          <button ng-click="login()">Login</button>
        </form>
      </div>
      <div ng-if = "authData">
        <h2>Managing users</h2>
        <form>
          Email: <input ng-model="email" type="text" placeholder="email">
          Password: <input ng-model="password" type="text" placeholder="password">
          <br><br>
          <button ng-click="createUser()">Create User</button>
          <button ng-click="removeUser()">Remove User</button>
          <p ng-if="message">Message: <strong>{{ message }}</strong></p>
          <p ng-if="error">Error: <strong>{{ error }}</strong></p>
        </form>
      </div>
    </div>
</div>

I've found the answer here what is the difference between ng-if and ng-show/ng-hide

Community
  • 1
  • 1
Trom
  • 65
  • 8

0 Answers0