Skip to content

Instantly share code, notes, and snippets.

@TechNinjaWeb
Forked from AlphaNerd/app.js
Created April 16, 2016 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TechNinjaWeb/8dada77f5a2c3ce3cc8ae54b66a4247c to your computer and use it in GitHub Desktop.
Save TechNinjaWeb/8dada77f5a2c3ce3cc8ae54b66a4247c to your computer and use it in GitHub Desktop.
Solid framework for secure content, network connection status, and general route setup
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])
.run(function($ionicPlatform, $rootScope, $cordovaNetwork, $state, $authservice) {
$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);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
////// ACCESS CONTROL ////////
if (!$authservice.isAuthenticated()){
// User isn’t authenticated
$state.transitionTo("login");
event.preventDefault();
}
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){
if (toState.authenticate && !$authservice.isAuthenticated()){
// User isn’t authenticated
console.log("You are not permitted to access this resource.")
$state.transitionTo("login");
event.preventDefault();
}
});
///// NETWORK STATUS //////
var type = $cordovaNetwork.getNetwork()
var isOnline = $cordovaNetwork.isOnline()
var isOffline = $cordovaNetwork.isOffline()
// listen for Online event
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
var onlineState = networkState;
console.log(onlineState)
})
// listen for Offline event
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
var offlineState = networkState;
console.log(offlineState)
})
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginCtrl',
authenticate: false
})
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl',
authenticate: true
})
.state('app.search', {
url: '/search',
views: {
'menuContent': {
templateUrl: 'templates/search.html'
}
},
authenticate: true
})
.state('app.browse', {
url: '/browse',
views: {
'menuContent': {
templateUrl: 'templates/browse.html',
controller: 'BrowseCtrl'
}
},
authenticate: true
})
.state('app.playlists', {
url: '/playlists',
views: {
'menuContent': {
templateUrl: 'templates/playlists.html',
controller: 'PlaylistsCtrl'
}
},
authenticate: true
})
.state('app.single', {
url: '/playlists/:playlistId',
views: {
'menuContent': {
templateUrl: 'templates/playlist.html',
controller: 'PlaylistCtrl'
}
},
authenticate: true
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/browse');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment