简体   繁体   中英

firebase login with ionic not working

in my index i import all the libraries,i do all the configurations in my firebase also,only the login part is not working but basic insert part is woking with the firebase array,when i add firebase auth it didnt work,i try this with only one return statement also,i need to work both firebase auth and firebase array,,

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);
})

login 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);
           });
       }

})

See complete codepen here.... http://codepen.io/aaronksaunders/pen/bpMrdr

be sure to set firebasepath

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

html not passing the data back correctly

  <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>

login function was missing the proper models

.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)
    });
}

This method is deprecated, take a look at your error message.

Additionally, the original source:

[Firebase Docs][2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM