I have a service checking user authentication to redirect to login or logged in area.
var servicesModule = angular.module('servicesModule', []);
servicesModule.service('loginService', ['$state', function($state){
console.log('Inside');
var ref = new Firebase("https://feedback-system.firebaseio.com/");
var authData = ref.getAuth();
console.log('Inside');
if (authData) {
$state.go('tabs.courses');
console.log("User " + authData.uid + " is logged in with " + authData.provider);
} else {
$state.go('login');
}
}]);
var ionicApp = angular.module('feedback-system', ['ionic', 'firebase', 'servicesModule', 'coursesModule', 'feedbackModule', 'loginModule', 'tabsModule']);
ionicApp.run(function($ionicPlatform, $state) {
$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) {
StatusBar.styleDefault();
}
});
});
ionicApp.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'views/login.html',
controller: 'loginCtrl'
})
.state('tabs', {
url: '/tabs',
abstract: true,
templateUrl: 'views/main.html',
controller: 'tabsCtrl'
})
.state('tabs.courses', {
url: '/courses',
views:{
'coursesView':{
templateUrl: 'views/courses.html',
controller: 'coursesCtrl'
}
}
})
.state('tabs.feedback', {
url: '/feedback',
views:{
'feedbackView':{
templateUrl: 'views/feedback.html',
controller: 'feedbackCtrl'
}
}
})
.state('tabs.notifications', {
url: '/notifications',
views:{
'notificationsView':{
templateUrl: 'views/notification.html',
controller: 'notificationCtrl'
}
}
});
$urlRouterProvider.otherwise("/courses");
});
<!DOCTYPE html>
<html ng-app="feedback-system">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="lib/ngcordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.2.7/firebase.js"></script>
<script src="lib/angularfire.min.js"></script>
<!-- Services -->
<script src="js/services/services.js"></script>
<!-- Controllers -->
<script src="js/controllers/loginCtrl.js"></script>
<script src="js/controllers/coursesCtrl.js"></script>
<script src="js/controllers/feedbackCtrl.js"></script>
<script src="js/controllers/tabsCtrl.js"></script>
<!-- Application -->
<script src="js/app.js"></script>
</head>
<body>
<ion-nav-view></ion-nav-view>
</body>
</html>
I am injecting it in app.js main module and index.html, there is no error at all, but nothing is happening, no logging nothing in console. Any idea why this servies.js is not executed?
Sorry i forgot to mention that this service worked exactly how it is now without any return before, it simply stopped working today.
I think your service declaration is went wrong. wrap the content inside the service in a return function block.
servicesModule.service('loginService', ['$state', function($state){
return function() {
console.log('Inside');
var ref = new Firebase("https://feedback-system.firebaseio.com/");
var authData = ref.getAuth();
console.log('Inside');
if (authData) {
$state.go('tabs.courses');
console.log("User " + authData.uid + " is logged in with " + authData.provider);
} else {
$state.go('login');
}
}
}]);
in controller
servicesModule.controller('loginController', ['$scope', 'loginService', function($scope, loginService){
loginService();
}]);
Do this,
var servicesModule = angular.module('servicesModule', []);
servicesModule.service('loginService', ['$state', '$firebaseAuth', '$firebase', function($state,$firebaseAuth ,$firebase ){
return function() {
console.log('Inside');
var ref = new Firebase("https://feedback-system.firebaseio.com/");
var authData = $firebaseAuth(ref);
console.log('Inside');
if (authData) {
$state.go('tabs.courses');
console.log("User " + authData.uid + " is logged in with " + authData.provider);
} else {
$state.go('login');
}
}
}]);
Inject $firebaseAuth
and $firebase
and try. Also can you show the controller and how you are injecting this service in index.html?