AngularJS Factory和Parse.com用户身份验证

时间:2015-01-31 16:15:36

标签: angularjs parse-platform

我正在使用Parse.com使用AngularJS,因为它是后端,当我尝试使用Parse进行身份验证和验证时,我遇到了问题。我很想做这两个功能,但我的观点不会更新。

app.factory('hsUser', ['$http', function($http){

  var dataObject = null;

  return {
    get: function()
    {
      var tmpData = Parse.User.current();
      console.log(tmpData);

      if(tmpData && dataObject == null)
      {
        dataObject = {};
        keys = Object.keys(tmpData);
        keys.forEach(function(value)
        {
          dataObject[value] = tmpData[value];
        });
        console.log(dataObject);
      }
      return dataObject;
    },

    authenticate: function(username, password)
    {
      console.log('auth');
      Parse.User.logIn(username, password, {
        success: function(userData) {
          dataObject = {};
          keys = Object.keys(userData);
          keys.forEach(function(value)
          {
            dataObject[value] = userData[value];
          });
        },
        error: function(userData, errorData) {
          console.log(errorData);
        }
      });
    },

    deauthenticate: function()
    {
      Parse.User.logOut();
      dataObject = Parse.User.current();
    },

    doRegister: function(registration)
    {
      console.log(registration);
      return {
        success: true
      }
    }
  }
}]);

这是我的控制器。

app.controller('HeaderController', ['hsConfig','hsUser', function( hsConfig, hsUser)
  {
    this.config = hsConfig.get();
    this.user = hsUser.get();
  }]);

app.controller('AuthController', ['hsUser', function( hsUser)
{
  this.user = hsUser.get();

  this.authUser = {
    emailAddress: "testinguser",
    password: "testinguser",
    remember: false,
  }

  this.authenticate = function()
  {
    hsUser.authenticate(this.authUser.emailAddress, this.authUser.password);
  }

  this.deauthenticate = function()
  {
    this.user = hsUser.deauthenticate();
    console.log('Deauthenticated')
  }

}]);

以下是创建导航栏的指令的HTML

<div class="header" ng-controller="HeaderController as headCtrl">
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="/">{{ headCtrl.config.brand }}</a>
      </div>
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right" ng-hide="headCtrl.user">
          <li class="dropdown" ng-controller="AuthController as authCtrl">
            <a class="dropdown-toggle" href data-toggle="dropdown">
              Sign In
              <strong class="caret"></strong>
            </a>
            <div class="dropdown-menu auth-form-container" style="padding: 15px; padding-bottom: 0px;" >
              <form role="form" ng-submit="authCtrl.authenticate();">
                <div class="form-group">
                  <label for="exampleInputEmail1">Email address</label>
                  <input type="email" class="form-control" placeholder="Enter email" ng-model="authCtrl.authUser.emailAddress" >
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">Password</label>
                  <input type="password" class="form-control" placeholder="Password" ng-model="authCtrl.authUser.password">
                </div>
                <div class="form-group checkbox">
                  <label>
                        <input type="checkbox" ng-model="authCtrl.authUser.remember"> Remember Me
                  </label>
                </div>
                <div class="form-group">
                  <button class="btn btn-primary btn-full">Sign In</button>
                </div>
              </form>
            </div>
          </li>
          <li class="divider-vertical"></li>
          <li><a href="/#/register">Register</a></li>
        </ul>        
        <ul class="nav navbar-nav navbar-right" ng-show="headCtrl.user">
          <li><a href="/#/sign-in">Profile</a></li>
          <li class="divider-vertical"></li>
          <li ng-controller="AuthController as authCtrl">
            <a href ng-click="authCtrl.deauthenticate();">Sign Out</a>
          </li>
        </ul>        
      </div>
    </div>
  </nav>
</div>

可以在此处找到包括Parse密钥在内的整个项目。一旦我弄清楚我的问题是什么,我会删除密钥。

https://github.com/thenetimp/hackerspace

如果有人能指出我做错了什么并解释它,那么我就能理解我需要对其他模型做些什么我会非常感激。

1 个答案:

答案 0 :(得分:1)

每当你在angulars上下文之外更新模型时,即在一个promise的回调中,你需要使用$scope.apply来通知angular。