firebase登录与离子无法正常工作

时间:2016-04-16 14:06:50

标签: javascript angularjs ionic-framework firebase

在我的索引中我导入所有库,我也在我的firebase中执行所有配置,只有登录部分不工作但基本插入部分与firebase数组一起使用,当我添加firebase auth它没有工作,我尝试这只有一个return语句,我需要同时使用firebase auth和firebase数组,

的login.html

<ion-view title="Login" class="login">
  <ion-content overflow-scroll="true" padding="true" scroll="false" class="has-header">
    <form class="list">
        <ion-list>
            <label class="item item-input">
                <span class="input-label">Username</span>
                <input type="text" placeholder=""ng-model="username">
            </label>
            <label class="item item-input">
                <span class="input-label">Password</span>
                <input type="password" placeholder=""ng-model="password">
            </label>
        </ion-list>
        <div class="spacer" style="width: 300px; height: 31px;"></div>
        <a id="login-button1" class="button button-block" ng-click="login()" > Log in</a>
        <a id="reg-button1" class="button button-block" ng-click="register(username,password)" > register</a>

        <a ui-sref="menu.signup" id="login-button4" class="button button-positive  button-block button-clear">Or create an account</a>

    </form>
  </ion-content>
</ion-view>

app.js

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.directives','app.services','firebase','angularMoment'])

.run(function($ionicPlatform) {

$ionicPlatform.ready(function() {
  // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
  // for form inputs)
  if(window.cordova && window.cordova.plugins.Keyboard) {
    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  }
  if(window.StatusBar) {
    // org.apache.cordova.statusbar required
    StatusBar.styleDefault();
  }
});

})

service.js

angular.module('app.services', [])
   .factory('CarInfor', function($firebaseAuth,$firebaseArray){
      var itemsRef = new Firebase('https://mycar12.firebaseio.com/');
      return $firebaseAuth(itemsRef);
      return $firebaseArray(itemsRef);
})

登录controller.js

 angular.module('app.controllers', [])

.controller('loginCtrl', function($scope,CarInfor) {  
     $scope.login = function(data) {
       var fbAuth = $firebaseAuth(CarInfor);
       fbAuth.$authWithPassword({

           email: username,
           password: password
       })
       .then(function(authData) {
           $location.path("/myads");
       })
       .catch(function(error) {
           console.error("ERROR: " + error);
       });
   }
   $scope.register = function(data) {
           var fbAuth = $firebaseAuth(CarInfor);
           fbAuth.$createUser({email: username, password: password}).then(function() {
               return fbAuth.$authWithPassword({
                   email: username,
                   password: password
               });
           }).then(function(authData) {
               $location.path("/myads");
           }).catch(function(error) {
               console.error("ERROR " + error);
           });
       }

})

2 个答案:

答案 0 :(得分:0)

请在此处查看完整的代码。http://codepen.io/aaronksaunders/pen/bpMrdr

务必设置firebasepath

  .value('ExampleConst', {
    'FBBasePath': 'https://[YOUR-APP-NAME].firebaseio.com/'
  })

html没有正确传回数据

  <ion-content class="has-subheader">
    <form class="list">
      <ion-list>
        <label class="item item-input">
            <span class="input-label">Username</span>
            <input type="text" placeholder=""ng-model="credentials.email">
        </label>
        <label class="item item-input">
            <span class="input-label">Password</span>
            <input type="password" placeholder=""ng-model="credentials.password">
        </label>
      </ion-list>
      <div class="spacer" style="width: 300px; height: 31px;"></div>
      <a id="login-button1" class="button button-block" ng-click="login(credentials)"> Log in</a>
      <a id="reg-button1" class="button button-block" ng-click="register(credentials)"> register</a>
    </form>
  </ion-content>

登录功能缺少正确的模型

.controller('LoginCtrl', function($scope, $state, $firebaseAuth, ExampleConst) {

  $scope.credentials = {
    email: "",
    password: ""
  }

  $scope.login = function(_credentials) {

  debugger;
  var fbRef = new Firebase(ExampleConst.FBBasePath)
  var fbAuth = $firebaseAuth(fbRef);
  fbAuth.$authWithPassword(_credentials)
    .then(function(authData) {
      console.log("Authentication Data:", authData)
      $state.go("home",{});
    })
    .catch(function(error) {
      console.error("ERROR: " + error);
      alert(error)
    });
}

答案 1 :(得分:0)

不推荐使用此方法,请查看错误消息。

此外,原始来源:

[Firebase文档] [2]