Ionic firebase login Auth not functioning (email)

2019-08-11 19:53发布

Everything seems to work when using login/logout. But when i do a initial "ionic serve" i get located to the login page but if i then change the url to "/menu/map" i can get into map.html which should not be possible. Same thing happens after i click my "logout" button.

Whats wrong with my authentication code?

app.js

angular.module('starter', ['ionic', 'firebase'])
.config(function($ionicConfigProvider) {
  $ionicConfigProvider.scrolling.jsScrolling(true);
})
.run(function($ionicPlatform, $rootScope, Auth, $state) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
    //stateChange event
      $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){
      if (toState.authRequired && !Auth.isAuthenticated()){ //Assuming the AuthService holds authentication logic
        // User isn’t authenticated
        $state.go("login");
        event.preventDefault(); 
      }
    });
})
.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
      .state('login', {
        url: '/',
        templateUrl: 'templates/login/login.html',
        controller: 'userCtrl'
      })

      .state('app', {
        url: '/menu',
        abstract: true,
        templateUrl: 'templates/menu.html'
      })
      .state('app.map', {
        url: '/map',
        views: {
            'public-map': {
                templateUrl: 'templates/map.html',
                authRequired: true
            }
        }
      })
      $urlRouterProvider.otherwise("/");
})
.constant('FIREBASE_ROOT', 'https://<MYAPP>.firebaseio.com');

service.js

angular.module('starter')
.factory("Auth", function($firebaseAuth, FIREBASE_ROOT) {
  var usersRef = new Firebase(FIREBASE_ROOT);
  return $firebaseAuth(usersRef);
});

controller.js

angular.module('starter')
.controller('userCtrl', function($scope, $state, Auth) {
    $scope.login = function(email, password) {
        Auth.$authWithPassword({
            email: email,
            password: password
        }).then(function(authData) {
            $state.go("app.map");
        }).catch(function(error) {
            console.error("ERROR: " + error);
        });
    };
     $scope.logout = function() {
          Auth.$unauth();
          $state.go("login");
     };
})

1条回答
来,给爷笑一个
2楼-- · 2019-08-11 20:23

Apparently it's a common issue. The Ionic Team has added some instructions on how to solve it; whitelist your domain with the cordova-whitelist-plugin.

You can read all about it here: http://docs.ionic.io/docs/cordova-whitelist

查看更多
登录 后发表回答